blob: ce244281938c0c4ca8f71246e2c57784a814b308 [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
75 except util.TimeoutException:
76 logging.error('Timed out logging in, tries=%d', i)
77 if i == num_tries-1:
78 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -070079
80
81 def __enter__(self):
82 return self
83
84
85 def __exit__(self, *args):
86 self.browser.Close()
87
88
89 @property
90 def browser(self):
91 """Returns a telemetry browser instance."""
92 return self._browser
93
94
95 def get_extension(self, extension_path):
96 """Fetches a telemetry extension instance given the extension path."""
97 for ext in self._extensions_to_load:
98 if extension_path == ext.path:
99 return self.browser.extensions[ext]
100 return None
101
102
103 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700104 def autotest_ext(self):
105 """Returns the autotest extension."""
106 return self.get_extension(self._autotest_ext_path)
107
108
109 @property
110 def login_status(self):
111 """Returns login status."""
112 ext = self.autotest_ext
113 if not ext:
114 return None
115
116 ext.ExecuteJavaScript('''
117 window.__login_status = null;
118 chrome.autotestPrivate.loginStatus(function(s) {
119 window.__login_status = s;
120 });
121 ''')
122 return ext.EvaluateJavaScript('window.__login_status')
123
124
125 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700126 def browser_type(self):
127 """Returns the browser_type."""
128 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700129
130
131 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700132 """Waits for the browser to come up. This should only be called after a
133 browser crash.
134 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700135 def _BrowserReady(cr):
136 try:
137 tab = cr.browser.tabs.New()
138 except (exceptions.BrowserGoneException,
139 exceptions.BrowserConnectionGoneException):
140 return False
141 tab.Close()
142 return True
143 util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10)
144
145
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700146 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700147 """Runs func, returns True if the browser crashed, False otherwise.
148
149 @param func: function to run.
150
151 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700152 try:
153 func()
154 except (exceptions.BrowserGoneException,
155 exceptions.BrowserConnectionGoneException):
156 return True
157 return False
158