コード例 #1
0
    def _RunNavigation(self, url, clear_cache, run_id=None):
        """Run a page navigation to the given URL.

    Args:
      url: The URL to navigate to.
      clear_cache: Whether if the cache should be cleared before navigation.
      run_id: Id of the run in the output directory. If it is None, then no
        trace or video will be saved.
    """
        run_path = None
        if self.trace_output_directory is not None and run_id is not None:
            run_path = os.path.join(self.trace_output_directory, str(run_id))
            if not os.path.isdir(run_path):
                os.makedirs(run_path)
        self._chrome_ctl.SetNetworkEmulation(
            self._GetEmulatorNetworkCondition('browser'))
        additional_categories = []
        if self.record_memory_dumps:
            additional_categories = [MEMORY_DUMP_CATEGORY]
        # TODO(gabadie): add a way to avoid recording a trace.
        with self._chrome_ctl.Open() as connection:
            if clear_cache:
                connection.ClearCache()
            if run_path is not None and self.record_video:
                device = self._chrome_ctl.GetDevice()
                if device is None:
                    raise RuntimeError(
                        'Can only record video on a remote device.')
                video_recording_path = os.path.join(run_path, VIDEO_FILENAME)
                with device_setup.RemoteSpeedIndexRecorder(
                        device, connection, video_recording_path):
                    trace = loading_trace.LoadingTrace.RecordUrlNavigation(
                        url=url,
                        connection=connection,
                        chrome_metadata=self._chrome_ctl.ChromeMetadata(),
                        additional_categories=additional_categories,
                        timeout_seconds=_DEVTOOLS_TIMEOUT)
            else:
                trace = loading_trace.LoadingTrace.RecordUrlNavigation(
                    url=url,
                    connection=connection,
                    chrome_metadata=self._chrome_ctl.ChromeMetadata(),
                    additional_categories=additional_categories,
                    timeout_seconds=_DEVTOOLS_TIMEOUT)
        if run_path is not None:
            trace_path = os.path.join(run_path, TRACE_FILENAME)
            trace.ToJsonFile(trace_path)
コード例 #2
0
ファイル: sandwich_runner.py プロジェクト: youminxue/gndemo
    def _RunNavigation(self, clear_cache, repeat_id=None):
        """Run a page navigation to the given URL.

    Args:
      clear_cache: Whether if the cache should be cleared before navigation.
      repeat_id: Id of the run in the output directory. If it is None, then no
        trace or video will be saved.
    """
        run_path = None
        if repeat_id is not None:
            run_path = os.path.join(self.output_dir, str(repeat_id))
            if not os.path.isdir(run_path):
                os.makedirs(run_path)
        self._chrome_ctl.SetNetworkEmulation(
            self._GetEmulatorNetworkCondition('browser'))
        categories = _TRACING_CATEGORIES
        if self.record_memory_dumps:
            categories += [MEMORY_DUMP_CATEGORY]
        if self.record_first_meaningful_paint:
            categories += TTFMP_ADDITIONAL_CATEGORIES
        stop_delay_multiplier = 0
        if self.wpr_record or self.cache_operation == CacheOperation.SAVE:
            stop_delay_multiplier = self._STOP_DELAY_MULTIPLIER
        # TODO(gabadie): add a way to avoid recording a trace.
        with common_util.TimeoutScope(self._ABORT_RUN_TIMEOUT_SECONDS,
                                      'Sandwich run overdue.'):
            with self._chrome_ctl.Open() as connection:
                if clear_cache:
                    connection.ClearCache()

                # Binds all parameters of RecordUrlNavigation() to avoid repetition.
                def RecordTrace():
                    return loading_trace.LoadingTrace.RecordUrlNavigation(
                        url=self.url,
                        connection=connection,
                        chrome_metadata=self._chrome_ctl.ChromeMetadata(),
                        categories=categories,
                        timeout_seconds=_DEVTOOLS_TIMEOUT,
                        stop_delay_multiplier=stop_delay_multiplier)

                if run_path is not None and self.record_video:
                    device = self._chrome_ctl.GetDevice()
                    if device is None:
                        raise RuntimeError(
                            'Can only record video on a remote device.')
                    video_recording_path = os.path.join(
                        run_path, VIDEO_FILENAME)
                    with device_setup.RemoteSpeedIndexRecorder(
                            device, connection, video_recording_path):
                        trace = RecordTrace()
                else:
                    trace = RecordTrace()
                for event in trace.request_track.GetEvents():
                    if event.failed:
                        logging.warning('request to %s failed: %s', event.url,
                                        event.error_text)
                if not trace.tracing_track.HasLoadingSucceeded():
                    raise SandwichRunnerError('Page load has failed.')
        if run_path is not None:
            trace_path = os.path.join(run_path, TRACE_FILENAME)
            trace.ToJsonFile(trace_path)