blob: a43453c1fd7a3185f0fbe18d3706135968523a4a [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=[],
Nathan Stoddardab583042014-08-11 10:41:47 -070031 extension_paths=[], is_component=True, username=None,
Payton Turnage0b2becc2015-06-17 16:31:14 -070032 password=None, server_port=None, skip_cleanup=False,
33 url_base=None, 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.
41 @param is_component: True if the manifest.json has a key.
Nathan Stoddardab583042014-08-11 10:41:47 -070042 @param username: Log in using this username instead of the default.
Resetswitch98a0f7d2015-05-04 14:48:08 -070043 @param password: Log in using this password instead of the default.
44 @param server_port: Port number for the chromedriver server. If None,
45 an available port is chosen at random.
Resetswitchd1df3d52015-05-20 18:20:55 -070046 @param skip_cleanup: If True, leave the server and browser running
47 so that remote tests can run after this script
48 ends. Default is False.
Payton Turnage0b2becc2015-06-17 16:31:14 -070049 @param url_base: Optional base url for chromedriver.
50 @param extra_chromedriver_args: List of extra arguments to forward to
51 the chromedriver binary, if any.
Dan Shic1d263b2013-10-04 17:31:38 -070052 """
Resetswitchd1df3d52015-05-20 18:20:55 -070053 self._cleanup = not skip_cleanup
Dan Shic1d263b2013-10-04 17:31:38 -070054 assert os.geteuid() == 0, 'Need superuser privileges'
55
56 # Log in with telemetry
beepsbff9f9d2013-12-06 11:14:08 -080057 self._chrome = chrome.Chrome(extension_paths=extension_paths,
58 is_component=is_component,
Nathan Stoddardab583042014-08-11 10:41:47 -070059 username=username,
60 password=password,
beepsbff9f9d2013-12-06 11:14:08 -080061 extra_browser_args=extra_chrome_flags)
62 self._browser = self._chrome.browser
Dan Shi2bff39b2014-03-28 11:36:13 -070063 # Close all tabs owned and opened by Telemetry, as these cannot be
64 # transferred to ChromeDriver.
65 self._browser.tabs[0].Close()
Dan Shic1d263b2013-10-04 17:31:38 -070066
67 # Start ChromeDriver server
Resetswitch98a0f7d2015-05-04 14:48:08 -070068 self._server = chromedriver_server(CHROMEDRIVER_EXE_PATH,
Resetswitchd1df3d52015-05-20 18:20:55 -070069 port=server_port,
Payton Turnage0b2becc2015-06-17 16:31:14 -070070 skip_cleanup=skip_cleanup,
71 url_base=url_base,
72 extra_args=extra_chromedriver_args)
Dan Shic1d263b2013-10-04 17:31:38 -070073
Dan Shi2bff39b2014-03-28 11:36:13 -070074 # Open a new tab using Chrome remote debugging. ChromeDriver expects
75 # a tab opened for remote to work. Tabs opened using Telemetry will be
76 # owned by Telemetry, and will be inaccessible to ChromeDriver.
77 urllib2.urlopen('http://localhost:%i/json/new' %
78 utils.get_chrome_remote_debugging_port())
79
Dan Shic1d263b2013-10-04 17:31:38 -070080 chromeOptions = {'debuggerAddress':
81 ('localhost:%d' %
82 utils.get_chrome_remote_debugging_port())}
83 capabilities = {'chromeOptions':chromeOptions}
84 # Handle to chromedriver, for chrome automation.
Dan Shi96c77cb2013-11-19 10:30:37 -080085 try:
86 self.driver = webdriver.Remote(command_executor=self._server.url,
87 desired_capabilities=capabilities)
88 except NameError:
89 logging.error('selenium module failed to be imported.')
90 raise
Dan Shic1d263b2013-10-04 17:31:38 -070091
92
93 def __enter__(self):
94 return self
95
96
97 def __exit__(self, *args):
98 """Clean up after running the test.
99
100 """
101 if hasattr(self, 'driver') and self.driver:
102 self.driver.close()
103 del self.driver
104
Resetswitchd1df3d52015-05-20 18:20:55 -0700105 if not hasattr(self, '_cleanup') or self._cleanup:
106 if hasattr(self, '_server') and self._server:
107 self._server.close()
108 del self._server
Dan Shic1d263b2013-10-04 17:31:38 -0700109
Resetswitchd1df3d52015-05-20 18:20:55 -0700110 if hasattr(self, '_browser') and self._browser:
111 self._browser.Close()
112 del self._browser
Dan Shic1d263b2013-10-04 17:31:38 -0700113
114
beepsbff9f9d2013-12-06 11:14:08 -0800115 def get_extension(self, extension_path):
116 """Gets an extension by proxying to the browser.
117
118 @param extension_path: Path to the extension loaded in the browser.
119
120 @return: A telemetry extension object representing the extension.
121 """
122 return self._chrome.get_extension(extension_path)
123
124
Dan Shic1d263b2013-10-04 17:31:38 -0700125class chromedriver_server(object):
126 """A running ChromeDriver server.
127
128 This code is migrated from chrome:
129 src/chrome/test/chromedriver/server/server.py
130 """
131
Payton Turnage0b2becc2015-06-17 16:31:14 -0700132 def __init__(self, exe_path, port=None, skip_cleanup=False,
133 url_base=None, extra_args=None):
Dan Shic1d263b2013-10-04 17:31:38 -0700134 """Starts the ChromeDriver server and waits for it to be ready.
135
136 Args:
137 exe_path: path to the ChromeDriver executable
Resetswitch98a0f7d2015-05-04 14:48:08 -0700138 port: server port. If None, an available port is chosen at random.
Resetswitchd1df3d52015-05-20 18:20:55 -0700139 skip_cleanup: If True, leave the server running so that remote
140 tests can run after this script ends. Default is
141 False.
Payton Turnage0b2becc2015-06-17 16:31:14 -0700142 url_base: Optional base url for chromedriver.
143 extra_args: List of extra arguments to forward to the chromedriver
144 binary, if any.
Dan Shic1d263b2013-10-04 17:31:38 -0700145 Raises:
146 RuntimeError if ChromeDriver fails to start
147 """
148 if not os.path.exists(exe_path):
149 raise RuntimeError('ChromeDriver exe not found at: ' + exe_path)
150
Resetswitchd1df3d52015-05-20 18:20:55 -0700151 chromedriver_args = [exe_path]
152 if port:
153 # Allow remote connections if a port was specified
154 chromedriver_args.append('--whitelisted-ips')
155 else:
Resetswitch98a0f7d2015-05-04 14:48:08 -0700156 port = utils.get_unused_port()
Resetswitchd1df3d52015-05-20 18:20:55 -0700157 chromedriver_args.append('--port=%d' % port)
beeps47a51292013-11-20 08:55:23 -0800158
Payton Turnage0b2becc2015-06-17 16:31:14 -0700159 self.url = 'http://localhost:%d' % port
160 if url_base:
161 chromedriver_args.append('--url-base=%s' % url_base)
162 self.url = urlparse.urljoin(self.url, url_base)
163
164 if extra_args:
165 chromedriver_args.extend(extra_args)
166
Ilja H. Friedela6245762015-06-09 15:15:34 -0700167 # TODO(ihf): Remove references to X after M45.
beeps47a51292013-11-20 08:55:23 -0800168 # Chromedriver will look for an X server running on the display
169 # specified through the DISPLAY environment variable.
170 os.environ['DISPLAY'] = X_SERVER_DISPLAY
171 os.environ['XAUTHORITY'] = X_AUTHORITY
172
Dan Shic1d263b2013-10-04 17:31:38 -0700173 self.bg_job = utils.BgJob(chromedriver_args, stderr_level=logging.DEBUG)
Dan Shic1d263b2013-10-04 17:31:38 -0700174 if self.bg_job is None:
175 raise RuntimeError('ChromeDriver server cannot be started')
176
177 try:
178 timeout_msg = 'Timeout on waiting for ChromeDriver to start.'
179 utils.poll_for_condition(self.is_running,
180 exception=utils.TimeoutError(timeout_msg),
181 timeout=10,
182 sleep_interval=.1)
183 except utils.TimeoutError:
184 self.close_bgjob()
185 raise RuntimeError('ChromeDriver server did not start')
186
187 logging.debug('Chrome Driver server is up and listening at port %d.',
188 port)
Resetswitchd1df3d52015-05-20 18:20:55 -0700189 if not skip_cleanup:
190 atexit.register(self.close)
Dan Shic1d263b2013-10-04 17:31:38 -0700191
192
193 def is_running(self):
194 """Returns whether the server is up and running."""
195 try:
196 urllib2.urlopen(self.url + '/status')
197 return True
198 except urllib2.URLError as e:
199 return False
200
201
202 def close_bgjob(self):
203 """Close background job and log stdout and stderr."""
204 utils.nuke_subprocess(self.bg_job.sp)
205 utils.join_bg_jobs([self.bg_job], timeout=1)
206 result = self.bg_job.result
207 if result.stdout or result.stderr:
208 logging.info('stdout of Chrome Driver:\n%s', result.stdout)
209 logging.error('stderr of Chrome Driver:\n%s', result.stderr)
210
211
212 def close(self):
213 """Kills the ChromeDriver server, if it is running."""
214 if self.bg_job is None:
215 return
216
217 try:
218 urllib2.urlopen(self.url + '/shutdown', timeout=10).close()
219 except:
220 pass
221
222 self.close_bgjob()