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
3 # found in the LICENSE file.
14 """A running ChromeDriver server."""
16 def __init__(self
, exe_path
, log_path
=None):
17 """Starts the ChromeDriver server and waits for it to be ready.
20 exe_path: path to the ChromeDriver executable
21 log_path: path to the log file
23 RuntimeError if ChromeDriver fails to start
25 if not os
.path
.exists(exe_path
):
26 raise RuntimeError('ChromeDriver exe not found at: ' + exe_path
)
28 port
= self
._FindOpenPort
()
29 chromedriver_args
= [exe_path
, '--port=%d' % port
]
31 chromedriver_args
.extend(['--verbose', '--log-path=%s' % log_path
])
32 self
._process
= subprocess
.Popen(chromedriver_args
)
33 self
._url
= 'http://127.0.0.1:%d' % port
34 if self
._process
is None:
35 raise RuntimeError('ChromeDriver server cannot be started')
37 max_time
= time
.time() + 10
38 while not self
.IsRunning():
39 if time
.time() > max_time
:
40 self
._process
.terminate()
41 raise RuntimeError('ChromeDriver server did not start')
44 atexit
.register(self
.Kill
)
46 def _FindOpenPort(self
):
47 for port
in range(9500, 10000):
49 socket
.create_connection(('127.0.0.1', port
), 0.2).close()
52 raise RuntimeError('Cannot find open port to launch ChromeDriver')
58 """Returns whether the server is up and running."""
60 urllib2
.urlopen(self
.GetUrl() + '/status')
62 except urllib2
.URLError
:
66 """Kills the ChromeDriver server, if it is running."""
67 if self
._process
is None:
71 urllib2
.urlopen(self
.GetUrl() + '/shutdown', timeout
=10).close()
73 self
._process
.terminate()