blob: cfcb9db70796d2c764e943623388f1cf32c776d9 [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
Achuith Bhandarkar704134d2015-03-05 17:31:57 -08009
10
11Error = exceptions.Error
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070012
13
Achuith Bhandarkared498932013-07-16 17:01:40 -070014class Chrome(object):
15 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070016
17
Achuith Bhandarkared498932013-07-16 17:01:40 -070018 BROWSER_TYPE_LOGIN = 'system'
19 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070020
21
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080022 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070023 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070024 clear_enterprise_policy=True, dont_override_profile=False,
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080025 disable_gaia_services=True, disable_default_apps = True,
26 auto_login=True, gaia_login=False,
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070027 username=None, password=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080028 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080029 Constructor of telemetry wrapper.
30
31 @param logged_in: Regular user (True) or guest user (False).
32 @param extension_paths: path of unpacked extension to install.
33 @param autotest_ext: Load a component extension with privileges to
34 invoke chrome.autotestPrivate.
35 @param is_component: Whether extensions should be loaded as component
36 extensions.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070037 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080038 @param extra_browser_args: Additional argument(s) to pass to the
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070039 browser. It can be a string or a list.
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070040 @param clear_enterprise_policy: Clear enterprise policy before
41 logging in.
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070042 @param dont_override_profile: Don't delete cryptohome before login.
43 Telemetry will output a warning with this
44 option.
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070045 @param disable_gaia_services: For enterprise autotests, this option may
46 be used to enable policy fetch.
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080047 @param disable_default_apps: For tests that exercise default apps.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080048 @param auto_login: Does not login automatically if this is False.
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070049 Useful if you need to examine oobe.
50 @param gaia_login: Logs in to real gaia.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080051 @param username: Log in using this username instead of the default.
52 @param username: Log in using this password instead of the default.
Dean Liaob12e2ee2013-11-19 16:49:12 +080053 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070054 self._autotest_ext_path = None
55 if autotest_ext:
56 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
57 'autotest_private_ext')
58 extension_paths.append(self._autotest_ext_path)
59
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070060 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070061 self._browser_type = (self.BROWSER_TYPE_LOGIN
62 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070063 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080064 if extra_browser_args:
65 finder_options.browser_options.AppendExtraBrowserArgs(
66 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070067
Achuith Bhandarkared498932013-07-16 17:01:40 -070068 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070069 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070070 for path in extension_paths:
71 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080072 path, self.browser_type, is_component=is_component)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070073 extensions_to_load.append(extension)
74 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070075
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070076 # finder options must be set before parse_args(), browser options must
77 # be set before Create().
Todd Brocheb6f4812014-04-29 16:04:28 -070078 # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit
79 # autotest debug logs
80 finder_options.verbosity = 2
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020081 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070082 b_options = finder_options.browser_options
83 b_options.disable_component_extensions_with_background_pages = False
84 b_options.create_browser_with_oobe = True
Achuith Bhandarkar5d6c26c2014-04-25 11:19:38 -070085 b_options.clear_enterprise_policy = clear_enterprise_policy
Achuith Bhandarkar882a8ab2014-08-26 15:51:20 -070086 b_options.dont_override_profile = dont_override_profile
Achuith Bhandarkar3c654f32014-09-29 06:33:35 -070087 b_options.disable_gaia_services = disable_gaia_services
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -080088 b_options.disable_default_apps = disable_default_apps
89 b_options.disable_component_extensions_with_background_pages = disable_default_apps
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080090
91 b_options.auto_login = auto_login
Achuith Bhandarkar3875e0c2014-03-20 14:17:31 -070092 b_options.gaia_login = gaia_login
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080093 self.username = b_options.username if username is None else username
94 self.password = b_options.password if password is None else password
95 b_options.username = self.username
96 b_options.password = self.password
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070097
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080098 for i in range(num_tries):
99 try:
100 browser_to_create = browser_finder.FindBrowser(finder_options)
Achuith Bhandarkara2bceaf2014-11-14 12:10:16 -0800101 self._browser = browser_to_create.Create(finder_options)
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800102 break
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800103 except (exceptions.LoginException) as e:
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -0800104 logging.error('Timed out logging in, tries=%d, error=%s',
105 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -0800106 if i == num_tries-1:
107 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -0700108
109
110 def __enter__(self):
111 return self
112
113
114 def __exit__(self, *args):
Derek Basehore41acbf82014-07-11 18:34:30 -0700115 self.close()
Achuith Bhandarkared498932013-07-16 17:01:40 -0700116
117
118 @property
119 def browser(self):
120 """Returns a telemetry browser instance."""
121 return self._browser
122
123
124 def get_extension(self, extension_path):
125 """Fetches a telemetry extension instance given the extension path."""
126 for ext in self._extensions_to_load:
127 if extension_path == ext.path:
128 return self.browser.extensions[ext]
129 return None
130
131
132 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700133 def autotest_ext(self):
134 """Returns the autotest extension."""
135 return self.get_extension(self._autotest_ext_path)
136
137
138 @property
139 def login_status(self):
140 """Returns login status."""
141 ext = self.autotest_ext
142 if not ext:
143 return None
144
145 ext.ExecuteJavaScript('''
146 window.__login_status = null;
147 chrome.autotestPrivate.loginStatus(function(s) {
148 window.__login_status = s;
149 });
150 ''')
151 return ext.EvaluateJavaScript('window.__login_status')
152
153
154 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700155 def browser_type(self):
156 """Returns the browser_type."""
157 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700158
159
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800160 @staticmethod
161 def did_browser_crash(func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700162 """Runs func, returns True if the browser crashed, False otherwise.
163
164 @param func: function to run.
165
166 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700167 try:
168 func()
Achuith Bhandarkar704134d2015-03-05 17:31:57 -0800169 except (Error):
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700170 return True
171 return False
Derek Basehore41acbf82014-07-11 18:34:30 -0700172
173
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800174 def wait_for_browser_to_come_up(self):
175 """Waits for the browser to come up. This should only be called after a
176 browser crash.
177 """
178 def _BrowserReady(cr):
179 tabs = [] # Wrapper for pass by reference.
180 if self.did_browser_crash(
181 lambda: tabs.append(cr.browser.tabs.New())):
182 return False
183 try:
184 tabs[0].Close()
Achuith Bhandarkarfab82d12015-02-26 11:05:35 -0800185 except:
Achuith Bhandarkar16ee9772015-01-23 17:18:18 -0800186 # crbug.com/350941
187 logging.error('Timed out closing tab')
188 return True
189 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
190
191
Derek Basehore41acbf82014-07-11 18:34:30 -0700192 def close(self):
193 """Closes the browser."""
194 self._browser.Close()