blob: a688f2d34fa55c5388595ac0ff1e6137c3c3aaf7 [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
David James54a830e2015-05-06 17:21:47 -07007from autotest_lib.client.cros import constants
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -07008from autotest_lib.client.bin import utils
Achuith Bhandarkaraa850d12015-06-30 20:48:55 -07009from telemetry.core import exceptions, util
Prathmesh Prabhub0220622015-07-21 10:40:02 -070010# Need to support both paths for the transition (crbug.com/512427).
11try:
12 from telemetry.core.platform import cros_interface
13except ImportError:
14 from telemetry.core import cros_interface
15
Achuith Bhandarkarb058a8f2015-07-02 01:29:05 -070016from telemetry.internal.browser import browser_finder, browser_options
17from telemetry.internal.browser import extension_to_load
Achuith Bhandarkar704134d2015-03-05 17:31:57 -080018
19Error = exceptions.Error
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070020
21
Achuith Bhandarkared498932013-07-16 17:01:40 -070022class Chrome(object):
23 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070024
25
Achuith Bhandarkared498932013-07-16 17:01:40 -070026 BROWSER_TYPE_LOGIN = 'system'
27 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070028
29
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080030 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070031 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070032 clear_enterprise_policy=True, dont_override_profile=False,
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080033 disable_gaia_services=True, disable_default_apps = True,
34 auto_login=True, gaia_login=False,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070035 username=None, password=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080036 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080037 Constructor of telemetry wrapper.
38
39 @param logged_in: Regular user (True) or guest user (False).
40 @param extension_paths: path of unpacked extension to install.
41 @param autotest_ext: Load a component extension with privileges to
42 invoke chrome.autotestPrivate.
43 @param is_component: Whether extensions should be loaded as component
44 extensions.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070045 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080046 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070047 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070048 @param clear_enterprise_policy: Clear enterprise policy before
49 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070050 @param dont_override_profile: Don't delete cryptohome before login.
51 Telemetry will output a warning with this
52 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070053 @param disable_gaia_services: For enterprise autotests, this option may
54 be used to enable policy fetch.
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080055 @param disable_default_apps: For tests that exercise default apps.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080056 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070057 Useful if you need to examine oobe.
58 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080059 @param username: Log in using this username instead of the default.
60 @param username: Log in using this password instead of the default.
Dean Liaob12e2ee2013-11-19 16:49:12 +080061 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070062 self._autotest_ext_path = None
63 if autotest_ext:
64 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
65 'autotest_private_ext')
66 extension_paths.append(self._autotest_ext_path)
67
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070068 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070069 self._browser_type = (self.BROWSER_TYPE_LOGIN
70 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070071 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080072 if extra_browser_args:
73 finder_options.browser_options.AppendExtraBrowserArgs(
74 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070075
Achuith Bhandarkared498932013-07-16 17:01:40 -070076 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070077 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070078 for path in extension_paths:
79 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080080 path, self.browser_type, is_component=is_component)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070081 extensions_to_load.append(extension)
82 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070083
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070084 # finder options must be set before parse_args(), browser options must
85 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -070086 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
87 # autotest debug logs
88 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020089 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070090 b_options = finder_options.browser_options
91 b_options.disable_component_extensions_with_background_pages = False
92 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070093 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070094 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070095 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080096 b_options.disable_default_apps = disable_default_apps
97 b_options.disable_component_extensions_with_background_pages = disable_default_apps
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080098
99 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -0700100 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -0800101 self.username = b_options.username if username is None else username
102 self.password = b_options.password if password is None else password
103 b_options.username = self.username
104 b_options.password = self.password
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700105
David James54a830e2015-05-06 17:21:47 -0700106 # Turn on collection of Chrome coredumps via creation of a magic file.
107 # (Without this, Chrome coredumps are trashed.)
108 open(constants.CHROME_CORE_MAGIC_FILE, 'w').close()
109
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800110 for i in range(num_tries):
111 try:
112 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800113 self._browser = browser_to_create.Create(finder_options)
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800114 break
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800115 except (exceptions.LoginException) as e:
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800116 logging.error('Timed out logging in, tries=%d, error=%s',
117 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800118 if i == num_tries-1:
119 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700120
121
122 def __enter__(self):
123 return self
124
125
126 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700127 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700128
129
130 @property
131 def browser(self):
132 """Returns a telemetry browser instance."""
133 return self._browser
134
135
136 def get_extension(self, extension_path):
137 """Fetches a telemetry extension instance given the extension path."""
138 for ext in self._extensions_to_load:
139 if extension_path == ext.path:
140 return self.browser.extensions[ext]
141 return None
142
143
144 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700145 def autotest_ext(self):
146 """Returns the autotest extension."""
147 return self.get_extension(self._autotest_ext_path)
148
149
150 @property
151 def login_status(self):
152 """Returns login status."""
153 ext = self.autotest_ext
154 if not ext:
155 return None
156
157 ext.ExecuteJavaScript('''
158 window.__login_status = null;
159 chrome.autotestPrivate.loginStatus(function(s) {
160 window.__login_status = s;
161 });
162 ''')
163 return ext.EvaluateJavaScript('window.__login_status')
164
165
166 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700167 def browser_type(self):
168 """Returns the browser_type."""
169 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700170
171
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800172 @staticmethod
173 def did_browser_crash(func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700174 """Runs func, returns True if the browser crashed, False otherwise.
175
176 @param func: function to run.
177
178 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700179 try:
180 func()
Achuith Bhandarkar704134d2015-03-05 17:31:57 -0800181 except (Error):
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700182 return True
183 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700184
185
Achuith Bhandarkar88a2bab2015-07-09 17:36:25 -0700186 @staticmethod
187 def wait_for_browser_restart(func):
188 """Runs func, and waits for a browser restart.
189
190 @param func: function to run.
191
192 """
193 _cri = cros_interface.CrOSInterface()
194 pid = _cri.GetChromePid()
195 Chrome.did_browser_crash(func)
196 utils.poll_for_condition(lambda: pid != _cri.GetChromePid(), timeout=60)
197
198
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800199 def wait_for_browser_to_come_up(self):
200 """Waits for the browser to come up. This should only be called after a
201 browser crash.
202 """
203 def _BrowserReady(cr):
204 tabs = [] # Wrapper for pass by reference.
205 if self.did_browser_crash(
206 lambda: tabs.append(cr.browser.tabs.New())):
207 return False
208 try:
209 tabs[0].Close()
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800210 except:
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800211 # crbug.com/350941
212 logging.error('Timed out closing tab')
213 return True
214 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
215
216
Derek Basehore41acbf82014-07-11 18:34:30 -0700217 def close(self):
218 """Closes the browser."""
219 self._browser.Close()