blob: b23e6cd8f713c660c5d276281a7331cbfb45d958 [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 Bhandarkar944405a2013-11-21 14:47:48 -080020 is_component=True, num_tries=1, extra_browser_args=None):
Dean Liaob12e2ee2013-11-19 16:49:12 +080021 """
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080022 Constructor of telemetry wrapper.
23
24 @param logged_in: Regular user (True) or guest user (False).
25 @param extension_paths: path of unpacked extension to install.
26 @param autotest_ext: Load a component extension with privileges to
27 invoke chrome.autotestPrivate.
28 @param is_component: Whether extensions should be loaded as component
29 extensions.
30 @param num_tries: Number of attempts to log in. (Temporary for
31 debugging).
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.
34 """
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070035 self._autotest_ext_path = None
36 if autotest_ext:
37 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
38 'autotest_private_ext')
39 extension_paths.append(self._autotest_ext_path)
40
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070041 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070042 self._browser_type = (self.BROWSER_TYPE_LOGIN
43 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070044 finder_options.browser_type = self.browser_type
Dean Liaob12e2ee2013-11-19 16:49:12 +080045 if extra_browser_args:
46 finder_options.browser_options.AppendExtraBrowserArgs(
47 extra_browser_args)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070048
Achuith Bhandarkared498932013-07-16 17:01:40 -070049 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070050 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070051 for path in extension_paths:
52 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkar944405a2013-11-21 14:47:48 -080053 path, self.browser_type, is_component=is_component)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070054 extensions_to_load.append(extension)
55 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070056
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070057 # finder options must be set before parse_args(), browser options must
58 # be set before Create().
59 finder_options.verbosity = 1 # info logging for telemetry.
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020060 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070061 b_options = finder_options.browser_options
62 b_options.disable_component_extensions_with_background_pages = False
63 b_options.create_browser_with_oobe = True
Achuith Bhandarkar696a8aa2014-01-21 15:52:21 -080064 self.username = b_options.username
65 self.password = b_options.password
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070066
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080067 for i in range(num_tries):
68 try:
69 browser_to_create = browser_finder.FindBrowser(finder_options)
70 self._browser = browser_to_create.Create()
71 self._browser.Start()
72 break
Achuith Bhandarkarf800edf2013-12-06 13:37:52 -080073 except (util.TimeoutException, exceptions.LoginException) as e:
74 logging.error('Timed out logging in, tries=%d, error=%s',
75 i, repr(e))
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080076 if i == num_tries-1:
77 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -070078
79
80 def __enter__(self):
81 return self
82
83
84 def __exit__(self, *args):
85 self.browser.Close()
86
87
88 @property
89 def browser(self):
90 """Returns a telemetry browser instance."""
91 return self._browser
92
93
94 def get_extension(self, extension_path):
95 """Fetches a telemetry extension instance given the extension path."""
96 for ext in self._extensions_to_load:
97 if extension_path == ext.path:
98 return self.browser.extensions[ext]
99 return None
100
101
102 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -0700103 def autotest_ext(self):
104 """Returns the autotest extension."""
105 return self.get_extension(self._autotest_ext_path)
106
107
108 @property
109 def login_status(self):
110 """Returns login status."""
111 ext = self.autotest_ext
112 if not ext:
113 return None
114
115 ext.ExecuteJavaScript('''
116 window.__login_status = null;
117 chrome.autotestPrivate.loginStatus(function(s) {
118 window.__login_status = s;
119 });
120 ''')
121 return ext.EvaluateJavaScript('window.__login_status')
122
123
124 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700125 def browser_type(self):
126 """Returns the browser_type."""
127 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700128
129
130 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700131 """Waits for the browser to come up. This should only be called after a
132 browser crash.
133 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700134 def _BrowserReady(cr):
135 try:
136 tab = cr.browser.tabs.New()
137 except (exceptions.BrowserGoneException,
138 exceptions.BrowserConnectionGoneException):
139 return False
140 tab.Close()
141 return True
142 util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10)
143
144
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700145 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700146 """Runs func, returns True if the browser crashed, False otherwise.
147
148 @param func: function to run.
149
150 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700151 try:
152 func()
153 except (exceptions.BrowserGoneException,
154 exceptions.BrowserConnectionGoneException):
155 return True
156 return False
157