Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 1 | # 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 Bhandarkar | a2bceaf | 2014-11-14 12:10:16 -0800 | [diff] [blame] | 5 | import logging, os |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 6 | |
David James | 54a830e | 2015-05-06 17:21:47 -0700 | [diff] [blame^] | 7 | from autotest_lib.client.cros import constants |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 8 | from telemetry.core import browser_finder, browser_options, exceptions |
| 9 | from telemetry.core import extension_to_load, util |
Achuith Bhandarkar | 704134d | 2015-03-05 17:31:57 -0800 | [diff] [blame] | 10 | |
| 11 | |
| 12 | Error = exceptions.Error |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 13 | |
| 14 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 15 | class Chrome(object): |
| 16 | """Wrapper for creating a telemetry browser instance with extensions.""" |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 17 | |
| 18 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 19 | BROWSER_TYPE_LOGIN = 'system' |
| 20 | BROWSER_TYPE_GUEST = 'system-guest' |
Achuith Bhandarkar | 86c4681 | 2013-07-15 17:13:39 -0700 | [diff] [blame] | 21 | |
| 22 | |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 23 | def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False, |
Achuith Bhandarkar | b7ef5e5 | 2014-03-20 14:18:45 -0700 | [diff] [blame] | 24 | is_component=True, num_tries=3, extra_browser_args=None, |
Achuith Bhandarkar | 882a8ab | 2014-08-26 15:51:20 -0700 | [diff] [blame] | 25 | clear_enterprise_policy=True, dont_override_profile=False, |
Achuith Bhandarkar | a2bceaf | 2014-11-14 12:10:16 -0800 | [diff] [blame] | 26 | disable_gaia_services=True, disable_default_apps = True, |
| 27 | auto_login=True, gaia_login=False, |
Achuith Bhandarkar | 882a8ab | 2014-08-26 15:51:20 -0700 | [diff] [blame] | 28 | username=None, password=None): |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 29 | """ |
Achuith Bhandarkar | 944405a | 2013-11-21 14:47:48 -0800 | [diff] [blame] | 30 | Constructor of telemetry wrapper. |
| 31 | |
| 32 | @param logged_in: Regular user (True) or guest user (False). |
| 33 | @param extension_paths: path of unpacked extension to install. |
| 34 | @param autotest_ext: Load a component extension with privileges to |
| 35 | invoke chrome.autotestPrivate. |
| 36 | @param is_component: Whether extensions should be loaded as component |
| 37 | extensions. |
Achuith Bhandarkar | b7ef5e5 | 2014-03-20 14:18:45 -0700 | [diff] [blame] | 38 | @param num_tries: Number of attempts to log in. |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 39 | @param extra_browser_args: Additional argument(s) to pass to the |
Achuith Bhandarkar | 3875e0c | 2014-03-20 14:17:31 -0700 | [diff] [blame] | 40 | browser. It can be a string or a list. |
Achuith Bhandarkar | 5d6c26c | 2014-04-25 11:19:38 -0700 | [diff] [blame] | 41 | @param clear_enterprise_policy: Clear enterprise policy before |
| 42 | logging in. |
Achuith Bhandarkar | 882a8ab | 2014-08-26 15:51:20 -0700 | [diff] [blame] | 43 | @param dont_override_profile: Don't delete cryptohome before login. |
| 44 | Telemetry will output a warning with this |
| 45 | option. |
Achuith Bhandarkar | 3c654f3 | 2014-09-29 06:33:35 -0700 | [diff] [blame] | 46 | @param disable_gaia_services: For enterprise autotests, this option may |
| 47 | be used to enable policy fetch. |
Achuith Bhandarkar | a2bceaf | 2014-11-14 12:10:16 -0800 | [diff] [blame] | 48 | @param disable_default_apps: For tests that exercise default apps. |
Achuith Bhandarkar | 1cce9dc | 2014-02-05 14:23:34 -0800 | [diff] [blame] | 49 | @param auto_login: Does not login automatically if this is False. |
Achuith Bhandarkar | 3875e0c | 2014-03-20 14:17:31 -0700 | [diff] [blame] | 50 | Useful if you need to examine oobe. |
| 51 | @param gaia_login: Logs in to real gaia. |
Achuith Bhandarkar | 1cce9dc | 2014-02-05 14:23:34 -0800 | [diff] [blame] | 52 | @param username: Log in using this username instead of the default. |
| 53 | @param username: Log in using this password instead of the default. |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 54 | """ |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 55 | self._autotest_ext_path = None |
| 56 | if autotest_ext: |
| 57 | self._autotest_ext_path = os.path.join(os.path.dirname(__file__), |
| 58 | 'autotest_private_ext') |
| 59 | extension_paths.append(self._autotest_ext_path) |
| 60 | |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 61 | finder_options = browser_options.BrowserFinderOptions() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 62 | self._browser_type = (self.BROWSER_TYPE_LOGIN |
| 63 | if logged_in else self.BROWSER_TYPE_GUEST) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 64 | finder_options.browser_type = self.browser_type |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame] | 65 | if extra_browser_args: |
| 66 | finder_options.browser_options.AppendExtraBrowserArgs( |
| 67 | extra_browser_args) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 68 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 69 | if logged_in: |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 70 | extensions_to_load = finder_options.extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 71 | for path in extension_paths: |
| 72 | extension = extension_to_load.ExtensionToLoad( |
Achuith Bhandarkar | 944405a | 2013-11-21 14:47:48 -0800 | [diff] [blame] | 73 | path, self.browser_type, is_component=is_component) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 74 | extensions_to_load.append(extension) |
| 75 | self._extensions_to_load = extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 76 | |
Achuith Bhandarkar | 3e5051d | 2013-10-15 15:20:12 -0700 | [diff] [blame] | 77 | # finder options must be set before parse_args(), browser options must |
| 78 | # be set before Create(). |
Todd Broch | eb6f481 | 2014-04-29 16:04:28 -0700 | [diff] [blame] | 79 | # TODO(crbug.com/360890) Below MUST be '2' so that it doesn't inhibit |
| 80 | # autotest debug logs |
| 81 | finder_options.verbosity = 2 |
Achuith Bhandarkar | dafc9a5 | 2013-09-24 15:26:33 +0200 | [diff] [blame] | 82 | finder_options.CreateParser().parse_args(args=[]) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 83 | b_options = finder_options.browser_options |
| 84 | b_options.disable_component_extensions_with_background_pages = False |
| 85 | b_options.create_browser_with_oobe = True |
Achuith Bhandarkar | 5d6c26c | 2014-04-25 11:19:38 -0700 | [diff] [blame] | 86 | b_options.clear_enterprise_policy = clear_enterprise_policy |
Achuith Bhandarkar | 882a8ab | 2014-08-26 15:51:20 -0700 | [diff] [blame] | 87 | b_options.dont_override_profile = dont_override_profile |
Achuith Bhandarkar | 3c654f3 | 2014-09-29 06:33:35 -0700 | [diff] [blame] | 88 | b_options.disable_gaia_services = disable_gaia_services |
Achuith Bhandarkar | a2bceaf | 2014-11-14 12:10:16 -0800 | [diff] [blame] | 89 | b_options.disable_default_apps = disable_default_apps |
| 90 | b_options.disable_component_extensions_with_background_pages = disable_default_apps |
Achuith Bhandarkar | 1cce9dc | 2014-02-05 14:23:34 -0800 | [diff] [blame] | 91 | |
| 92 | b_options.auto_login = auto_login |
Achuith Bhandarkar | 3875e0c | 2014-03-20 14:17:31 -0700 | [diff] [blame] | 93 | b_options.gaia_login = gaia_login |
Achuith Bhandarkar | 1cce9dc | 2014-02-05 14:23:34 -0800 | [diff] [blame] | 94 | self.username = b_options.username if username is None else username |
| 95 | self.password = b_options.password if password is None else password |
| 96 | b_options.username = self.username |
| 97 | b_options.password = self.password |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 98 | |
David James | 54a830e | 2015-05-06 17:21:47 -0700 | [diff] [blame^] | 99 | # Turn on collection of Chrome coredumps via creation of a magic file. |
| 100 | # (Without this, Chrome coredumps are trashed.) |
| 101 | open(constants.CHROME_CORE_MAGIC_FILE, 'w').close() |
| 102 | |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 103 | for i in range(num_tries): |
| 104 | try: |
| 105 | browser_to_create = browser_finder.FindBrowser(finder_options) |
Achuith Bhandarkar | a2bceaf | 2014-11-14 12:10:16 -0800 | [diff] [blame] | 106 | self._browser = browser_to_create.Create(finder_options) |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 107 | break |
Achuith Bhandarkar | fab82d1 | 2015-02-26 11:05:35 -0800 | [diff] [blame] | 108 | except (exceptions.LoginException) as e: |
Achuith Bhandarkar | f800edf | 2013-12-06 13:37:52 -0800 | [diff] [blame] | 109 | logging.error('Timed out logging in, tries=%d, error=%s', |
| 110 | i, repr(e)) |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 111 | if i == num_tries-1: |
| 112 | raise |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def __enter__(self): |
| 116 | return self |
| 117 | |
| 118 | |
| 119 | def __exit__(self, *args): |
Derek Basehore | 41acbf8 | 2014-07-11 18:34:30 -0700 | [diff] [blame] | 120 | self.close() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 121 | |
| 122 | |
| 123 | @property |
| 124 | def browser(self): |
| 125 | """Returns a telemetry browser instance.""" |
| 126 | return self._browser |
| 127 | |
| 128 | |
| 129 | def get_extension(self, extension_path): |
| 130 | """Fetches a telemetry extension instance given the extension path.""" |
| 131 | for ext in self._extensions_to_load: |
| 132 | if extension_path == ext.path: |
| 133 | return self.browser.extensions[ext] |
| 134 | return None |
| 135 | |
| 136 | |
| 137 | @property |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 138 | def autotest_ext(self): |
| 139 | """Returns the autotest extension.""" |
| 140 | return self.get_extension(self._autotest_ext_path) |
| 141 | |
| 142 | |
| 143 | @property |
| 144 | def login_status(self): |
| 145 | """Returns login status.""" |
| 146 | ext = self.autotest_ext |
| 147 | if not ext: |
| 148 | return None |
| 149 | |
| 150 | ext.ExecuteJavaScript(''' |
| 151 | window.__login_status = null; |
| 152 | chrome.autotestPrivate.loginStatus(function(s) { |
| 153 | window.__login_status = s; |
| 154 | }); |
| 155 | ''') |
| 156 | return ext.EvaluateJavaScript('window.__login_status') |
| 157 | |
| 158 | |
| 159 | @property |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 160 | def browser_type(self): |
| 161 | """Returns the browser_type.""" |
| 162 | return self._browser_type |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 163 | |
| 164 | |
Achuith Bhandarkar | 16ee977 | 2015-01-23 17:18:18 -0800 | [diff] [blame] | 165 | @staticmethod |
| 166 | def did_browser_crash(func): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 167 | """Runs func, returns True if the browser crashed, False otherwise. |
| 168 | |
| 169 | @param func: function to run. |
| 170 | |
| 171 | """ |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 172 | try: |
| 173 | func() |
Achuith Bhandarkar | 704134d | 2015-03-05 17:31:57 -0800 | [diff] [blame] | 174 | except (Error): |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 175 | return True |
| 176 | return False |
Derek Basehore | 41acbf8 | 2014-07-11 18:34:30 -0700 | [diff] [blame] | 177 | |
| 178 | |
Achuith Bhandarkar | 16ee977 | 2015-01-23 17:18:18 -0800 | [diff] [blame] | 179 | def wait_for_browser_to_come_up(self): |
| 180 | """Waits for the browser to come up. This should only be called after a |
| 181 | browser crash. |
| 182 | """ |
| 183 | def _BrowserReady(cr): |
| 184 | tabs = [] # Wrapper for pass by reference. |
| 185 | if self.did_browser_crash( |
| 186 | lambda: tabs.append(cr.browser.tabs.New())): |
| 187 | return False |
| 188 | try: |
| 189 | tabs[0].Close() |
Achuith Bhandarkar | fab82d1 | 2015-02-26 11:05:35 -0800 | [diff] [blame] | 190 | except: |
Achuith Bhandarkar | 16ee977 | 2015-01-23 17:18:18 -0800 | [diff] [blame] | 191 | # crbug.com/350941 |
| 192 | logging.error('Timed out closing tab') |
| 193 | return True |
| 194 | util.WaitFor(lambda: _BrowserReady(self), timeout=10) |
| 195 | |
| 196 | |
Derek Basehore | 41acbf8 | 2014-07-11 18:34:30 -0700 | [diff] [blame] | 197 | def close(self): |
| 198 | """Closes the browser.""" |
| 199 | self._browser.Close() |