blob: 1b0f8b10fda7f6fee7875ca3f9b2854491e26640 [file] [log] [blame]
Dan Shic1d263b2013-10-04 17:31:38 -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
5import atexit
6import logging
7import os
8import urllib2
Payton Turnage0b2becc2015-06-17 16:31:14 -07009import urlparse
Dan Shic1d263b2013-10-04 17:31:38 -070010
Dan Shi96c77cb2013-11-19 10:30:37 -080011try:
12 from selenium import webdriver
13except ImportError:
14 # Ignore import error, as this can happen when builder tries to call the
15 # setup method of test that imports chromedriver.
16 logging.error('selenium module failed to be imported.')
17 pass
Dan Shic1d263b2013-10-04 17:31:38 -070018
Dan Shic1d263b2013-10-04 17:31:38 -070019from autotest_lib.client.bin import utils
20from autotest_lib.client.common_lib.cros import chrome
21
22CHROMEDRIVER_EXE_PATH = '/usr/local/chromedriver/chromedriver'
beeps47a51292013-11-20 08:55:23 -080023X_SERVER_DISPLAY = ':0'
24X_AUTHORITY = '/home/chronos/.Xauthority'
25
Dan Shic1d263b2013-10-04 17:31:38 -070026
27class chromedriver(object):
28 """Wrapper class, a context manager type, for tests to use Chrome Driver."""
29
30 def __init__(self, extra_chrome_flags=[], subtract_extra_chrome_flags=[],
Achuith Bhandarkar8949a8d2016-11-16 14:28:09 -080031 extension_paths=[], username=None, password=None,
32 server_port=None, skip_cleanup=False, url_base=None,
33 extra_chromedriver_args=None, *args, **kwargs):
Dan Shic1d263b2013-10-04 17:31:38 -070034 """Initialize.
35
36 @param extra_chrome_flags: Extra chrome flags to pass to chrome, if any.
37 @param subtract_extra_chrome_flags: Remove default flags passed to
38 chrome by chromedriver, if any.
beeps47a51292013-11-20 08:55:23 -080039 @param extension_paths: A list of paths to unzipped extensions. Note
40 that paths to crx files won't work.
Nathan Stoddardab583042014-08-11 10:41:47 -070041 @param username: Log in using this username instead of the default.
Resetswitch98a0f7d2015-05-04 14:48:08 -070042 @param password: Log in using this password instead of the default.
43 @param server_port: Port number for the chromedriver server. If None,
44 an available port is chosen at random.
Resetswitchd1df3d52015-05-20 18:20:55 -070045 @param skip_cleanup: If True, leave the server and browser running
46 so that remote tests can run after this script
47 ends. Default is False.
Payton Turnage0b2becc2015-06-17 16:31:14 -070048 @param url_base: Optional base url for chromedriver.
49 @param extra_chromedriver_args: List of extra arguments to forward to
50 the chromedriver binary, if any.
Dan Shic1d263b2013-10-04 17:31:38 -070051 """
Resetswitchd1df3d52015-05-20 18:20:55 -070052 self._cleanup = not skip_cleanup
Dan Shic1d263b2013-10-04 17:31:38 -070053 assert os.geteuid() == 0, 'Need superuser privileges'
54
55 # Log in with telemetry
beepsbff9f9d2013-12-06 11:14:08 -080056 self._chrome = chrome.Chrome(extension_paths=extension_paths,
Nathan Stoddardab583042014-08-11 10:41:47 -070057 username=username,
58 password=password,
beepsbff9f9d2013-12-06 11:14:08 -080059 extra_browser_args=extra_chrome_flags)
60 self._browser = self._chrome.browser
Dan Shi2bff39b2014-03-28 11:36:13 -070061 # Close all tabs owned and opened by Telemetry, as these cannot be
62 # transferred to ChromeDriver.
63 self._browser.tabs[0].Close()
Dan Shic1d263b2013-10-04 17:31:38 -070064
65 # Start ChromeDriver server
Resetswitch98a0f7d2015-05-04 14:48:08 -070066 self._server = chromedriver_server(CHROMEDRIVER_EXE_PATH,
Resetswitchd1df3d52015-05-20 18:20:55 -070067 port=server_port,
Payton Turnage0b2becc2015-06-17 16:31:14 -070068 skip_cleanup=skip_cleanup,
69 url_base=url_base,
70 extra_args=extra_chromedriver_args)
Dan Shic1d263b2013-10-04 17:31:38 -070071
Dan Shi2bff39b2014-03-28 11:36:13 -070072 # Open a new tab using Chrome remote debugging. ChromeDriver expects
73 # a tab opened for remote to work. Tabs opened using Telemetry will be
74 # owned by Telemetry, and will be inaccessible to ChromeDriver.
75 urllib2.urlopen('http://localhost:%i/json/new' %
76 utils.get_chrome_remote_debugging_port())
77
Dan Shic1d263b2013-10-04 17:31:38 -070078 chromeOptions = {'debuggerAddress':
79 ('localhost:%d' %
80 utils.get_chrome_remote_debugging_port())}
81 capabilities = {'chromeOptions':chromeOptions}
82 # Handle to chromedriver, for chrome automation.
Dan Shi96c77cb2013-11-19 10:30:37 -080083 try:
84 self.driver = webdriver.Remote(command_executor=self._server.url,
85 desired_capabilities=capabilities)
86 except NameError:
87 logging.error('selenium module failed to be imported.')
88 raise
Dan Shic1d263b2013-10-04 17:31:38 -070089
90
91 def __enter__(self):
92 return self
93
94
95 def __exit__(self, *args):
96 """Clean up after running the test.
97
98 """
99 if hasattr(self, 'driver') and self.driver:
100 self.driver.close()
101 del self.driver
102
Resetswitchd1df3d52015-05-20 18:20:55 -0700103 if not hasattr(self, '_cleanup') or self._cleanup:
104 if hasattr(self, '_server') and self._server:
105 self._server.close()
106 del self._server
Dan Shic1d263b2013-10-04 17:31:38 -0700107
Resetswitchd1df3d52015-05-20 18:20:55 -0700108 if hasattr(self, '_browser') and self._browser:
109 self._browser.Close()
110 del self._browser
Dan Shic1d263b2013-10-04 17:31:38 -0700111
beepsbff9f9d2013-12-06 11:14:08 -0800112 def get_extension(self, extension_path):
113 """Gets an extension by proxying to the browser.
114
115 @param extension_path: Path to the extension loaded in the browser.
116
117 @return: A telemetry extension object representing the extension.
118 """
119 return self._chrome.get_extension(extension_path)
120
121
Mussaa074a512015-10-20 16:53:23 -0700122 @property
123 def chrome_instance(self):
124 """ The chrome instance used by this chrome driver instance. """
125 return self._chrome
126
127
Dan Shic1d263b2013-10-04 17:31:38 -0700128class chromedriver_server(object):
129 """A running ChromeDriver server.
130
131 This code is migrated from chrome:
132 src/chrome/test/chromedriver/server/server.py
133 """
134
Payton Turnage0b2becc2015-06-17 16:31:14 -0700135 def __init__(self, exe_path, port=None, skip_cleanup=False,
136 url_base=None, extra_args=None):
Dan Shic1d263b2013-10-04 17:31:38 -0700137 """Starts the ChromeDriver server and waits for it to be ready.
138
139 Args:
140 exe_path: path to the ChromeDriver executable
Resetswitch98a0f7d2015-05-04 14:48:08 -0700141 port: server port. If None, an available port is chosen at random.
Resetswitchd1df3d52015-05-20 18:20:55 -0700142 skip_cleanup: If True, leave the server running so that remote
143 tests can run after this script ends. Default is
144 False.
Payton Turnage0b2becc2015-06-17 16:31:14 -0700145 url_base: Optional base url for chromedriver.
146 extra_args: List of extra arguments to forward to the chromedriver
147 binary, if any.
Dan Shic1d263b2013-10-04 17:31:38 -0700148 Raises:
149 RuntimeError if ChromeDriver fails to start
150 """
151 if not os.path.exists(exe_path):
152 raise RuntimeError('ChromeDriver exe not found at: ' + exe_path)
153
Resetswitchd1df3d52015-05-20 18:20:55 -0700154 chromedriver_args = [exe_path]
155 if port:
156 # Allow remote connections if a port was specified
157 chromedriver_args.append('--whitelisted-ips')
158 else:
Resetswitch98a0f7d2015-05-04 14:48:08 -0700159 port = utils.get_unused_port()
Resetswitchd1df3d52015-05-20 18:20:55 -0700160 chromedriver_args.append('--port=%d' % port)
beeps47a51292013-11-20 08:55:23 -0800161
Payton Turnage0b2becc2015-06-17 16:31:14 -0700162 self.url = 'http://localhost:%d' % port
163 if url_base:
164 chromedriver_args.append('--url-base=%s' % url_base)
165 self.url = urlparse.urljoin(self.url, url_base)
166
167 if extra_args:
168 chromedriver_args.extend(extra_args)
169
Ilja H. Friedela6245762015-06-09 15:15:34 -0700170 # TODO(ihf): Remove references to X after M45.
beeps47a51292013-11-20 08:55:23 -0800171 # Chromedriver will look for an X server running on the display
172 # specified through the DISPLAY environment variable.
173 os.environ['DISPLAY'] = X_SERVER_DISPLAY
174 os.environ['XAUTHORITY'] = X_AUTHORITY
175
Dan Shic1d263b2013-10-04 17:31:38 -0700176 self.bg_job = utils.BgJob(chromedriver_args, stderr_level=logging.DEBUG)
Dan Shic1d263b2013-10-04 17:31:38 -0700177 if self.bg_job is None:
178 raise RuntimeError('ChromeDriver server cannot be started')
179
180 try:
181 timeout_msg = 'Timeout on waiting for ChromeDriver to start.'
182 utils.poll_for_condition(self.is_running,
183 exception=utils.TimeoutError(timeout_msg),
184 timeout=10,
185 sleep_interval=.1)
186 except utils.TimeoutError:
187 self.close_bgjob()
188 raise RuntimeError('ChromeDriver server did not start')
189
190 logging.debug('Chrome Driver server is up and listening at port %d.',
191 port)
Resetswitchd1df3d52015-05-20 18:20:55 -0700192 if not skip_cleanup:
193 atexit.register(self.close)
Dan Shic1d263b2013-10-04 17:31:38 -0700194
195
196 def is_running(self):
197 """Returns whether the server is up and running."""
198 try:
199 urllib2.urlopen(self.url + '/status')
200 return True
201 except urllib2.URLError as e:
202 return False
203
204
205 def close_bgjob(self):
206 """Close background job and log stdout and stderr."""
207 utils.nuke_subprocess(self.bg_job.sp)
208 utils.join_bg_jobs([self.bg_job], timeout=1)
209 result = self.bg_job.result
210 if result.stdout or result.stderr:
211 logging.info('stdout of Chrome Driver:\n%s', result.stdout)
212 logging.error('stderr of Chrome Driver:\n%s', result.stderr)
213
214
215 def close(self):
216 """Kills the ChromeDriver server, if it is running."""
217 if self.bg_job is None:
218 return
219
220 try:
221 urllib2.urlopen(self.url + '/shutdown', timeout=10).close()
222 except:
223 pass
224
225 self.close_bgjob()