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