Skip to content

Commit 94a787f

Browse files
committed
rb: Allow to start multiple Safari drivers in parallel
1 parent 5b69f9d commit 94a787f

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

rb/lib/selenium/webdriver/safari/bridge.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def camel_case(str)
104104

105105
def prepare_connect_file
106106
# TODO: use tempfile?
107-
path = File.join(Dir.tmpdir, "safaridriver-#{Time.now.to_i}.html")
107+
path = File.join(Dir.tmpdir, "safaridriver-#{Time.now.usec}.html")
108108

109109
File.open(path, 'w') do |io|
110110
io << "<!DOCTYPE html><script>window.location = '#{@server.uri}';</script>"

rb/lib/selenium/webdriver/safari/browser.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module Safari
2323

2424
class Browser
2525
def start(*args)
26+
Platform.exit_hook { stop } # make sure we don't leave the browser running
27+
2628
@process = ChildProcess.new(Safari.path, *args)
2729
@process.io.inherit! if $DEBUG
2830
@process.start

rb/lib/selenium/webdriver/safari/options.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module Selenium
2121
module WebDriver
2222
module Safari
2323
class Options
24+
DEFAULT_PORT = 56485
25+
2426
attr_accessor :port, :data_dir, :skip_extension_installation
2527

2628
def initialize(opts = {})
@@ -40,16 +42,16 @@ def to_capabilities
4042

4143
def as_json
4244
{
43-
'port' => port,
44-
'dataDir' => data_dir,
45-
'cleanSession' => clean_session?,
45+
'port' => port,
46+
'dataDir' => data_dir,
47+
'cleanSession' => clean_session?,
4648
}
4749
end
4850

4951
private
5052

5153
def extract_options(opts)
52-
@port = Integer(opts[:port] || PortProber.random)
54+
@port = Integer(opts[:port] || DEFAULT_PORT)
5355
@data_dir = opts[:custom_data_dir] || opts[:data_dir]
5456
@clean_session = opts[:clean_session]
5557
end

rb/lib/selenium/webdriver/safari/server.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ module WebDriver
2222
module Safari
2323

2424
class Server
25+
SOCKET_LOCK_TIMEOUT = 45
26+
2527
def initialize(port, command_timeout)
26-
@port = port
28+
@port = port
2729
@command_timeout = command_timeout
2830
end
2931

3032
def start
31-
@server = TCPServer.new(Platform.localhost, @port)
33+
Platform.exit_hook { stop } # make sure we don't leave the server running
34+
35+
socket_lock.locked do
36+
find_free_port
37+
start_server
38+
end
3239
end
3340

3441
def stop
@@ -158,6 +165,20 @@ def encode_form_component(str)
158165
end
159166
end
160167

168+
private
169+
170+
def start_server
171+
@server = TCPServer.new(Platform.localhost, @port)
172+
end
173+
174+
def find_free_port
175+
@port = PortProber.above @port
176+
end
177+
178+
def socket_lock
179+
@socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
180+
end
181+
161182
end # Server
162183
end # Safari
163184
end # WebDriver

0 commit comments

Comments
 (0)