blob: 9a5310e7ac00fed84bc645e25948282e3264c46f [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 Bhandarkaref243fe2014-10-31 14:06:36 -07005import inspect, 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 Bhandarkarb7ef5e52014-03-20 14:18:45 -070020 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070021 clear_enterprise_policy=True, dont_override_profile=False,
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070022 disable_gaia_services=True, auto_login=True, gaia_login=False,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070023 username=None, password=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080024 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080025 Constructor of telemetry wrapper.
26
27 @param logged_in: Regular user (True) or guest user (False).
28 @param extension_paths: path of unpacked extension to install.
29 @param autotest_ext: Load a component extension with privileges to
30 invoke chrome.autotestPrivate.
31 @param is_component: Whether extensions should be loaded as component
32 extensions.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070033 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080034 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070035 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070036 @param clear_enterprise_policy: Clear enterprise policy before
37 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070038 @param dont_override_profile: Don't delete cryptohome before login.
39 Telemetry will output a warning with this
40 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070041 @param disable_gaia_services: For enterprise autotests, this option may
42 be used to enable policy fetch.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080043 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070044 Useful if you need to examine oobe.
45 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080046 @param username: Log in using this username instead of the default.
47 @param username: Log in using this password instead of the default.
Dean Liaob12e2ee2013-11-19 16:49:12 +080048 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070049 self._autotest_ext_path = None
50 if autotest_ext:
51 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
52 'autotest_private_ext')
53 extension_paths.append(self._autotest_ext_path)
54
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070055 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070056 self._browser_type = (self.BROWSER_TYPE_LOGIN
57 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070058 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080059 if extra_browser_args:
60 finder_options.browser_options.AppendExtraBrowserArgs(
61 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070062
Achuith Bhandarkared498932013-07-16 17:01:40 -070063 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070064 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070065 for path in extension_paths:
66 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080067 path, self.browser_type, is_component=is_component)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070068 extensions_to_load.append(extension)
69 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070070
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070071 # finder options must be set before parse_args(), browser options must
72 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -070073 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
74 # autotest debug logs
75 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020076 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070077 b_options = finder_options.browser_options
78 b_options.disable_component_extensions_with_background_pages = False
79 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070080 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070081 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070082 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkar410d1f62014-10-29 18:31:57 -070083 b_options.disable_default_apps = False
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080084
85 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070086 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080087 self.username = b_options.username if username is None else username
88 self.password = b_options.password if password is None else password
89 b_options.username = self.username
90 b_options.password = self.password
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070091
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080092 for i in range(num_tries):
93 try:
94 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkaref243fe2014-10-31 14:06:36 -070095 # TODO(achuith): Remove inspect and old Create call.
96 # crbug.com/428967.
97 if len(inspect.getargspec(browser_to_create.Create).args) == 1:
98 self._browser = browser_to_create.Create()
99 else:
100 self._browser = browser_to_create.Create(finder_options)
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800101 break
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800102 except (util.TimeoutException, exceptions.LoginException) as e:
103 logging.error('Timed out logging in, tries=%d, error=%s',
104 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800105 if i == num_tries-1:
106 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700107
108
109 def __enter__(self):
110 return self
111
112
113 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700114 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700115
116
117 @property
118 def browser(self):
119 """Returns a telemetry browser instance."""
120 return self._browser
121
122
123 def get_extension(self, extension_path):
124 """Fetches a telemetry extension instance given the extension path."""
125 for ext in self._extensions_to_load:
126 if extension_path == ext.path:
127 return self.browser.extensions[ext]
128 return None
129
130
131 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700132 def autotest_ext(self):
133 """Returns the autotest extension."""
134 return self.get_extension(self._autotest_ext_path)
135
136
137 @property
138 def login_status(self):
139 """Returns login status."""
140 ext = self.autotest_ext
141 if not ext:
142 return None
143
144 ext.ExecuteJavaScript('''
145 window.__login_status = null;
146 chrome.autotestPrivate.loginStatus(function(s) {
147 window.__login_status = s;
148 });
149 ''')
150 return ext.EvaluateJavaScript('window.__login_status')
151
152
153 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700154 def browser_type(self):
155 """Returns the browser_type."""
156 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700157
158
159 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700160 """Waits for the browser to come up. This should only be called after a
161 browser crash.
162 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700163 def _BrowserReady(cr):
164 try:
165 tab = cr.browser.tabs.New()
166 except (exceptions.BrowserGoneException,
167 exceptions.BrowserConnectionGoneException):
168 return False
Achuith Bhandarkar0998e182014-03-17 16:44:21 -0700169 try:
170 tab.Close()
171 except (util.TimeoutException):
172 # crbug.com/350941
173 logging.error('Timed out closing tab')
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700174 return True
Chris Masone73efa2b2014-02-06 14:20:55 -0800175 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700176
177
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700178 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700179 """Runs func, returns True if the browser crashed, False otherwise.
180
181 @param func: function to run.
182
183 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700184 try:
185 func()
186 except (exceptions.BrowserGoneException,
187 exceptions.BrowserConnectionGoneException):
188 return True
189 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700190
191
192 def close(self):
193 """Closes the browser."""
194 self._browser.Close()