blob: 65cdae02e00ccc69d38476cc3f52a5d33e76b97c [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 -070011class Chrome(object):
12 """Wrapper for creating a telemetry browser instance with extensions."""
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070013
14
Achuith Bhandarkared498932013-07-16 17:01:40 -070015 BROWSER_TYPE_LOGIN = 'system'
16 BROWSER_TYPE_GUEST = 'system-guest'
Achuith Bhandarkar86c46812013-07-15 17:13:39 -070017
18
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080019 def __init__(self, logged_in=True, extension_paths=[], autotest_ext=False,
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070020 is_component=True, num_tries=3, extra_browser_args=None,
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080021 auto_login=True, username=None, password=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080022 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080023 Constructor of telemetry wrapper.
24
25 @param logged_in: Regular user (True) or guest user (False).
26 @param extension_paths: path of unpacked extension to install.
27 @param autotest_ext: Load a component extension with privileges to
28 invoke chrome.autotestPrivate.
29 @param is_component: Whether extensions should be loaded as component
30 extensions.
Achuith Bhandarkarb7ef5e52014-03-20 14:18:45 -070031 @param num_tries: Number of attempts to log in.
Dean Liaob12e2ee2013-11-19 16:49:12 +080032 @param extra_browser_args: Additional argument(s) to pass to the
33 browser. It can be a string or a list.
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080034 @param auto_login: Does not login automatically if this is False.
35 Useful if you need to examine oobe.
36 @param username: Log in using this username instead of the default.
37 @param username: Log in using this password instead of the default.
Dean Liaob12e2ee2013-11-19 16:49:12 +080038 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070039 self._autotest_ext_path = None
40 if autotest_ext:
41 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
42 'autotest_private_ext')
43 extension_paths.append(self._autotest_ext_path)
44
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070045 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070046 self._browser_type = (self.BROWSER_TYPE_LOGIN
47 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070048 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080049 if extra_browser_args:
50 finder_options.browser_options.AppendExtraBrowserArgs(
51 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070052
Achuith Bhandarkared498932013-07-16 17:01:40 -070053 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070054 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070055 for path in extension_paths:
56 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080057 path, self.browser_type, is_component=is_component)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070058 extensions_to_load.append(extension)
59 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070060
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070061 # finder options must be set before parse_args(), browser options must
62 # be set before Create().
63 finder_options.verbosity = 1 # info logging for telemetry.
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020064 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070065 b_options = finder_options.browser_options
66 b_options.disable_component_extensions_with_background_pages = False
67 b_options.create_browser_with_oobe = True
Achuith Bhandarkar1cce9dc2014-02-05 14:23:34 -080068
69 b_options.auto_login = auto_login
70 self.username = b_options.username if username is None else username
71 self.password = b_options.password if password is None else password
72 b_options.username = self.username
73 b_options.password = self.password
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070074
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080075 for i in range(num_tries):
76 try:
77 browser_to_create = browser_finder.FindBrowser(finder_options)
78 self._browser = browser_to_create.Create()
79 self._browser.Start()
80 break
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -080081 except (util.TimeoutException, exceptions.LoginException) as e:
82 logging.error('Timed out logging in, tries=%d, error=%s',
83 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080084 if i == num_tries-1:
85 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -070086
87
88 def __enter__(self):
89 return self
90
91
92 def __exit__(self, *args):
93 self.browser.Close()
94
95
96 @property
97 def browser(self):
98 """Returns a telemetry browser instance."""
99 return self._browser
100
101
102 def get_extension(self, extension_path):
103 """Fetches a telemetry extension instance given the extension path."""
104 for ext in self._extensions_to_load:
105 if extension_path == ext.path:
106 return self.browser.extensions[ext]
107 return None
108
109
110 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700111 def autotest_ext(self):
112 """Returns the autotest extension."""
113 return self.get_extension(self._autotest_ext_path)
114
115
116 @property
117 def login_status(self):
118 """Returns login status."""
119 ext = self.autotest_ext
120 if not ext:
121 return None
122
123 ext.ExecuteJavaScript('''
124 window.__login_status = null;
125 chrome.autotestPrivate.loginStatus(function(s) {
126 window.__login_status = s;
127 });
128 ''')
129 return ext.EvaluateJavaScript('window.__login_status')
130
131
132 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700133 def browser_type(self):
134 """Returns the browser_type."""
135 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700136
137
138 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700139 """Waits for the browser to come up. This should only be called after a
140 browser crash.
141 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700142 def _BrowserReady(cr):
143 try:
144 tab = cr.browser.tabs.New()
145 except (exceptions.BrowserGoneException,
146 exceptions.BrowserConnectionGoneException):
147 return False
Achuith Bhandarkar0998e182014-03-17 16:44:21 -0700148 try:
149 tab.Close()
150 except (util.TimeoutException):
151 # crbug.com/350941
152 logging.error('Timed out closing tab')
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700153 return True
Chris Masone73efa2b2014-02-06 14:20:55 -0800154 util.WaitFor(lambda: _BrowserReady(self), timeout=10)
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700155
156
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700157 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700158 """Runs func, returns True if the browser crashed, False otherwise.
159
160 @param func: function to run.
161
162 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700163 try:
164 func()
165 except (exceptions.BrowserGoneException,
166 exceptions.BrowserConnectionGoneException):
167 return True
168 return False