def get_marionette(self): if not self.m: self.m = Marionette(port=self.port) self.m.start_session() self.device = GaiaDevice(self.m) self.device.add_device_manager(self.dm) self.gaia_apps = GaiaApps(self.m) else: tries = 5 while tries > 0: try: self.m.get_url() break except MarionetteException as e: if "Please start a session" in str(e): time.sleep(5) self.m = Marionette(port=self.port) self.m.start_session() self.device = GaiaDevice(self.m) self.device.add_device_manager(self.dm) self.gaia_apps = GaiaApps(self.m) tries -= 1 else: raise e else: self.run_log.error("Can't connect to marionette, rebooting") self.restart_device() return self.m
def set_up_device(opt): if not opt.wifi_ssid or not opt.wifi_key or not opt.wifi_pass: raise ValueError('Missing --wifi options') mc = Marionette('localhost', opt.adb_port) for i in range(2): try: mc.start_session() break except socket.error: sh('adb forward tcp:%s tcp:%s' % (opt.adb_port, opt.adb_port)) if opt.shell: from pdb import set_trace set_trace() return # watch out! This is how gaiatest does it. mc.__class__ = type('Marionette', (Marionette, MarionetteTouchMixin), {}) device = GaiaDevice(mc) device.restart_b2g() apps = GaiaApps(mc) data_layer = GaiaData(mc) lockscreen = LockScreen(mc) mc.setup_touch() lockscreen.unlock() apps.kill_all() data_layer.enable_wifi() if opt.wifi_key == 'WPA-PSK': pass_key = 'psk' elif opt.wifi_key == 'WEP': pass_key = 'wep' else: assert 0, 'unknown key management' data = {'ssid': opt.wifi_ssid, 'keyManagement': opt.wifi_key, pass_key: opt.wifi_pass} data_layer.connect_to_wifi(data) mc.switch_to_frame() all_apps = set(a['manifest']['name'] for a in get_installed(apps)) if 'Marketplace Dev' not in all_apps: mc.execute_script( 'navigator.mozApps.install' '("https://marketplace-dev.allizom.org/manifest.webapp");') wait_for_element_displayed(mc, 'id', 'app-install-install-button') yes = mc.find_element('id', 'app-install-install-button') mc.tap(yes) wait_for_element_displayed(mc, 'id', 'system-banner') print 'Pushing payment prefs' sh('adb shell stop b2g') sh('adb push "%s" /data/local/user.js' % ( os.path.join(os.path.dirname(__file__), 'payment-prefs.js'))) sh('adb shell start b2g') print 'When your device reboots, Marketplace Dev will be installed'
def install_apps(): mc = get_marionette(args) device = GaiaDevice(mc) try: device.restart_b2g() print 'Your device is rebooting.' except Exception: print ' ** Check to make sure you don\'t have desktop B2G running' raise apps = GaiaApps(mc) apps.kill_all() lockscreen = LockScreen(mc) lockscreen.unlock() if args.wifi_ssid: print 'Configuring WiFi' if not args.wifi_key or not args.wifi_pass: args.error('Missing --wifi_key or --wifi_pass option') args.wifi_key = args.wifi_key.upper() data_layer = GaiaData(mc) data_layer.enable_wifi() if args.wifi_key == 'WPA-PSK': pass_key = 'psk' elif args.wifi_key == 'WEP': pass_key = 'wep' else: args.error('not sure what key to use for %r' % args.wifi_key) data = { 'ssid': args.wifi_ssid, 'keyManagement': args.wifi_key, pass_key: args.wifi_pass } data_layer.connect_to_wifi(data) # disconnect marionette client because install_app would need it mc.client.close() # install apps one by one for manifest in args.apps: args.manifest = manifest args.app = None install_app(args)
def setup(self): if not self.serial or not self.port: logger.error("Fail to get device") raise DMError self.config_raptor() self.marionette and self.marionette.session and self.marionette.cleanup( ) self.dm = mozdevice.DeviceManagerADB(deviceSerial=self.serial, port=self.port) self.marionette = Marionette(device_serial=self.serial, port=self.port) self.marionette.wait_for_port() self.marionette.start_session() self.device = GaiaDevice(marionette=self.marionette, manager=self.dm) self.apps = GaiaApps(self.marionette) self.data_layer = GaiaData(self.marionette) if self.flashed: self.device.wait_for_b2g_ready()
def restart_device(self, restart_tries=0): self.run_log.info("rebooting") # TODO restarting b2g doesn't seem to work... reboot then while restart_tries < 3: restart_tries += 1 self.dm.reboot(wait=True) self.run_log.info("forwarding") if not self.forward_port(): self.run_log.error("couldn't forward port in time, rebooting") continue self.m = Marionette(port=self.port) if not self.m.wait_for_port(180): self.run_log.error( "couldn't contact marionette in time, rebooting") continue time.sleep(1) self.m.start_session() try: Wait(self.m, timeout=240).until(lambda m: m.find_element( "id", "lockscreen-container").is_displayed()) # It retuns a little early time.sleep(2) self.device = GaiaDevice(self.m) self.device.add_device_manager(self.dm) self.device.unlock() self.gaia_apps = GaiaApps(self.m) except (MarionetteException, IOError, socket.error) as e: self.run_log.error("got exception: %s, going to retry" % e) try: self.m.delete_session() except: # at least attempt to clear the session if possible pass continue break else: raise Exception( "Couldn't restart the device in time, even after 3 tries")
def run(self, script, address='localhost:2828', symbols=None, treeherder='https://treeherder.mozilla.org/', reset=False, **kwargs): try: host, port = address.split(':') except ValueError: raise ValueError('--address must be in the format host:port') # Check that Orangutan is installed self.adb_device = ADBDevice(self.device_serial) orng_path = posixpath.join('data', 'local', 'orng') if not self.adb_device.exists(orng_path): raise Exception('Orangutan not found! Please install it according ' 'to the documentation.') self.runner = B2GDeviceRunner(serial=self.device_serial, process_args={'stream': None}, symbols_path=symbols, logdir=self.temp_dir) if reset: self.runner.start() else: self.runner.device.connect() port = self.runner.device.setup_port_forwarding(remote_port=port) assert self.runner.device.wait_for_port(port), \ 'Timed out waiting for port!' marionette = Marionette(host=host, port=port) marionette.start_session() try: marionette.set_context(marionette.CONTEXT_CHROME) self.is_debug = marionette.execute_script( 'return Components.classes["@mozilla.org/xpcom/debug;1"].' 'getService(Components.interfaces.nsIDebug2).isDebugBuild;') marionette.set_context(marionette.CONTEXT_CONTENT) if reset: gaia_device = GaiaDevice(marionette) gaia_device.wait_for_b2g_ready(timeout=120) gaia_device.unlock() gaia_apps = GaiaApps(marionette) gaia_apps.kill_all() # TODO: Disable bluetooth, emergency calls, carrier, etc # Run Orangutan script remote_script = posixpath.join(self.adb_device.test_root, 'orng.script') self.adb_device.push(script, remote_script) self.start_time = time.time() # TODO: Kill remote process on keyboard interrupt self.adb_device.shell( '%s %s %s' % (orng_path, self.device_properties['input'], remote_script)) self.end_time = time.time() self.adb_device.rm(remote_script) except (MarionetteException, IOError): if self.runner.crashed: # Crash has been detected pass else: raise self.runner.check_for_crashes(test_name='b2gmonkey') # Report results to Treeherder required_envs = ['TREEHERDER_KEY', 'TREEHERDER_SECRET'] if all([os.environ.get(v) for v in required_envs]): self.post_to_treeherder(script, treeherder) else: self._logger.info( 'Results will not be posted to Treeherder. Please set the ' 'following environment variables to enable Treeherder ' 'reports: %s' % ', '.join([v for v in required_envs if not os.environ.get(v)]))
def do_login(args): mc = get_marionette(args) device = GaiaDevice(mc) apps = GaiaApps(mc) data_layer = GaiaData(mc) mc.setup_touch() _persona_frame_locator = ('css selector', "iframe") # Trusty UI on home screen _tui_container_locator = ('id', 'trustedui-frame-container') # Persona dialog _waiting_locator = ('css selector', 'body.waiting') _email_input_locator = ('id', 'authentication_email') _password_input_locator = ('id', 'authentication_password') _new_password = ('id', 'password') _verify_new_password = ('id', 'vpassword') _next_button_locator = ('css selector', 'button.start') _verify_start_button = ('css selector', 'button#verify_user') _returning_button_locator = ('css selector', 'button.returning') _sign_in_button_locator = ('id', 'signInButton') _this_session_only_button_locator = ('id', 'this_is_not_my_computer') # Switch to top level frame then Persona frame mc.switch_to_frame() wait_for_element_present(mc, *_tui_container_locator) trustyUI = mc.find_element(*_tui_container_locator) wait_for_condition( mc, lambda m: trustyUI.find_element(*_persona_frame_locator)) personaDialog = trustyUI.find_element(*_persona_frame_locator) mc.switch_to_frame(personaDialog) try: ready = mc.find_element(*_email_input_locator).is_displayed() except NoSuchElementException: ready = False if not ready: print 'Persona email input is not present.' print 'Are you on a new login screen?' return done = False while not done: username = raw_input('Persona username: '******'password: '******'Not a new account? Trying to log in to existing account' # Logging into an exisiting account: password_field = mc.find_element(*_password_input_locator) password_field.send_keys(password) wait_for_element_displayed(mc, *_returning_button_locator) mc.tap(mc.find_element(*_returning_button_locator)) #.click() print 'You should be logged in now'
def set_up_device(args): mc = get_marionette(args) device = GaiaDevice(mc) try: device.restart_b2g() except Exception: print ' ** Check to make sure you don\'t have desktop B2G running' raise apps = GaiaApps(mc) data_layer = GaiaData(mc) lockscreen = LockScreen(mc) mc.setup_touch() lockscreen.unlock() apps.kill_all() if args.wifi_ssid: print 'Configuring WiFi' if not args.wifi_key or not args.wifi_pass: args.error('Missing --wifi_key or --wifi_pass option') args.wifi_key = args.wifi_key.upper() data_layer.enable_wifi() if args.wifi_key == 'WPA-PSK': pass_key = 'psk' elif args.wifi_key == 'WEP': pass_key = 'wep' else: args.error('not sure what key to use for %r' % args.wifi_key) data = { 'ssid': args.wifi_ssid, 'keyManagement': args.wifi_key, pass_key: args.wifi_pass } data_layer.connect_to_wifi(data) for manifest in args.apps: # There is probably a way easier way to do this by adb pushing # something. Send me a patch! mc.switch_to_frame() try: data = requests.get(manifest).json() app_name = data['name'] all_apps = set(a['manifest']['name'] for a in get_installed(apps)) if app_name not in all_apps: print 'Installing %s from %s' % (app_name, manifest) mc.execute_script('navigator.mozApps.install("%s");' % manifest) wait_for_element_displayed(mc, 'id', 'app-install-install-button') yes = mc.find_element('id', 'app-install-install-button') mc.tap(yes) # This still works but the id check broke. # See https://bugzilla.mozilla.org/show_bug.cgi?id=853878 wait_for_element_displayed(mc, 'id', 'system-banner') except Exception, exc: print ' ** installing manifest %s failed (maybe?)' % manifest print ' ** error: %s: %s' % (exc.__class__.__name__, exc) continue