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
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()