| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Launches and kills ChromeDriver. | 6 """Launches and kills ChromeDriver. |
| 7 | 7 |
| 8 For ChromeDriver documentation, refer to: | 8 For ChromeDriver documentation, refer to: |
| 9 http://dev.chromium.org/developers/testing/webdriver-for-chrome | 9 http://dev.chromium.org/developers/testing/webdriver-for-chrome |
| 10 """ | 10 """ |
| 11 | 11 |
| 12 import logging | 12 import logging |
| 13 import os | 13 import os |
| 14 import platform | 14 import platform |
| 15 import signal | 15 import signal |
| 16 import subprocess | 16 import subprocess |
| 17 import sys | 17 import sys |
| 18 import threading | 18 import threading |
| 19 import urllib2 | 19 import urllib2 |
| 20 | 20 |
| 21 | 21 |
| 22 class ChromeDriverLauncher: | 22 class ChromeDriverLauncher: |
| 23 """Launches and kills the ChromeDriver process.""" | 23 """Launches and kills the ChromeDriver process.""" |
| 24 | 24 |
| 25 def __init__(self, exe_path=None, root_path=None, port=None, url_base=None): | 25 def __init__(self, exe_path=None, root_path=None, port=None, url_base=None, |
| 26 use_native_events=False): |
| 26 """Initializes a new launcher. | 27 """Initializes a new launcher. |
| 27 | 28 |
| 28 Args: | 29 Args: |
| 29 exe_path: path to the ChromeDriver executable | 30 exe_path: path to the ChromeDriver executable |
| 30 root_path: base path from which ChromeDriver webserver will serve files | 31 root_path: base path from which ChromeDriver webserver will serve files |
| 31 port: port that ChromeDriver will listen on | 32 port: port that ChromeDriver will listen on |
| 32 url_base: base URL which ChromeDriver webserver will listen from | 33 url_base: base URL which ChromeDriver webserver will listen from |
| 33 """ | 34 """ |
| 34 self._exe_path = exe_path | 35 self._exe_path = exe_path |
| 35 self._root_path = root_path | 36 self._root_path = root_path |
| 36 self._port = port | 37 self._port = port |
| 37 self._url_base = url_base | 38 self._url_base = url_base |
| 39 self._use_native_events = use_native_events |
| 38 if self._exe_path is None: | 40 if self._exe_path is None: |
| 39 self._exe_path = ChromeDriverLauncher.LocateExe() | 41 self._exe_path = ChromeDriverLauncher.LocateExe() |
| 40 if self._exe_path is None: | 42 if self._exe_path is None: |
| 41 raise RuntimeError('ChromeDriver exe could not be found in its default ' | 43 raise RuntimeError('ChromeDriver exe could not be found in its default ' |
| 42 'location. Searched in following directories: ' + | 44 'location. Searched in following directories: ' + |
| 43 ', '.join(self.DefaultExeLocations())) | 45 ', '.join(self.DefaultExeLocations())) |
| 44 if self._root_path is None: | 46 if self._root_path is None: |
| 45 self._root_path = '.' | 47 self._root_path = '.' |
| 46 self._root_path = os.path.abspath(self._root_path) | 48 self._root_path = os.path.abspath(self._root_path) |
| 47 self._process = None | 49 self._process = None |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 started_event.release() | 128 started_event.release() |
| 127 | 129 |
| 128 if self._process is not None: | 130 if self._process is not None: |
| 129 self.Kill() | 131 self.Kill() |
| 130 | 132 |
| 131 chromedriver_args = [self._exe_path, '--root=%s' % self._root_path] | 133 chromedriver_args = [self._exe_path, '--root=%s' % self._root_path] |
| 132 if self._port is not None: | 134 if self._port is not None: |
| 133 chromedriver_args += ['--port=%d' % self._port] | 135 chromedriver_args += ['--port=%d' % self._port] |
| 134 if self._url_base is not None: | 136 if self._url_base is not None: |
| 135 chromedriver_args += ['--url-base=%s' % self._url_base] | 137 chromedriver_args += ['--url-base=%s' % self._url_base] |
| 138 if self._use_native_events: |
| 139 chromedriver_args += ['--native-events'] |
| 136 proc = subprocess.Popen(chromedriver_args, | 140 proc = subprocess.Popen(chromedriver_args, |
| 137 stdout=subprocess.PIPE) | 141 stdout=subprocess.PIPE) |
| 138 if proc is None: | 142 if proc is None: |
| 139 raise RuntimeError('ChromeDriver cannot be started') | 143 raise RuntimeError('ChromeDriver cannot be started') |
| 140 self._process = proc | 144 self._process = proc |
| 141 | 145 |
| 142 # Wait for ChromeDriver to be initialized before returning. | 146 # Wait for ChromeDriver to be initialized before returning. |
| 143 launch_result = {} | 147 launch_result = {} |
| 144 started_event = threading.Condition() | 148 started_event = threading.Condition() |
| 145 started_event.acquire() | 149 started_event.acquire() |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 198 |
| 195 def GetURL(self): | 199 def GetURL(self): |
| 196 url = 'http://localhost:' + str(self._port) | 200 url = 'http://localhost:' + str(self._port) |
| 197 if self._url_base: | 201 if self._url_base: |
| 198 url += self._url_base | 202 url += self._url_base |
| 199 return url | 203 return url |
| 200 | 204 |
| 201 def GetPort(self): | 205 def GetPort(self): |
| 202 return self._port | 206 return self._port |
| 203 | 207 |
| 208 def GetUseNativeEvents(self): |
| 209 return self._use_native_events |
| 210 |
| 204 def __del__(self): | 211 def __del__(self): |
| 205 self.Kill() | 212 self.Kill() |
| OLD | NEW |