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 | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 5 | import logging, os |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 6 | |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 7 | from telemetry.core import browser_finder, browser_options, exceptions |
| 8 | from telemetry.core import extension_to_load, util |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 9 | |
| 10 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 11 | # Name of the logged-in user specified by the telemetry login extension. |
Dennis Jeffrey | dffc0bd | 2013-05-03 13:24:31 -0700 | [diff] [blame] | 12 | LOGIN_USER = 'test@test.test' |
| 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, |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame^] | 24 | num_tries=1, extra_browser_args=None): |
| 25 | """ |
| 26 | @param extra_browser_args: Additional argument(s) to pass to the |
| 27 | browser. It can be a string or a list. |
| 28 | """ |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 29 | self._autotest_ext_path = None |
| 30 | if autotest_ext: |
| 31 | self._autotest_ext_path = os.path.join(os.path.dirname(__file__), |
| 32 | 'autotest_private_ext') |
| 33 | extension_paths.append(self._autotest_ext_path) |
| 34 | |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 35 | finder_options = browser_options.BrowserFinderOptions() |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 36 | self._browser_type = (self.BROWSER_TYPE_LOGIN |
| 37 | if logged_in else self.BROWSER_TYPE_GUEST) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 38 | finder_options.browser_type = self.browser_type |
Dean Liao | b12e2ee | 2013-11-19 16:49:12 +0800 | [diff] [blame^] | 39 | if extra_browser_args: |
| 40 | finder_options.browser_options.AppendExtraBrowserArgs( |
| 41 | extra_browser_args) |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 42 | |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 43 | if logged_in: |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 44 | extensions_to_load = finder_options.extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 45 | for path in extension_paths: |
| 46 | extension = extension_to_load.ExtensionToLoad( |
Achuith Bhandarkar | f130c40 | 2013-09-11 14:39:22 -0700 | [diff] [blame] | 47 | path, self.browser_type, is_component=True) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 48 | extensions_to_load.append(extension) |
| 49 | self._extensions_to_load = extensions_to_load |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 50 | |
Achuith Bhandarkar | 3e5051d | 2013-10-15 15:20:12 -0700 | [diff] [blame] | 51 | # finder options must be set before parse_args(), browser options must |
| 52 | # be set before Create(). |
| 53 | finder_options.verbosity = 1 # info logging for telemetry. |
Achuith Bhandarkar | dafc9a5 | 2013-09-24 15:26:33 +0200 | [diff] [blame] | 54 | finder_options.CreateParser().parse_args(args=[]) |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 55 | b_options = finder_options.browser_options |
| 56 | b_options.disable_component_extensions_with_background_pages = False |
| 57 | b_options.create_browser_with_oobe = True |
| 58 | |
Achuith Bhandarkar | b0e9907 | 2013-11-18 16:15:03 -0800 | [diff] [blame] | 59 | for i in range(num_tries): |
| 60 | try: |
| 61 | browser_to_create = browser_finder.FindBrowser(finder_options) |
| 62 | self._browser = browser_to_create.Create() |
| 63 | self._browser.Start() |
| 64 | break |
| 65 | except util.TimeoutException: |
| 66 | logging.error('Timed out logging in, tries=%d', i) |
| 67 | if i == num_tries-1: |
| 68 | raise |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def __enter__(self): |
| 72 | return self |
| 73 | |
| 74 | |
| 75 | def __exit__(self, *args): |
| 76 | self.browser.Close() |
| 77 | |
| 78 | |
| 79 | @property |
| 80 | def browser(self): |
| 81 | """Returns a telemetry browser instance.""" |
| 82 | return self._browser |
| 83 | |
| 84 | |
| 85 | def get_extension(self, extension_path): |
| 86 | """Fetches a telemetry extension instance given the extension path.""" |
| 87 | for ext in self._extensions_to_load: |
| 88 | if extension_path == ext.path: |
| 89 | return self.browser.extensions[ext] |
| 90 | return None |
| 91 | |
| 92 | |
| 93 | @property |
Achuith Bhandarkar | a2df47b | 2013-10-09 21:23:15 -0700 | [diff] [blame] | 94 | def autotest_ext(self): |
| 95 | """Returns the autotest extension.""" |
| 96 | return self.get_extension(self._autotest_ext_path) |
| 97 | |
| 98 | |
| 99 | @property |
| 100 | def login_status(self): |
| 101 | """Returns login status.""" |
| 102 | ext = self.autotest_ext |
| 103 | if not ext: |
| 104 | return None |
| 105 | |
| 106 | ext.ExecuteJavaScript(''' |
| 107 | window.__login_status = null; |
| 108 | chrome.autotestPrivate.loginStatus(function(s) { |
| 109 | window.__login_status = s; |
| 110 | }); |
| 111 | ''') |
| 112 | return ext.EvaluateJavaScript('window.__login_status') |
| 113 | |
| 114 | |
| 115 | @property |
Achuith Bhandarkar | ed49893 | 2013-07-16 17:01:40 -0700 | [diff] [blame] | 116 | def browser_type(self): |
| 117 | """Returns the browser_type.""" |
| 118 | return self._browser_type |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def wait_for_browser_to_come_up(self): |
Achuith Bhandarkar | a5bb3f6 | 2013-10-10 10:39:57 -0700 | [diff] [blame] | 122 | """Waits for the browser to come up. This should only be called after a |
| 123 | browser crash. |
| 124 | """ |
Achuith Bhandarkar | 49c72d9 | 2013-07-25 11:10:10 -0700 | [diff] [blame] | 125 | def _BrowserReady(cr): |
| 126 | try: |
| 127 | tab = cr.browser.tabs.New() |
| 128 | except (exceptions.BrowserGoneException, |
| 129 | exceptions.BrowserConnectionGoneException): |
| 130 | return False |
| 131 | tab.Close() |
| 132 | return True |
| 133 | util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10) |
| 134 | |
| 135 | |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 136 | def did_browser_crash(self, func): |
Achuith Bhandarkar | 7fb57f7 | 2013-08-29 15:29:06 -0700 | [diff] [blame] | 137 | """Runs func, returns True if the browser crashed, False otherwise. |
| 138 | |
| 139 | @param func: function to run. |
| 140 | |
| 141 | """ |
Achuith Bhandarkar | 5abf60b | 2013-08-01 12:35:53 -0700 | [diff] [blame] | 142 | try: |
| 143 | func() |
| 144 | except (exceptions.BrowserGoneException, |
| 145 | exceptions.BrowserConnectionGoneException): |
| 146 | return True |
| 147 | return False |
| 148 | |