Ejemplo n.º 1
0
    def Run(self):
        """SandwichRunner main entry point meant to be called once configured."""
        assert self.output_dir is not None
        assert self._chrome_ctl == None
        assert self._local_cache_directory_path == None
        self._CleanTraceOutputDirectory()

        if self.android_device:
            self._chrome_ctl = controller.RemoteChromeController(
                self.android_device)
        else:
            self._chrome_ctl = controller.LocalChromeController()
        self._chrome_ctl.AddChromeArguments(self.chrome_args)
        if self.cache_operation == CacheOperation.SAVE:
            self._chrome_ctl.SetSlowDeath()
        try:
            if self.cache_operation == CacheOperation.PUSH:
                assert os.path.isfile(self.cache_archive_path)
                self._local_cache_directory_path = tempfile.mkdtemp(
                    suffix='.cache')
                chrome_cache.UnzipDirectoryContent(
                    self.cache_archive_path, self._local_cache_directory_path)
            times_repeated = self._RunWithWpr(0, False)
            if times_repeated < self.repeat:
                self._chrome_ctl.RebootDevice()
                self._RunWithWpr(times_repeated, True)
        finally:
            if self._local_cache_directory_path:
                shutil.rmtree(self._local_cache_directory_path)
                self._local_cache_directory_path = None
        if self.cache_operation == CacheOperation.SAVE:
            self._PullCacheFromDevice()

        self._chrome_ctl = None
def GenerateTrace(url, emulate_device, emulate_network, filename, log_filename):
  """ Generates a trace.

  Args:
    url: URL as a string.
    emulate_device: Name of the device to emulate. Empty for no emulation.
    emulate_network: Type of network emulation. Empty for no emulation.
    filename: Name of the file where the trace is saved.
    log_filename: Name of the file where standard output and errors are
                  logged.

  Returns:
    A dictionary of metadata about the trace, including a 'succeeded' field
    indicating whether the trace was successfully generated.
  """
  try:
    os.remove(filename)  # Remove any existing trace for this URL.
  except OSError:
    pass  # Nothing to remove.

  old_stdout = sys.stdout
  old_stderr = sys.stderr

  trace_metadata = { 'succeeded' : False, 'url' : url }
  trace = None
  if not url.startswith('http') and not url.startswith('file'):
    url = 'http://' + url
  with open(log_filename, 'w') as sys.stdout:
    try:
      sys.stderr = sys.stdout

      # Set up the controller.
      chrome_ctl = controller.LocalChromeController()
      chrome_ctl.SetChromeEnvOverride(xvfb_helper.GetChromeEnvironment())
      if emulate_device:
        chrome_ctl.SetDeviceEmulation(emulate_device)
      if emulate_network:
        chrome_ctl.SetNetworkEmulation(emulate_network)

      # Record and write the trace.
      with chrome_ctl.Open() as connection:
        connection.ClearCache()
        trace = loading_trace.LoadingTrace.RecordUrlNavigation(
            url, connection, chrome_ctl.ChromeMetadata())
        trace_metadata['succeeded'] = True
        trace_metadata.update(trace.ToJsonDict()[trace._METADATA_KEY])
    except controller.ChromeControllerError as e:
      e.Dump(sys.stderr)
    except Exception as e:
      sys.stderr.write('Unknown exception:\n' + str(e))
      traceback.print_exc(file=sys.stderr)

    if trace:
      with open(filename, 'w') as f:
        json.dump(trace.ToJsonDict(), f, sort_keys=True, indent=2)

  sys.stdout = old_stdout
  sys.stderr = old_stderr

  return trace_metadata
