blob: be7efe204d7cf34af8a89473fe0f42cf777e710d [file] [log] [blame]
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging, os
from telemetry.core import browser_finder, browser_options, exceptions
from telemetry.core import extension_to_load, util
class Chrome(object):
"""Wrapper for creating a telemetry browser instance with extensions."""
BROWSER_TYPE_GUEST = 'system-guest'
def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
is_component=True, num_tries=3, extra_browser_args=None,
auto_login=True, gaia_login=False,
username=None, password=None):
Constructor of telemetry wrapper.
@param logged_in: Regular user (True) or guest user (False).
@param extension_paths: path of unpacked extension to install.
@param autotest_ext: Load a component extension with privileges to
invoke chrome.autotestPrivate.
@param is_component: Whether extensions should be loaded as component
@param num_tries: Number of attempts to log in.
@param extra_browser_args: Additional argument(s) to pass to the
browser. It can be a string or a list.
@param auto_login: Does not login automatically if this is False.
Useful if you need to examine oobe.
@param gaia_login: Logs in to real gaia.
@param username: Log in using this username instead of the default.
@param username: Log in using this password instead of the default.
self._autotest_ext_path = None
if autotest_ext:
self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
finder_options = browser_options.BrowserFinderOptions()
self._browser_type = (self.BROWSER_TYPE_LOGIN
if logged_in else self.BROWSER_TYPE_GUEST)
finder_options.browser_type = self.browser_type
if extra_browser_args:
if logged_in:
extensions_to_load = finder_options.extensions_to_load
for path in extension_paths:
extension = extension_to_load.ExtensionToLoad(
path, self.browser_type, is_component=is_component)
self._extensions_to_load = extensions_to_load
# finder options must be set before parse_args(), browser options must
# be set before Create().
finder_options.verbosity = 1 # info logging for telemetry.
b_options = finder_options.browser_options
b_options.disable_component_extensions_with_background_pages = False
b_options.create_browser_with_oobe = True
b_options.auto_login = auto_login
b_options.gaia_login = gaia_login
self.username = b_options.username if username is None else username
self.password = b_options.password if password is None else password
b_options.username = self.username
b_options.password = self.password
for i in range(num_tries):
browser_to_create = browser_finder.FindBrowser(finder_options)
self._browser = browser_to_create.Create()
except (util.TimeoutException, exceptions.LoginException) as e:
logging.error('Timed out logging in, tries=%d, error=%s',
i, repr(e))
if i == num_tries-1:
def __enter__(self):
return self
def __exit__(self, *args):
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
def autotest_ext(self):
"""Returns the autotest extension."""
return self.get_extension(self._autotest_ext_path)
def login_status(self):
"""Returns login status."""
ext = self.autotest_ext
if not ext:
return None
window.__login_status = null;
chrome.autotestPrivate.loginStatus(function(s) {
window.__login_status = s;
return ext.EvaluateJavaScript('window.__login_status')
def browser_type(self):
"""Returns the browser_type."""
return self._browser_type
def wait_for_browser_to_come_up(self):
"""Waits for the browser to come up. This should only be called after a
browser crash.
def _BrowserReady(cr):
tab = cr.browser.tabs.New()
except (exceptions.BrowserGoneException,
return False
except (util.TimeoutException):
logging.error('Timed out closing tab')
return True
util.WaitFor(lambda: _BrowserReady(self), timeout=10)
def did_browser_crash(self, func):
"""Runs func, returns True if the browser crashed, False otherwise.
@param func: function to run.
except (exceptions.BrowserGoneException,
return True
return False