blob: 3f86dafd116274b063bd241b379f46f84cde5d35 [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,
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080024 is_component=True, num_tries=1, extra_browser_args=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080025 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080026 Constructor of telemetry wrapper.
27
28 @param logged_in: Regular user (True) or guest user (False).
29 @param extension_paths: path of unpacked extension to install.
30 @param autotest_ext: Load a component extension with privileges to
31 invoke chrome.autotestPrivate.
32 @param is_component: Whether extensions should be loaded as component
33 extensions.
34 @param num_tries: Number of attempts to log in. (Temporary for
35 debugging).
Dean Liaob12e2ee2013-11-19 16:49:12 +080036 @param extra_browser_args: Additional argument(s) to pass to the
37 browser. It can be a string or a list.
38 """
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
68
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080069 for i in range(num_tries):
70 try:
71 browser_to_create = browser_finder.FindBrowser(finder_options)
72 self._browser = browser_to_create.Create()
73 self._browser.Start()
74 break
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -080075 except (util.TimeoutException, exceptions.LoginException) as e:
76 logging.error('Timed out logging in, tries=%d, error=%s',
77 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080078 if i == num_tries-1:
79 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -070080
81
82 def __enter__(self):
83 return self
84
85
86 def __exit__(self, *args):
87 self.browser.Close()
88
89
90 @property
91 def browser(self):
92 """Returns a telemetry browser instance."""
93 return self._browser
94
95
96 def get_extension(self, extension_path):
97 """Fetches a telemetry extension instance given the extension path."""
98 for ext in self._extensions_to_load:
99 if extension_path == ext.path:
100 return self.browser.extensions[ext]
101 return None
102
103
104 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700105 def autotest_ext(self):
106 """Returns the autotest extension."""
107 return self.get_extension(self._autotest_ext_path)
108
109
110 @property
111 def login_status(self):
112 """Returns login status."""
113 ext = self.autotest_ext
114 if not ext:
115 return None
116
117 ext.ExecuteJavaScript('''
118 window.__login_status = null;
119 chrome.autotestPrivate.loginStatus(function(s) {
120 window.__login_status = s;
121 });
122 ''')
123 return ext.EvaluateJavaScript('window.__login_status')
124
125
126 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700127 def browser_type(self):
128 """Returns the browser_type."""
129 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700130
131
132 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700133 """Waits for the browser to come up. This should only be called after a
134 browser crash.
135 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700136 def _BrowserReady(cr):
137 try:
138 tab = cr.browser.tabs.New()
139 except (exceptions.BrowserGoneException,
140 exceptions.BrowserConnectionGoneException):
141 return False
142 tab.Close()
143 return True
144 util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10)
145
146
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700147 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700148 """Runs func, returns True if the browser crashed, False otherwise.
149
150 @param func: function to run.
151
152 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700153 try:
154 func()
155 except (exceptions.BrowserGoneException,
156 exceptions.BrowserConnectionGoneException):
157 return True
158 return False
159