def Open(self): """Overridden connection creation.""" if self._wpr_attributes: assert self._wpr_attributes.chrome_env_override == {}, \ 'Remote controller doesn\'t support chrome environment variables.' package_info = OPTIONS.ChromePackage() command_line_path = '/data/local/chrome-command-line' self._device.ForceStop(package_info.package) chrome_args = self._GetChromeArguments() logging.info( 'Launching %s with flags: %s' % (package_info.package, subprocess.list2cmdline(chrome_args))) with device_setup.FlagReplacer(self._device, command_line_path, self._GetChromeArguments()): start_intent = intent.Intent(package=package_info.package, activity=package_info.activity, data='about:blank') self._device.adb.Logcat(clear=True, dump=True) self._device.StartActivity(start_intent, blocking=True) try: for attempt_id in xrange(self.DEVTOOLS_CONNECTION_ATTEMPTS): logging.info('Devtools connection attempt %d' % attempt_id) with device_setup.ForwardPort( self._device, 'tcp:%d' % OPTIONS.devtools_port, 'localabstract:chrome_devtools_remote'): try: connection = devtools_monitor.DevToolsConnection( OPTIONS.devtools_hostname, OPTIONS.devtools_port) self._StartConnection(connection) except socket.error as e: if e.errno != errno.ECONNRESET: raise time.sleep( self. DEVTOOLS_CONNECTION_ATTEMPT_INTERVAL_SECONDS) continue yield connection if self._slow_death: self._device.adb.Shell( 'am start com.google.android.launcher') time.sleep(self.TIME_TO_IDLE_SECONDS) break else: raise ChromeControllerInternalError( 'Failed to connect to Chrome devtools after {} ' 'attempts.'.format(self.DEVTOOLS_CONNECTION_ATTEMPTS)) except: logcat = ''.join( [l + '\n' for l in self._device.adb.Logcat(dump=True)]) raise ChromeControllerError(log=logcat) finally: self._device.ForceStop(package_info.package)
def Open(self): """Overridden connection creation.""" package_info = OPTIONS.ChromePackage() command_line_path = '/data/local/chrome-command-line' self._device.KillAll(package_info.package, quiet=True) chrome_args = self._GetChromeArguments() logging.info( 'Launching %s with flags: %s' % (package_info.package, subprocess.list2cmdline(chrome_args))) with device_setup.FlagReplacer(self._device, command_line_path, self._GetChromeArguments()): start_intent = intent.Intent(package=package_info.package, activity=package_info.activity, data='about:blank') self._device.StartActivity(start_intent, blocking=True) try: for attempt_id in xrange(self.DEVTOOLS_CONNECTION_ATTEMPTS + 1): if attempt_id == self.DEVTOOLS_CONNECTION_ATTEMPTS: raise RuntimeError( 'Failed to connect to chrome devtools after {} ' 'attempts.'.format(attempt_id)) logging.info('Devtools connection attempt %d' % attempt_id) with device_setup.ForwardPort( self._device, 'tcp:%d' % OPTIONS.devtools_port, 'localabstract:chrome_devtools_remote'): try: connection = devtools_monitor.DevToolsConnection( OPTIONS.devtools_hostname, OPTIONS.devtools_port) self._StartConnection(connection) except socket.error as e: assert str(e).startswith( '[Errno 104] Connection reset by peer') time.sleep( self. DEVTOOLS_CONNECTION_ATTEMPT_INTERVAL_SECONDS) continue logging.info('Devtools connection success') yield connection if self._slow_death: self._device.adb.Shell( 'am start com.google.android.launcher') time.sleep(self.TIME_TO_IDLE_SECONDS) break finally: self._device.KillAll(package_info.package, quiet=True)
def Open(self): """Overridden connection creation.""" if self._wpr_attributes: assert self._wpr_attributes.chrome_env_override == {}, \ 'Remote controller doesn\'t support chrome environment variables.' package_info = OPTIONS.ChromePackage() command_line_path = '/data/local/chrome-command-line' self._device.ForceStop(package_info.package) chrome_args = self._GetChromeArguments() logging.info( 'Launching %s with flags: %s' % (package_info.package, subprocess.list2cmdline(chrome_args))) with device_setup.FlagReplacer(self._device, command_line_path, self._GetChromeArguments()): self._DismissCrashDialogIfNeeded() start_intent = intent.Intent(package=package_info.package, activity=package_info.activity, data='about:blank') self._device.adb.Logcat(clear=True, dump=True) self._device.StartActivity(start_intent, blocking=True) try: for attempt_id in xrange(self.DEVTOOLS_CONNECTION_ATTEMPTS): logging.info('Devtools connection attempt %d' % attempt_id) # Adb forwarding does not provide a way to print the port number if # it is allocated atomically by the OS by passing port=0, but we need # dynamically allocated listening port here to handle parallel run on # different devices. host_side_port = _AllocateTcpListeningPort() logging.info( 'Allocated host sided listening port for devtools ' 'connection: %d', host_side_port) try: with device_setup.ForwardPort( self._device, 'tcp:%d' % host_side_port, 'localabstract:chrome_devtools_remote'): try: connection = devtools_monitor.DevToolsConnection( OPTIONS.devtools_hostname, host_side_port) self._StartConnection(connection) except socket.error as e: if e.errno != errno.ECONNRESET: raise time.sleep( self. DEVTOOLS_CONNECTION_ATTEMPT_INTERVAL_SECONDS ) continue yield connection if self._slow_death: self._device.adb.Shell( 'am start com.google.android.launcher') time.sleep(self.TIME_TO_IDLE_SECONDS) break except device_errors.AdbCommandFailedError as error: _KNOWN_ADB_FORWARDER_FAILURES = [ 'cannot bind to socket: Address already in use', 'cannot rebind existing socket: Resource temporarily unavailable' ] for message in _KNOWN_ADB_FORWARDER_FAILURES: if message in error.message: break else: raise continue else: raise ChromeControllerInternalError( 'Failed to connect to Chrome devtools after {} ' 'attempts.'.format(self.DEVTOOLS_CONNECTION_ATTEMPTS)) except ChromeControllerError._PASSTHROUGH_WHITE_LIST: raise except Exception: logcat = ''.join( [l + '\n' for l in self._device.adb.Logcat(dump=True)]) raise ChromeControllerError(log=logcat) finally: self._device.ForceStop(package_info.package) self._DismissCrashDialogIfNeeded()