blob: 3576bc9707d9ecf5c5c3962116fc928f6641b994 [file] [log] [blame]
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -08005import logging, os
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -07006
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -07007from telemetry.core import browser_finder, browser_options, exceptions
8from telemetry.core import extension_to_load, util
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -07009
10
Achuith Bhandarkared498932013-07-16 17:01:40 -070011class Chrome(object):
12 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070013
14
Achuith Bhandarkared498932013-07-16 17:01:40 -070015 BROWSER_TYPE_LOGIN = 'system'
16 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070017
18
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080019 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080020 is_component=True, num_tries=1, extra_browser_args=None,
21 auto_login=True, username=None, password=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080022 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080023 Constructor of telemetry wrapper.
24
25 @param logged_in: Regular user (True) or guest user (False).
26 @param extension_paths: path of unpacked extension to install.
27 @param autotest_ext: Load a component extension with privileges to
28 invoke chrome.autotestPrivate.
29 @param is_component: Whether extensions should be loaded as component
30 extensions.
31 @param num_tries: Number of attempts to log in. (Temporary for
32 debugging).
Dean Liaob12e2ee2013-11-19 16:49:12 +080033 @param extra_browser_args: Additional argument(s) to pass to the
34 browser. It can be a string or a list.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080035 @param auto_login: Does not login automatically if this is False.
36 Useful if you need to examine oobe.
37 @param username: Log in using this username instead of the default.
38 @param username: Log in using this password instead of the default.
Dean Liaob12e2ee2013-11-19 16:49:12 +080039 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070040 self._autotest_ext_path = None
41 if autotest_ext:
42 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
43 'autotest_private_ext')
44 extension_paths.append(self._autotest_ext_path)
45
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070046 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070047 self._browser_type = (self.BROWSER_TYPE_LOGIN
48 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070049 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080050 if extra_browser_args:
51 finder_options.browser_options.AppendExtraBrowserArgs(
52 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070053
Achuith Bhandarkared498932013-07-16 17:01:40 -070054 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070055 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070056 for path in extension_paths:
57 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080058 path, self.browser_type, is_component=is_component)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070059 extensions_to_load.append(extension)
60 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070061
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070062 # finder options must be set before parse_args(), browser options must
63 # be set before Create().
64 finder_options.verbosity = 1 # info logging for telemetry.
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020065 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070066 b_options = finder_options.browser_options
67 b_options.disable_component_extensions_with_background_pages = False
68 b_options.create_browser_with_oobe = True
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080069
70 b_options.auto_login = auto_login
71 self.username = b_options.username if username is None else username
72 self.password = b_options.password if password is None else password
73 b_options.username = self.username
74 b_options.password = self.password
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070075
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080076 for i in range(num_tries):
77 try:
78 browser_to_create = browser_finder.FindBrowser(finder_options)
79 self._browser = browser_to_create.Create()
80 self._browser.Start()
81 break
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -080082 except (util.TimeoutException, exceptions.LoginException) as e:
83 logging.error('Timed out logging in, tries=%d, error=%s',
84 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080085 if i == num_tries-1:
86 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -070087
88
89 def __enter__(self):
90 return self
91
92
93 def __exit__(self, *args):
94 self.browser.Close()
95
96
97 @property
98 def browser(self):
99 """Returns a telemetry browser instance."""
100 return self._browser
101
102
103 def get_extension(self, extension_path):
104 """Fetches a telemetry extension instance given the extension path."""
105 for ext in self._extensions_to_load:
106 if extension_path == ext.path:
107 return self.browser.extensions[ext]
108 return None
109
110
111 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700112 def autotest_ext(self):
113 """Returns the autotest extension."""
114 return self.get_extension(self._autotest_ext_path)
115
116
117 @property
118 def login_status(self):
119 """Returns login status."""
120 ext = self.autotest_ext
121 if not ext:
122 return None
123
124 ext.ExecuteJavaScript('''
125 window.__login_status = null;
126 chrome.autotestPrivate.loginStatus(function(s) {
127 window.__login_status = s;
128 });
129 ''')
130 return ext.EvaluateJavaScript('window.__login_status')
131
132
133 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700134 def browser_type(self):
135 """Returns the browser_type."""
136 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700137
138
139 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700140 """Waits for the browser to come up. This should only be called after a
141 browser crash.
142 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700143 def _BrowserReady(cr):
144 try:
145 tab = cr.browser.tabs.New()
146 except (exceptions.BrowserGoneException,
147 exceptions.BrowserConnectionGoneException):
148 return False
149 tab.Close()
150 return True
Chris Masone73efa2b2014-02-06 14:20:55 -0800151 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700152
153
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700154 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700155 """Runs func, returns True if the browser crashed, False otherwise.
156
157 @param func: function to run.
158
159 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700160 try:
161 func()
162 except (exceptions.BrowserGoneException,
163 exceptions.BrowserConnectionGoneException):
164 return True
165 return False