Support for extensions in chrome.py
Create a Chrome wrapper object.
Add support for loading component extensions by path.
Add a test invoking a private api in telemetry_LoginTest.
BUG=None
TEST=manual
Change-Id: Ice05decb47aeff5bebd445b33306cfc614decd0c
Reviewed-on: https://gerrit.chromium.org/gerrit/62259
Reviewed-by: Simran Basi <sbasi@chromium.org>
Commit-Queue: Achuith Bhandarkar <achuith@chromium.org>
Tested-by: Achuith Bhandarkar <achuith@chromium.org>
diff --git a/client/common_lib/cros/chrome.py b/client/common_lib/cros/chrome.py
index c353ed0..c7a37af 100644
--- a/client/common_lib/cros/chrome.py
+++ b/client/common_lib/cros/chrome.py
@@ -2,43 +2,61 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-"""Telemetry-based Chrome automation functions."""
-
-from telemetry.core import browser_finder, browser_options
+from telemetry.core import browser_finder, browser_options, extension_to_load
-# The following constant is the name of the logged-in user that is specified
-# by the Telemetry chromeOS login extension
-# (chromium/src/tools/telemetry/telemetry/core/chrome/chromeos_login_ext).
-# The value here must match what is specified in that login extension.
+# Name of the logged-in user specified by the telemetry login extension.
LOGIN_USER = 'test@test.test'
-_BROWSER_TYPE_LOGIN = 'system'
-_BROWSER_TYPE_GUEST = 'system-guest'
-def _get_browser(browser_type):
- options = browser_options.BrowserOptions()
- options.browser_type = browser_type
- browser_to_create = browser_finder.FindBrowser(options)
- return browser_to_create.Create()
+class Chrome(object):
+ """Wrapper for creating a telemetry browser instance with extensions."""
-def logged_in_browser():
- """Returns a logged in browser.
-
- Wrapping this within a Python with/as construct will take care of
- automatically logging into Chrome at the start and logging out of Chrome
- at the end, e.g.:
-
- with chrome.logged_in_browser() as browser:
- do_test() # Will be logged in for this.
- # Logged out at this point.
-
- @return A Telemetry Browser object supporting context management.
- """
- return _get_browser(_BROWSER_TYPE_LOGIN)
+ BROWSER_TYPE_LOGIN = 'system'
+ BROWSER_TYPE_GUEST = 'system-guest'
-def incognito_browser():
- """Returns an incognito browser."""
- return _get_browser(_BROWSER_TYPE_GUEST)
+ def __init__(self, logged_in=True, extension_paths=[]):
+ options = browser_options.BrowserOptions()
+ self._browser_type = (self.BROWSER_TYPE_LOGIN
+ if logged_in else self.BROWSER_TYPE_GUEST)
+ options.browser_type = self._browser_type
+
+ if logged_in:
+ for path in extension_paths:
+ extension = extension_to_load.ExtensionToLoad(
+ path, self._browser_type, is_component=True)
+ options.extensions_to_load.append(extension)
+ self._extensions_to_load = options.extensions_to_load
+
+ browser_to_create = browser_finder.FindBrowser(options)
+ self._browser = browser_to_create.Create()
+
+
+ def __enter__(self):
+ return self
+
+
+ def __exit__(self, *args):
+ self.browser.Close()
+
+
+ @property
+ def browser(self):
+ """Returns a telemetry browser instance."""
+ return self._browser
+
+
+ def get_extension(self, extension_path):
+ """Fetches a telemetry extension instance given the extension path."""
+ for ext in self._extensions_to_load:
+ if extension_path == ext.path:
+ return self.browser.extensions[ext]
+ return None
+
+
+ @property
+ def browser_type(self):
+ """Returns the browser_type."""
+ return self._browser_type
diff --git a/client/site_tests/desktopui_FlashSanityCheck/desktopui_FlashSanityCheck.py b/client/site_tests/desktopui_FlashSanityCheck/desktopui_FlashSanityCheck.py
index fcbfb7b..e4e4646 100644
--- a/client/site_tests/desktopui_FlashSanityCheck/desktopui_FlashSanityCheck.py
+++ b/client/site_tests/desktopui_FlashSanityCheck/desktopui_FlashSanityCheck.py
@@ -59,5 +59,5 @@
def run_once(self, time_to_wait_secs=25):
- with chrome.logged_in_browser() as browser:
- self.run_flash_sanity_test(browser, time_to_wait_secs)
+ with chrome.Chrome() as cr:
+ self.run_flash_sanity_test(cr.browser, time_to_wait_secs)
diff --git a/client/site_tests/login_CryptohomeMountedTelemetry/login_CryptohomeMountedTelemetry.py b/client/site_tests/login_CryptohomeMountedTelemetry/login_CryptohomeMountedTelemetry.py
index 8840bfc..cee2279 100644
--- a/client/site_tests/login_CryptohomeMountedTelemetry/login_CryptohomeMountedTelemetry.py
+++ b/client/site_tests/login_CryptohomeMountedTelemetry/login_CryptohomeMountedTelemetry.py
@@ -20,7 +20,7 @@
def run_once(self):
"""Verifies cryptohome is mounted after login (uses Telemetry login)."""
try:
- with chrome.logged_in_browser():
+ with chrome.Chrome():
login.wait_for_cryptohome(chrome.LOGIN_USER)
cryptohome.remove_vault(chrome.LOGIN_USER)
@@ -29,7 +29,7 @@
open(test_file, 'w').close()
cryptohome.unmount_vault(TEST_USER)
- with chrome.logged_in_browser():
+ with chrome.Chrome():
login.wait_for_cryptohome(chrome.LOGIN_USER)
self.assert_(not os.path.exists(test_file))
# TODO(dennisjeffrey): Make this more fine-grained.
diff --git a/client/site_tests/telemetry_LoginTest/login_status_ext/background.js b/client/site_tests/telemetry_LoginTest/login_status_ext/background.js
new file mode 100644
index 0000000..a7c19b3
--- /dev/null
+++ b/client/site_tests/telemetry_LoginTest/login_status_ext/background.js
@@ -0,0 +1,4 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
diff --git a/client/site_tests/telemetry_LoginTest/login_status_ext/manifest.json b/client/site_tests/telemetry_LoginTest/login_status_ext/manifest.json
new file mode 100644
index 0000000..1eb454e
--- /dev/null
+++ b/client/site_tests/telemetry_LoginTest/login_status_ext/manifest.json
@@ -0,0 +1,13 @@
+{
+ "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDuUZGKCDbff6IRaxa4Pue7PPkxwPaNhGT3JEqppEsNWFjM80imEdqMbf3lrWqEfaHgaNku7nlpwPO1mu3/4Hr+XdNa5MhfnOnuPee4hyTLwOs3Vzz81wpbdzUxZSi2OmqMyI5oTaBYICfNHLwcuc65N5dbt6WKGeKgTpp4v7j7zwIDAQAB",
+ "description": "Telemetry ChromeOS Autotest component extension",
+ "name": "Telemetry ChromeOS AutoTest Component Extension",
+ "background": {
+ "scripts": ["background.js"]
+ },
+ "manifest_version": 2,
+ "version": "0.1",
+ "permissions" : [
+ "autotestPrivate"
+ ]
+}
diff --git a/client/site_tests/telemetry_LoginTest/telemetry_LoginTest.py b/client/site_tests/telemetry_LoginTest/telemetry_LoginTest.py
index ca2f257..e675d7d 100644
--- a/client/site_tests/telemetry_LoginTest/telemetry_LoginTest.py
+++ b/client/site_tests/telemetry_LoginTest/telemetry_LoginTest.py
@@ -19,8 +19,37 @@
the login flow and checks to ensure that the login process is
completed.
"""
- with chrome.logged_in_browser():
+ extension_path = os.path.join(os.path.dirname(__file__),
+ 'login_status_ext')
+ with chrome.Chrome(logged_in=True,
+ extension_paths=[extension_path]) as cr:
# By creating a browser and using 'with' any code in this section
# is wrapped by a login/logout.
if not os.path.exists('/var/run/state/logged-in'):
raise error.TestFail('Failed to log into the system.')
+
+ extension = cr.get_extension(extension_path)
+ if not extension:
+ raise error.TestFail('Failed to find loaded extension %s'
+ % extension_path)
+
+ # Ensure private api loginStatus can be called.
+ extension.ExecuteJavaScript('''
+ chrome.autotestPrivate.loginStatus(function(s) {
+ window.__login_status = s;
+ });
+ ''')
+ login_status = extension.EvaluateJavaScript(
+ 'window.__login_status')
+ if type(login_status) != dict:
+ raise error.TestFail('LoginStatus type mismatch %r'
+ % type(login_status))
+
+ if not login_status['isRegularUser']:
+ raise error.TestFail('isRegularUser should be True')
+ if login_status['isGuest']:
+ raise error.TestFail('isGuest should be False')
+ if login_status['email'] != chrome.LOGIN_USER:
+ raise error.TestFail('user email mismatch %s'
+ % login_status['email'])
+