OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import atexit | 5 import atexit |
6 import os | 6 import os |
7 import socket | 7 import socket |
8 import subprocess | 8 import subprocess |
9 import time | 9 import time |
10 import urllib2 | 10 import urllib2 |
11 | 11 |
12 | 12 |
13 class Server(object): | 13 class Server(object): |
14 """A running ChromeDriver server.""" | 14 """A running ChromeDriver server.""" |
15 | 15 |
16 def __init__(self, exe_path, log_path=None): | 16 def __init__(self, exe_path, log_path=None, verbose=True): |
17 """Starts the ChromeDriver server and waits for it to be ready. | 17 """Starts the ChromeDriver server and waits for it to be ready. |
18 | 18 |
19 Args: | 19 Args: |
20 exe_path: path to the ChromeDriver executable | 20 exe_path: path to the ChromeDriver executable |
21 log_path: path to the log file | 21 log_path: path to the log file |
22 Raises: | 22 Raises: |
23 RuntimeError if ChromeDriver fails to start | 23 RuntimeError if ChromeDriver fails to start |
24 """ | 24 """ |
25 if not os.path.exists(exe_path): | 25 if not os.path.exists(exe_path): |
26 raise RuntimeError('ChromeDriver exe not found at: ' + exe_path) | 26 raise RuntimeError('ChromeDriver exe not found at: ' + exe_path) |
27 | 27 |
28 port = self._FindOpenPort() | 28 port = self._FindOpenPort() |
29 chromedriver_args = [exe_path, '--port=%d' % port] | 29 chromedriver_args = [exe_path, '--port=%d' % port] |
30 if log_path: | 30 if log_path: |
31 chromedriver_args.extend(['--verbose', '--log-path=%s' % log_path]) | 31 chromedriver_args.extend(['--log-path=%s' %log_path]) |
| 32 if verbose: |
| 33 chromedriver_args.extend(['--verbose']) |
32 self._process = subprocess.Popen(chromedriver_args) | 34 self._process = subprocess.Popen(chromedriver_args) |
33 self._url = 'http://127.0.0.1:%d' % port | 35 self._url = 'http://127.0.0.1:%d' % port |
34 if self._process is None: | 36 if self._process is None: |
35 raise RuntimeError('ChromeDriver server cannot be started') | 37 raise RuntimeError('ChromeDriver server cannot be started') |
36 | 38 |
37 max_time = time.time() + 10 | 39 max_time = time.time() + 10 |
38 while not self.IsRunning(): | 40 while not self.IsRunning(): |
39 if time.time() > max_time: | 41 if time.time() > max_time: |
40 self._process.terminate() | 42 self._process.terminate() |
41 raise RuntimeError('ChromeDriver server did not start') | 43 raise RuntimeError('ChromeDriver server did not start') |
(...skipping 24 matching lines...) Expand all Loading... |
66 """Kills the ChromeDriver server, if it is running.""" | 68 """Kills the ChromeDriver server, if it is running.""" |
67 if self._process is None: | 69 if self._process is None: |
68 return | 70 return |
69 | 71 |
70 try: | 72 try: |
71 urllib2.urlopen(self.GetUrl() + '/shutdown', timeout=10).close() | 73 urllib2.urlopen(self.GetUrl() + '/shutdown', timeout=10).close() |
72 except: | 74 except: |
73 self._process.terminate() | 75 self._process.terminate() |
74 self._process.wait() | 76 self._process.wait() |
75 self._process = None | 77 self._process = None |
OLD | NEW |