blob: 35c642f4f265f6c8e08106058ffdc5c5590883bc [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,
24 num_tries=1):
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070025 self._autotest_ext_path = None
26 if autotest_ext:
27 self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
28 'autotest_private_ext')
29 extension_paths.append(self._autotest_ext_path)
30
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -070031 finder_options = browser_options.BrowserFinderOptions()
Achuith Bhandarkared498932013-07-16 17:01:40 -070032 self._browser_type = (self.BROWSER_TYPE_LOGIN
33 if logged_in else self.BROWSER_TYPE_GUEST)
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070034 finder_options.browser_type = self.browser_type
35
Achuith Bhandarkared498932013-07-16 17:01:40 -070036 if logged_in:
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070037 extensions_to_load = finder_options.extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070038 for path in extension_paths:
39 extension = extension_to_load.ExtensionToLoad(
Achuith Bhandarkarf130c402013-09-11 14:39:22 -070040 path, self.browser_type, is_component=True)
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070041 extensions_to_load.append(extension)
42 self._extensions_to_load = extensions_to_load
Achuith Bhandarkared498932013-07-16 17:01:40 -070043
Achuith Bhandarkar3e5051d2013-10-15 15:20:12 -070044 # finder options must be set before parse_args(), browser options must
45 # be set before Create().
46 finder_options.verbosity = 1 # info logging for telemetry.
Achuith Bhandarkardafc9a52013-09-24 15:26:33 +020047 finder_options.CreateParser().parse_args(args=[])
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070048 b_options = finder_options.browser_options
49 b_options.disable_component_extensions_with_background_pages = False
50 b_options.create_browser_with_oobe = True
51
Achuith Bhandarkarb0e99072013-11-18 16:15:03 -080052 for i in range(num_tries):
53 try:
54 browser_to_create = browser_finder.FindBrowser(finder_options)
55 self._browser = browser_to_create.Create()
56 self._browser.Start()
57 break
58 except util.TimeoutException:
59 logging.error('Timed out logging in, tries=%d', i)
60 if i == num_tries-1:
61 raise
Achuith Bhandarkared498932013-07-16 17:01:40 -070062
63
64 def __enter__(self):
65 return self
66
67
68 def __exit__(self, *args):
69 self.browser.Close()
70
71
72 @property
73 def browser(self):
74 """Returns a telemetry browser instance."""
75 return self._browser
76
77
78 def get_extension(self, extension_path):
79 """Fetches a telemetry extension instance given the extension path."""
80 for ext in self._extensions_to_load:
81 if extension_path == ext.path:
82 return self.browser.extensions[ext]
83 return None
84
85
86 @property
Achuith Bhandarkara2df47b2013-10-09 21:23:15 -070087 def autotest_ext(self):
88 """Returns the autotest extension."""
89 return self.get_extension(self._autotest_ext_path)
90
91
92 @property
93 def login_status(self):
94 """Returns login status."""
95 ext = self.autotest_ext
96 if not ext:
97 return None
98
99 ext.ExecuteJavaScript('''
100 window.__login_status = null;
101 chrome.autotestPrivate.loginStatus(function(s) {
102 window.__login_status = s;
103 });
104 ''')
105 return ext.EvaluateJavaScript('window.__login_status')
106
107
108 @property
Achuith Bhandarkared498932013-07-16 17:01:40 -0700109 def browser_type(self):
110 """Returns the browser_type."""
111 return self._browser_type
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700112
113
114 def wait_for_browser_to_come_up(self):
Achuith Bhandarkara5bb3f62013-10-10 10:39:57 -0700115 """Waits for the browser to come up. This should only be called after a
116 browser crash.
117 """
Achuith Bhandarkar49c72d92013-07-25 11:10:10 -0700118 def _BrowserReady(cr):
119 try:
120 tab = cr.browser.tabs.New()
121 except (exceptions.BrowserGoneException,
122 exceptions.BrowserConnectionGoneException):
123 return False
124 tab.Close()
125 return True
126 util.WaitFor(lambda: _BrowserReady(self), poll_interval=1, timeout=10)
127
128
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700129 def did_browser_crash(self, func):
Achuith Bhandarkar7fb57f72013-08-29 15:29:06 -0700130 """Runs func, returns True if the browser crashed, False otherwise.
131
132 @param func: function to run.
133
134 """
Achuith Bhandarkar5abf60b2013-08-01 12:35:53 -0700135 try:
136 func()
137 except (exceptions.BrowserGoneException,
138 exceptions.BrowserConnectionGoneException):
139 return True
140 return False
141