Skip to content

Commit 03d36c9

Browse files
committed
Add --websocket-port parameter to firefox service
1 parent 1cfa0a0 commit 03d36c9

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

rb/lib/selenium/webdriver/firefox/service.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class Service < WebDriver::Service
2525
EXECUTABLE = 'geckodriver'
2626
SHUTDOWN_SUPPORTED = false
2727
DRIVER_PATH_ENV_KEY = 'SE_GECKODRIVER'
28+
29+
def initialize(path: nil, port: nil, log: nil, args: nil)
30+
args ||= []
31+
unless args.any? { |arg| arg.include?('--connect-existing') }
32+
args << '--websocket-port'
33+
args << Support::Sockets.free_port.to_s
34+
end
35+
super
36+
end
2837
end # Service
2938
end # Firefox
3039
end # WebDriver

rb/lib/selenium/webdriver/support.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
require 'selenium/webdriver/support/select'
2525
require 'selenium/webdriver/support/color'
2626
require 'selenium/webdriver/support/relative_locator'
27+
require 'selenium/webdriver/support/sockets'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
require 'socket'
21+
22+
module Selenium
23+
module WebDriver
24+
module Support
25+
module Sockets
26+
def self.free_port
27+
server = TCPServer.new('127.0.0.1', 0)
28+
port = server.addr[1]
29+
server.close
30+
port
31+
end
32+
end # Sockets
33+
end # Support
34+
end # WebDriver
35+
end # Selenium
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Selenium
2+
module WebDriver
3+
module Support
4+
module Sockets
5+
def self.free_port: -> Integer
6+
end
7+
end
8+
end
9+
end

rb/spec/integration/selenium/webdriver/firefox/service_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ module Firefox
3737
it 'can be started outside driver' do
3838
expect(service_manager.uri).to be_a(URI)
3939
end
40+
41+
context 'with BiDi enabled (websocket port)' do
42+
it 'ensures two service instances use different websocket port' do
43+
service1 = described_class.new
44+
service2 = described_class.new
45+
46+
ws_index1 = service1.args.index('--websocket-port')
47+
ws_index2 = service2.args.index('--websocket-port')
48+
49+
port1 = service1.args[ws_index1 + 1].to_i
50+
port2 = service2.args[ws_index2 + 1].to_i
51+
52+
expect(port1).not_to eq(port2)
53+
end
54+
end
4055
end
4156
end # Firefox
4257
end # WebDriver

rb/spec/unit/selenium/webdriver/firefox/service_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module Firefox
5151
it 'does not create args by default' do
5252
service = described_class.new
5353

54-
expect(service.extra_args).to be_empty
54+
expect(service.extra_args.count).to eq 2
5555
end
5656

5757
it 'uses sets log path to stdout' do
@@ -73,17 +73,32 @@ module Firefox
7373
end
7474

7575
it 'uses provided args' do
76-
service = described_class.new(args: ['--foo', '--bar'])
76+
service = described_class.new(args: %w[--foo --bar])
77+
expect(service.extra_args).to include(*%w[--foo --bar])
78+
end
79+
80+
context 'with websocket parameter' do
81+
it 'validates the websocket parameter is present and there is a random port' do
82+
service = described_class.new
83+
ws_index = service.extra_args.index('--websocket-port')
84+
port = service.extra_args[ws_index + 1].to_i
85+
expect(port).to be > 0
86+
expect(port).to be < 65_536
87+
end
7788

78-
expect(service.extra_args).to eq ['--foo', '--bar']
89+
it 'validates with --connect-existing that there is no --websocket-port and the port is not random' do
90+
service = described_class.new(args: ['--connect-existing'])
91+
expect(service.extra_args).not_to include('--websocket-port')
92+
expect(service.extra_args).to eq(['--connect-existing'])
93+
end
7994
end
8095
end
8196

8297
context 'when initializing driver' do
8398
let(:driver) { Firefox::Driver }
8499
let(:service) do
85100
instance_double(described_class, launch: service_manager, executable_path: nil, 'executable_path=': nil,
86-
class: described_class)
101+
class: described_class)
87102
end
88103
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
89104
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }

0 commit comments

Comments
 (0)