def _CreateBrowser(self, autotest_ext=False, auto_login=True, gaia_login=False, username=None, password=None): """Finds and creates a browser for tests. if autotest_ext is True, also loads the autotest extension""" options = options_for_unittests.GetCopy() if autotest_ext: extension_path = os.path.join(util.GetUnittestDataDir(), 'autotest_ext') assert os.path.isdir(extension_path) self._load_extension = extension_to_load.ExtensionToLoad( path=extension_path, browser_type=options.browser_type, is_component=True) options.extensions_to_load = [self._load_extension] browser_to_create = browser_finder.FindBrowser(options) self.assertTrue(browser_to_create) browser_options = options.browser_options browser_options.create_browser_with_oobe = True browser_options.auto_login = auto_login browser_options.gaia_login = gaia_login if username is not None: browser_options.username = username if password is not None: browser_options.password = password return browser_to_create.Create(options)
def testLoginStatus(self): extension_path = os.path.join(os.path.dirname(__file__), 'autotest_ext') load_extension = extension_to_load.ExtensionToLoad( extension_path, True) options = options_for_unittests.GetCopy() options.extensions_to_load = [load_extension] browser_to_create = browser_finder.FindBrowser(options) self.assertTrue(browser_to_create) with browser_to_create.Create() as b: extension = b.extensions[load_extension] self.assertTrue(extension) extension.ExecuteJavaScript(''' chrome.autotestPrivate.loginStatus(function(s) { window.__autotest_result = s; }); ''') login_status = extension.EvaluateJavaScript( 'window.__autotest_result') self.assertEquals(type(login_status), dict) self.assertEquals(not self._is_guest, login_status['isRegularUser']) self.assertEquals(self._is_guest, login_status['isGuest']) self.assertEquals(login_status['email'], self._email) self.assertFalse(login_status['isScreenLocked'])
def testNonExistentExtensionPath(self): """Test that a non-existent extension path will raise an exception.""" extension_path = os.path.join(os.path.dirname(__file__), '..', '..', 'unittest_data', 'foo') self.assertRaises( extension_to_load.ExtensionPathNonExistentException, lambda: extension_to_load.ExtensionToLoad(extension_path))
def testNonExistentExtensionPath(self): """Test that a non-existent extension path will raise an exception.""" extension_path = os.path.join(util.GetUnittestDataDir(), 'foo') options = options_for_unittests.GetCopy() self.assertRaises(extension_to_load.ExtensionPathNonExistentException, lambda: extension_to_load.ExtensionToLoad( extension_path, options.browser_type))
def testComponentExtensionNoPublicKey(self): # simple_extension does not have a public key. extension_path = os.path.join(os.path.dirname(__file__), '..', '..', 'unittest_data', 'simple_extension') self.assertRaises( extension_to_load.MissingPublicKeyException, lambda: extension_to_load.ExtensionToLoad(extension_path, True))
def testComponentExtensionNoPublicKey(self): # simple_extension does not have a public key. extension_path = os.path.join(util.GetUnittestDataDir(), 'simple_extension') options = options_for_unittests.GetCopy() self.assertRaises(extension_to_load.MissingPublicKeyException, lambda: extension_to_load.ExtensionToLoad( extension_path, browser_type=options.browser_type, is_component=True))
def testExtensionNotLoaded(self): """Querying an extension that was not loaded will return None""" extension_path = os.path.join(util.GetUnittestDataDir(), 'simple_extension') options = options_for_unittests.GetCopy() load_extension = extension_to_load.ExtensionToLoad( extension_path, options.browser_type) browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: if b.supports_extensions: self.assertRaises(KeyError, lambda: b.extensions[load_extension])
def Start(self, local_app_path=None): """ Connects to the ChromeOS device and logs in. Args: local_app_path: A path on the local device containing the Smart Lock app to use instead of the app on the ChromeOS device. Return: |self| for using in a "with" statement. """ assert (self._browser is None) finder_opts = browser_options.BrowserFinderOptions('cros-chrome') finder_opts.CreateParser().parse_args(args=[]) finder_opts.cros_remote = self._remote_address if self._ssh_port is not None: finder_opts.cros_remote_ssh_port = self._ssh_port finder_opts.verbosity = 1 browser_opts = finder_opts.browser_options browser_opts.create_browser_with_oobe = True browser_opts.disable_component_extensions_with_background_pages = False browser_opts.gaia_login = True browser_opts.username = self._username browser_opts.password = self._password browser_opts.auto_login = True self._cros_interface = cros_interface.CrOSInterface( finder_opts.cros_remote, finder_opts.cros_remote_ssh_port, finder_opts.cros_ssh_identity) browser_opts.disable_default_apps = local_app_path is not None if local_app_path is not None: easy_unlock_app = extension_to_load.ExtensionToLoad( path=local_app_path, browser_type='cros-chrome', is_component=True) finder_opts.extensions_to_load.append(easy_unlock_app) retries = 3 while self._browser is not None or retries > 0: try: browser_to_create = browser_finder.FindBrowser(finder_opts) self._browser = browser_to_create.Create(finder_opts) break except (exceptions.LoginException) as e: logger.error('Timed out logging in: %s' % e) if retries == 1: raise bg_page_path = '/_generated_background_page.html' util.WaitFor( lambda: self._FindSmartLockAppPage(bg_page_path) is not None, 10) self._background_page = self._FindSmartLockAppPage(bg_page_path) return self
def testExtensionNotLoaded(self): """Querying an extension that was not loaded will return None""" extension_path = os.path.join(os.path.dirname(__file__), '..', '..', 'unittest_data', 'simple_extension') load_extension = extension_to_load.ExtensionToLoad(extension_path) options = options_for_unittests.GetCopy() browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: if b.supports_extensions: self.assertRaises( extension_dict_backend.ExtensionNotFoundException, lambda: b.extensions[load_extension])
def _CreateBrowser(self, with_autotest_ext): """Finds and creates a browser for tests. if with_autotest_ext is True, also loads the autotest extension""" options = options_for_unittests.GetCopy() if with_autotest_ext: extension_path = os.path.join(os.path.dirname(__file__), 'autotest_ext') self._load_extension = extension_to_load.ExtensionToLoad( extension_path, True) options.extensions_to_load = [self._load_extension] browser_to_create = browser_finder.FindBrowser(options) self.assertTrue(browser_to_create) return browser_to_create.Create()
def CreateBrowserWithExtension(self, ext_path): extension_path = os.path.join(util.GetUnittestDataDir(), ext_path) options = options_for_unittests.GetCopy() load_extension = extension_to_load.ExtensionToLoad( extension_path, options.browser_type) options.extensions_to_load = [load_extension] browser_to_create = browser_finder.FindBrowser(options) if not browser_to_create: # May not find a browser that supports extensions. return False self._browser = browser_to_create.Create() self._extension = self._browser.extensions[load_extension] self._extension_id = load_extension.extension_id self.assertTrue(self._extension) return True
def setUp(self): extension_path = os.path.join(util.GetUnittestDataDir(), 'simple_extension') options = options_for_unittests.GetCopy() load_extension = extension_to_load.ExtensionToLoad( extension_path, options.browser_type) options.extensions_to_load = [load_extension] browser_to_create = browser_finder.FindBrowser(options) self._browser = None self._extension = None if not browser_to_create: # May not find a browser that supports extensions. return self._browser = browser_to_create.Create() self._browser.Start() self._extension = self._browser.extensions[load_extension] self.assertTrue(self._extension)
def setUp(self): extension_path = os.path.join(os.path.dirname(__file__), '..', '..', 'unittest_data', 'simple_extension') load_extension = extension_to_load.ExtensionToLoad(extension_path) options = options_for_unittests.GetCopy() options.extensions_to_load = [load_extension] browser_to_create = browser_finder.FindBrowser(options) self._browser = None self._extension = None if not browser_to_create: # May not find a browser that supports extensions. return self._browser = browser_to_create.Create() self._extension = self._browser.extensions[load_extension] self.assertTrue(self._extension)
def testComponentExtensionBasic(self): extension_path = os.path.join(os.path.dirname(__file__), '..', '..', 'unittest_data', 'component_extension') options = options_for_unittests.GetCopy() load_extension = extension_to_load.ExtensionToLoad( extension_path, options.browser_type, is_component=True) options.extensions_to_load = [load_extension] browser_to_create = browser_finder.FindBrowser(options) if not browser_to_create: logging.warning('Did not find a browser that supports extensions, ' 'skipping test.') return with browser_to_create.Create() as b: extension = b.extensions[load_extension] extension.ExecuteJavaScript('setTestVar("abcdef")') self.assertEquals('abcdef', extension.EvaluateJavaScript('_testVar'))
def _CreateBrowser(self, autotest_ext=False, auto_login=True): """Finds and creates a browser for tests. if autotest_ext is True, also loads the autotest extension""" options = options_for_unittests.GetCopy() if autotest_ext: extension_path = os.path.join(os.path.dirname(__file__), 'autotest_ext') self._load_extension = extension_to_load.ExtensionToLoad( path=extension_path, browser_type=options.browser_type, is_component=True) options.extensions_to_load = [self._load_extension] browser_to_create = browser_finder.FindBrowser(options) self.assertTrue(browser_to_create) options.browser_options.create_browser_with_oobe = True options.browser_options.auto_login = auto_login b = browser_to_create.Create(options) b.Start() return b
def testComponentExtensionBasic(self): extension_path = os.path.join( util.GetUnittestDataDir(), 'component_extension') options = options_for_unittests.GetCopy() load_extension = extension_to_load.ExtensionToLoad( extension_path, options.browser_type, is_component=True) options.extensions_to_load = [load_extension] browser_to_create = browser_finder.FindBrowser(options) if not browser_to_create: logging.warning('Did not find a browser that supports extensions, ' 'skipping test.') return with browser_to_create.Create() as b: b.Start() extension = b.extensions[load_extension] self.assertTrue( extension.EvaluateJavaScript('chrome.runtime != null')) extension.ExecuteJavaScript('setTestVar("abcdef")') self.assertEquals('abcdef', extension.EvaluateJavaScript('_testVar'))
def setUp(self): """ Copy the manifest and background.js files of simple_extension to a number of temporary directories to load as extensions""" self._extension_dirs = [tempfile.mkdtemp() for i in range(3)] # pylint: disable=W0612 src_extension_dir = os.path.join( util.GetUnittestDataDir(), 'simple_extension') manifest_path = os.path.join(src_extension_dir, 'manifest.json') script_path = os.path.join(src_extension_dir, 'background.js') for d in self._extension_dirs: shutil.copy(manifest_path, d) shutil.copy(script_path, d) options = options_for_unittests.GetCopy() self._extensions_to_load = [extension_to_load.ExtensionToLoad( d, options.browser_type) for d in self._extension_dirs] options.extensions_to_load = self._extensions_to_load browser_to_create = browser_finder.FindBrowser(options) self._browser = None # May not find a browser that supports extensions. if browser_to_create: self._browser = browser_to_create.Create()