Ejemplo n.º 3
0
def _LogRequests(url, clear_cache_override=None):
  """Logs requests for a web page.

  Args:
    url: url to log as string.
    clear_cache_override: if not None, set clear_cache different from OPTIONS.

  Returns:
    JSON dict of logged information (ie, a dict that describes JSON).
  """
  if OPTIONS.local:
    chrome_ctl = controller.LocalChromeController()
    chrome_ctl.SetHeadless(OPTIONS.headless)
  else:
    chrome_ctl = controller.RemoteChromeController(
        device_setup.GetFirstDevice())

  clear_cache = (clear_cache_override if clear_cache_override is not None
                 else OPTIONS.clear_cache)
  if OPTIONS.emulate_device:
    chrome_ctl.SetDeviceEmulation(OPTIONS.emulate_device)
  if OPTIONS.emulate_network:
    chrome_ctl.SetNetworkEmulation(OPTIONS.emulate_network)
  with chrome_ctl.Open() as connection:
    if clear_cache:
      connection.ClearCache()
    trace = loading_trace.LoadingTrace.RecordUrlNavigation(
        url, connection, chrome_ctl.ChromeMetadata())
  return trace.ToJsonDict()
    def Run(self):
        """SandwichRunner main entry point meant to be called once configured."""
        assert self._chrome_ctl == None
        assert self._local_cache_directory_path == None
        if self.trace_output_directory:
            self._CleanTraceOutputDirectory()

        if self.android_device:
            self._chrome_ctl = controller.RemoteChromeController(
                self.android_device)
        else:
            self._chrome_ctl = controller.LocalChromeController()
        self._chrome_ctl.AddChromeArgument('--disable-infobars')
        if self.cache_operation == 'save':
            self._chrome_ctl.SetSlowDeath()

        if self.cache_operation == 'push':
            assert os.path.isfile(self.cache_archive_path)
            self._local_cache_directory_path = tempfile.mkdtemp(
                suffix='.cache')
            chrome_cache.UnzipDirectoryContent(
                self.cache_archive_path, self._local_cache_directory_path)

        out_log_path = None
        if self.trace_output_directory:
            out_log_path = os.path.join(self.trace_output_directory,
                                        WPR_LOG_FILENAME)

        ran_urls = []
        with self._chrome_ctl.OpenWprHost(
                self.wpr_archive_path,
                record=self.wpr_record,
                network_condition_name=self._GetEmulatorNetworkCondition(
                    'wpr'),
                disable_script_injection=self.disable_wpr_script_injection,
                out_log_path=out_log_path):
            for _ in xrange(self.job_repeat):
                for url in self.urls:
                    self._RunUrl(url, run_id=len(ran_urls))
                    ran_urls.append(url)

        if self._local_cache_directory_path:
            shutil.rmtree(self._local_cache_directory_path)
            self._local_cache_directory_path = None
        if self.cache_operation == 'save':
            self._PullCacheFromDevice()

        self._chrome_ctl = None
Ejemplo n.º 5
0
def _LogRequests(url, clear_cache_override=None):
    """Logs requests for a web page.

  Args:
    url: url to log as string.
    clear_cache_override: if not None, set clear_cache different from OPTIONS.

  Returns:
    JSON dict of logged information (ie, a dict that describes JSON).
  """
    xvfb_process = None
    if OPTIONS.local:
        chrome_ctl = controller.LocalChromeController()
        if OPTIONS.headless:
            xvfb_process = xvfb_helper.LaunchXvfb()
            chrome_ctl.SetChromeEnvOverride(xvfb_helper.GetChromeEnvironment())
    else:
        chrome_ctl = controller.RemoteChromeController(
            device_setup.GetFirstDevice())

    clear_cache = (clear_cache_override if clear_cache_override is not None
                   else OPTIONS.clear_cache)
    if OPTIONS.emulate_device:
        chrome_ctl.SetDeviceEmulation(OPTIONS.emulate_device)
    if OPTIONS.emulate_network:
        chrome_ctl.SetNetworkEmulation(OPTIONS.emulate_network)
    try:
        with chrome_ctl.Open() as connection:
            if clear_cache:
                connection.ClearCache()
            trace = loading_trace.LoadingTrace.RecordUrlNavigation(
                url,
                connection,
                chrome_ctl.ChromeMetadata(),
                categories=clovis_constants.DEFAULT_CATEGORIES)
    except controller.ChromeControllerError as e:
        e.Dump(sys.stderr)
        raise

    if xvfb_process:
        xvfb_process.terminate()

    return trace.ToJsonDict()
Ejemplo n.º 6
0
def RunTest(webserver, test_page, expected):
    """Run an webserver test.

  The expected result can be None, in which case --failed_trace_dir can be set
  to output the observed trace.

  Args:
    webserver [WebServer]: the webserver to use for the test. It must be
      started.
    test_page: the name of the page to load.
    expected [InitiatorSequence]: expected initiator sequence.

  Returns:
    True if the test passed and false otherwise. Status is printed to stdout.
  """
    url = 'http://%s/%s' % (webserver.Address(), test_page)
    sys.stdout.write('Testing %s...' % url)
    chrome_controller = controller.LocalChromeController()

    with chrome_controller.Open() as connection:
        connection.ClearCache()
        observed_seq = InitiatorSequence(
            loading_trace.LoadingTrace.RecordUrlNavigation(
                url,
                connection,
                chrome_controller.ChromeMetadata(),
                categories=clovis_constants.DEFAULT_CATEGORIES))
    if observed_seq == expected:
        sys.stdout.write(' ok\n')
        return True
    else:
        sys.stdout.write(' FAILED!\n')
        if OPTIONS.failed_trace_dir:
            outname = os.path.join(OPTIONS.failed_trace_dir,
                                   test_page + '.observed_result')
            with file(outname, 'w') as output:
                observed_seq.DumpToFile(output)
            sys.stdout.write('Wrote observed result to %s\n' % outname)
    return False