blob: b83f56508d829d6a8634115c81ce6b13157ae67d [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
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 Bhandarkara2bceaf2014-11-14 12:10:16 -080022 disable_gaia_services=True, disable_default_apps = True,
23 auto_login=True, gaia_login=False,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070024 username=None, password=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080025 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080026 Constructor of telemetry wrapper.
27
28 @param logged_in: Regular user (True) or guest user (False).
29 @param extension_paths: path of unpacked extension to install.
30 @param autotest_ext: Load a component extension with privileges to
31 invoke chrome.autotestPrivate.
32 @param is_component: Whether extensions should be loaded as component
33 extensions.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070034 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080035 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070036 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070037 @param clear_enterprise_policy: Clear enterprise policy before
38 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070039 @param dont_override_profile: Don't delete cryptohome before login.
40 Telemetry will output a warning with this
41 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070042 @param disable_gaia_services: For enterprise autotests, this option may
43 be used to enable policy fetch.
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080044 @param disable_default_apps: For tests that exercise default apps.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080045 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070046 Useful if you need to examine oobe.
47 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080048 @param username: Log in using this username instead of the default.
49 @param username: Log in using this password instead of the default.
Dean Liaob12e2ee2013-11-19 16:49:12 +080050 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070051 self._autotest_ext_path = None
52 if autotest_ext:
53 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
54 'autotest_private_ext')
55 extension_paths.append(self._autotest_ext_path)
56
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070057 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070058 self._browser_type = (self.BROWSER_TYPE_LOGIN
59 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070060 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080061 if extra_browser_args:
62 finder_options.browser_options.AppendExtraBrowserArgs(
63 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070064
Achuith Bhandarkared498932013-07-16 17:01:40 -070065 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070066 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070067 for path in extension_paths:
68 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080069 path, self.browser_type, is_component=is_component)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070070 extensions_to_load.append(extension)
71 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070072
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070073 # finder options must be set before parse_args(), browser options must
74 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -070075 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
76 # autotest debug logs
77 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020078 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070079 b_options = finder_options.browser_options
80 b_options.disable_component_extensions_with_background_pages = False
81 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070082 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070083 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070084 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080085 b_options.disable_default_apps = disable_default_apps
86 b_options.disable_component_extensions_with_background_pages = disable_default_apps
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080087
88 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070089 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080090 self.username = b_options.username if username is None else username
91 self.password = b_options.password if password is None else password
92 b_options.username = self.username
93 b_options.password = self.password
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070094
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080095 for i in range(num_tries):
96 try:
97 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080098 self._browser = browser_to_create.Create(finder_options)
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080099 break
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800100 except (util.TimeoutException, exceptions.LoginException) as e:
101 logging.error('Timed out logging in, tries=%d, error=%s',
102 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800103 if i == num_tries-1:
104 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700105
106
107 def __enter__(self):
108 return self
109
110
111 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700112 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700113
114
115 @property
116 def browser(self):
117 """Returns a telemetry browser instance."""
118 return self._browser
119
120
121 def get_extension(self, extension_path):
122 """Fetches a telemetry extension instance given the extension path."""
123 for ext in self._extensions_to_load:
124 if extension_path == ext.path:
125 return self.browser.extensions[ext]
126 return None
127
128
129 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700130 def autotest_ext(self):
131 """Returns the autotest extension."""
132 return self.get_extension(self._autotest_ext_path)
133
134
135 @property
136 def login_status(self):
137 """Returns login status."""
138 ext = self.autotest_ext
139 if not ext:
140 return None
141
142 ext.ExecuteJavaScript('''
143 window.__login_status = null;
144 chrome.autotestPrivate.loginStatus(function(s) {
145 window.__login_status = s;
146 });
147 ''')
148 return ext.EvaluateJavaScript('window.__login_status')
149
150
151 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700152 def browser_type(self):
153 """Returns the browser_type."""
154 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700155
156
157 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700158 """Waits for the browser to come up. This should only be called after a
159 browser crash.
160 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700161 def _BrowserReady(cr):
162 try:
163 tab = cr.browser.tabs.New()
164 except (exceptions.BrowserGoneException,
165 exceptions.BrowserConnectionGoneException):
166 return False
Achuith Bhandarkar0998e182014-03-17 16:44:21 -0700167 try:
168 tab.Close()
169 except (util.TimeoutException):
170 # crbug.com/350941
171 logging.error('Timed out closing tab')
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700172 return True
Chris Masone73efa2b2014-02-06 14:20:55 -0800173 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700174
175
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700176 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700177 """Runs func, returns True if the browser crashed, False otherwise.
178
179 @param func: function to run.
180
181 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700182 try:
183 func()
184 except (exceptions.BrowserGoneException,
185 exceptions.BrowserConnectionGoneException):
186 return True
187 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700188
189
190 def close(self):
191 """Closes the browser."""
192 self._browser.Close()