blob: 045cdce21eadc5f7d9e6b0b7344f44dd80be2bce [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 Bhandarkarb0e99072013-11-18 16:15:03 -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 -070011# Name of the logged-in user specified by the telemetry login extension.
Dennis Jeffreydffc0bd2013-05-03 13:24:31 -070012LOGIN_USER = 'test@test.test'
13
14
Achuith Bhandarkared498932013-07-16 17:01:40 -070015class Chrome(object):
16 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070017
18
Achuith Bhandarkared498932013-07-16 17:01:40 -070019 BROWSER_TYPE_LOGIN = 'system'
20 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070021
22
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080023 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Dean Liaob12e2ee2013-11-19 16:49:12 +080024 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 Bhandarkara2df47b2013-10-09 21:23:15 -070029 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 Bhandarkar7fb57f72013-08-29 15:29:06 -070035 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070036 self._browser_type = (self.BROWSER_TYPE_LOGIN
37 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070038 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080039 if extra_browser_args:
40 finder_options.browser_options.AppendExtraBrowserArgs(
41 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070042
Achuith Bhandarkared498932013-07-16 17:01:40 -070043 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070044 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070045 for path in extension_paths:
46 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070047 path, self.browser_type, is_component=True)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070048 extensions_to_load.append(extension)
49 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070050
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070051 # 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 Bhandarkardafc9a52013-09-24 15:26:33 +020054 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070055 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 Bhandarkarb0e99072013-11-18 16:15:03 -080059 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 Bhandarkared498932013-07-16 17:01:40 -070069
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 Bhandarkara2df47b2013-10-09 21:23:15 -070094 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 Bhandarkared498932013-07-16 17:01:40 -0700116 def browser_type(self):
117 """Returns the browser_type."""
118 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700119
120
121 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700122 """Waits for the browser to come up. This should only be called after a
123 browser crash.
124 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700125 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 Bhandarkar5abf60b2013-08-01 12:35:53 -0700136 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700137 """Runs func, returns True if the browser crashed, False otherwise.
138
139 @param func: function to run.
140
141 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700142 try:
143 func()
144 except (exceptions.BrowserGoneException,
145 exceptions.BrowserConnectionGoneException):
146 return True
147 return False
148