blob: 24343fb4f18de814f5de16ca96b5f3ddbabcc286 [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 Bhandarkara2bceaf2014-11-14 12:10:16 -08005import logging, os
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -07006
Don Garrett11b13972015-09-11 17:37:53 +00007from autotest_lib.client.cros import constants
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -07008from autotest_lib.client.bin import utils
Prathmesh Prabhuc3961df2015-07-21 10:44:54 -07009from telemetry.core import cros_interface, exceptions, util
Achuith Bhandarkarb058a8f2015-07-02 01:29:05 -070010from telemetry.internal.browser import browser_finder, browser_options
11from telemetry.internal.browser import extension_to_load
Achuith Bhandarkar704134d2015-03-05 17:31:57 -080012
13Error = exceptions.Error
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070014
15
Achuith Bhandarkared498932013-07-16 17:01:40 -070016class Chrome(object):
17 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070018
19
Chung-yih Wang8988eb22016-03-16 16:16:10 +080020 CHEETS = '-cheets'
Achuith Bhandarkared498932013-07-16 17:01:40 -070021 BROWSER_TYPE_LOGIN = 'system'
22 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070023
24
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080025 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070026 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070027 clear_enterprise_policy=True, dont_override_profile=False,
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080028 disable_gaia_services=True, disable_default_apps = True,
29 auto_login=True, gaia_login=False,
Alexander Alekseevd9682862015-10-01 22:42:49 -070030 username=None, password=None, gaia_id=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080031 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080032 Constructor of telemetry wrapper.
33
34 @param logged_in: Regular user (True) or guest user (False).
35 @param extension_paths: path of unpacked extension to install.
36 @param autotest_ext: Load a component extension with privileges to
37 invoke chrome.autotestPrivate.
38 @param is_component: Whether extensions should be loaded as component
39 extensions.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070040 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080041 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070042 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070043 @param clear_enterprise_policy: Clear enterprise policy before
44 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070045 @param dont_override_profile: Don't delete cryptohome before login.
46 Telemetry will output a warning with this
47 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070048 @param disable_gaia_services: For enterprise autotests, this option may
49 be used to enable policy fetch.
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080050 @param disable_default_apps: For tests that exercise default apps.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080051 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070052 Useful if you need to examine oobe.
53 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080054 @param username: Log in using this username instead of the default.
Alexander Alekseevd9682862015-10-01 22:42:49 -070055 @param password: Log in using this password instead of the default.
56 @param gaia_id: Log in using this gaia_id instead of the default.
Dean Liaob12e2ee2013-11-19 16:49:12 +080057 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070058 self._autotest_ext_path = None
59 if autotest_ext:
60 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
61 'autotest_private_ext')
62 extension_paths.append(self._autotest_ext_path)
63
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070064 finder_options = browser_options.BrowserFinderOptions()
Chung-yih Wang8988eb22016-03-16 16:16:10 +080065 # Append cheets specific browser args
Hidehiko Abe202ed322016-04-14 15:08:43 +090066 self._is_cheets_platform = (
67 utils.get_current_board().endswith(self.CHEETS))
68 if self._is_cheets_platform:
Chung-yih Wang8988eb22016-03-16 16:16:10 +080069 from autotest_lib.client.common_lib.cros import cheets
70 extra_browser_args = cheets.append_extra_args(extra_browser_args)
71 logged_in = True
Achuith Bhandarkared498932013-07-16 17:01:40 -070072 self._browser_type = (self.BROWSER_TYPE_LOGIN
73 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070074 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080075 if extra_browser_args:
76 finder_options.browser_options.AppendExtraBrowserArgs(
77 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070078
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -070079 # TODO(achuith): Remove this after PFQ revs. crbug.com/603169.
Achuith Bhandarkared498932013-07-16 17:01:40 -070080 if logged_in:
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -070081 try:
82 extensions_to_load = finder_options.extensions_to_load
83 for path in extension_paths:
84 extension = extension_to_load.ExtensionToLoad(
85 path, self.browser_type, is_component=is_component)
86 extensions_to_load.append(extension)
87 self._extensions_to_load = extensions_to_load
88 except AttributeError:
89 pass
Achuith Bhandarkared498932013-07-16 17:01:40 -070090
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070091 # finder options must be set before parse_args(), browser options must
92 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -070093 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
94 # autotest debug logs
95 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020096 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070097 b_options = finder_options.browser_options
98 b_options.disable_component_extensions_with_background_pages = False
99 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -0700100 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -0700101 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -0700102 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800103 b_options.disable_default_apps = disable_default_apps
104 b_options.disable_component_extensions_with_background_pages = disable_default_apps
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800105
106 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -0700107 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800108 self.username = b_options.username if username is None else username
109 self.password = b_options.password if password is None else password
110 b_options.username = self.username
111 b_options.password = self.password
Alexander Alekseevd9682862015-10-01 22:42:49 -0700112 # gaia_id will be added to telemetry code in chromium repository later
113 try:
114 self.gaia_id = b_options.gaia_id if gaia_id is None else gaia_id
115 b_options.gaia_id = self.gaia_id
116 except AttributeError:
117 pass
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700118
Achuith Bhandarkar531a62a2016-04-13 11:09:43 -0700119 if logged_in:
120 try:
121 extensions_to_load = b_options.extensions_to_load
122 for path in extension_paths:
123 extension = extension_to_load.ExtensionToLoad(
124 path, self.browser_type, is_component=is_component)
125 extensions_to_load.append(extension)
126 self._extensions_to_load = extensions_to_load
127 except AttributeError:
128 pass
129
David James54a830e2015-05-06 17:21:47 -0700130 # Turn on collection of Chrome coredumps via creation of a magic file.
131 # (Without this, Chrome coredumps are trashed.)
Don Garrett11b13972015-09-11 17:37:53 +0000132 open(constants.CHROME_CORE_MAGIC_FILE, 'w').close()
David James54a830e2015-05-06 17:21:47 -0700133
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800134 for i in range(num_tries):
135 try:
136 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800137 self._browser = browser_to_create.Create(finder_options)
Hidehiko Abe202ed322016-04-14 15:08:43 +0900138 if self._is_cheets_platform:
Chung-yih Wang8988eb22016-03-16 16:16:10 +0800139 cheets.post_processing_after_browser()
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800140 break
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800141 except (exceptions.LoginException) as e:
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800142 logging.error('Timed out logging in, tries=%d, error=%s',
143 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800144 if i == num_tries-1:
145 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700146
147
148 def __enter__(self):
149 return self
150
151
152 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700153 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700154
155
156 @property
157 def browser(self):
158 """Returns a telemetry browser instance."""
159 return self._browser
160
161
162 def get_extension(self, extension_path):
163 """Fetches a telemetry extension instance given the extension path."""
164 for ext in self._extensions_to_load:
165 if extension_path == ext.path:
166 return self.browser.extensions[ext]
167 return None
168
169
170 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700171 def autotest_ext(self):
172 """Returns the autotest extension."""
173 return self.get_extension(self._autotest_ext_path)
174
175
176 @property
177 def login_status(self):
178 """Returns login status."""
179 ext = self.autotest_ext
180 if not ext:
181 return None
182
183 ext.ExecuteJavaScript('''
184 window.__login_status = null;
185 chrome.autotestPrivate.loginStatus(function(s) {
186 window.__login_status = s;
187 });
188 ''')
189 return ext.EvaluateJavaScript('window.__login_status')
190
191
Victor Hsiehfc3417e2016-03-25 16:49:59 -0700192 def get_visible_notifications(self):
193 """Returns an array of visible notifications of Chrome.
194
195 For specific type of each notification, please refer to Chromium's
196 chrome/common/extensions/api/autotest_private.idl.
197 """
198 ext = self.autotest_ext
199 if not ext:
200 return None
201
202 ext.ExecuteJavaScript('''
203 window.__items = null;
204 chrome.autotestPrivate.getVisibleNotifications(function(items) {
205 window.__items = items;
206 });
207 ''')
208 if ext.EvaluateJavaScript('window.__items') is None:
209 return None
210 return ext.EvaluateJavaScript('window.__items')
211
212
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700213 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700214 def browser_type(self):
215 """Returns the browser_type."""
216 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700217
218
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800219 @staticmethod
220 def did_browser_crash(func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700221 """Runs func, returns True if the browser crashed, False otherwise.
222
223 @param func: function to run.
224
225 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700226 try:
227 func()
Achuith Bhandarkar704134d2015-03-05 17:31:57 -0800228 except (Error):
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700229 return True
230 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700231
232
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -0700233 @staticmethod
234 def wait_for_browser_restart(func):
235 """Runs func, and waits for a browser restart.
236
237 @param func: function to run.
238
239 """
240 _cri = cros_interface.CrOSInterface()
241 pid = _cri.GetChromePid()
242 Chrome.did_browser_crash(func)
243 utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60)
244
245
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800246 def wait_for_browser_to_come_up(self):
247 """Waits for the browser to come up. This should only be called after a
248 browser crash.
249 """
250 def _BrowserReady(cr):
251 tabs = [] # Wrapper for pass by reference.
252 if self.did_browser_crash(
253 lambda: tabs.append(cr.browser.tabs.New())):
254 return False
255 try:
256 tabs[0].Close()
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800257 except:
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800258 # crbug.com/350941
259 logging.error('Timed out closing tab')
260 return True
261 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
262
263
Derek Basehore41acbf82014-07-11 18:34:30 -0700264 def close(self):
265 """Closes the browser."""
Hidehiko Abe202ed322016-04-14 15:08:43 +0900266 try:
267 if self._is_cheets_platform:
268 from autotest_lib.client.common_lib.cros import cheets
269 cheets.pre_processing_before_close()
270 finally:
271 self._browser.Close()