Skip to content

Commit 08ee2d9

Browse files
committed
[rb] process url in driver constructors
local drivers obtain url from service objects, remote driver obtains url from user, and neither should be passed the other's argument
1 parent 9776b0b commit 08ee2d9

12 files changed

Lines changed: 58 additions & 23 deletions

File tree

rb/lib/selenium/webdriver/chrome/driver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ module Chrome
2929
#
3030

3131
class Driver < Chromium::Driver
32+
def initialize(service: nil, url: nil, **opts)
33+
raise ArgumentError, "Can't initialize #{self.class} with :url" if url
34+
35+
url = service_url(service || Service.chrome)
36+
super(url: url, **opts)
37+
end
38+
3239
def browser
3340
:chrome
3441
end

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,8 @@ def ref
308308

309309
attr_reader :bridge
310310

311-
def create_bridge(capabilities: nil, options: nil, url: nil, service: nil, http_client: nil)
312-
Remote::Bridge.new(http_client: http_client,
313-
url: url || service_url(service)).tap do |bridge|
311+
def create_bridge(capabilities: nil, options: nil, url: nil, http_client: nil)
312+
Remote::Bridge.new(http_client: http_client, url: url).tap do |bridge|
314313
generated_caps = options ? options.as_json : generate_capabilities(capabilities)
315314
bridge.create_session(generated_caps)
316315
end
@@ -329,7 +328,6 @@ def generate_capabilities(capabilities)
329328
end
330329

331330
def service_url(service)
332-
service ||= Service.send(browser)
333331
@service_manager = service.launch
334332
@service_manager.uri
335333
end

rb/lib/selenium/webdriver/edge/driver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ module Edge
2929
#
3030

3131
class Driver < Chromium::Driver
32+
def initialize(service: nil, url: nil, **opts)
33+
raise ArgumentError, "Can't initialize #{self.class} with :url" if url
34+
35+
url = service_url(service || Service.edge)
36+
super(url: url, **opts)
37+
end
38+
3239
def browser
3340
:edge
3441
end

rb/lib/selenium/webdriver/firefox/driver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class Driver < WebDriver::Driver
3737
DriverExtensions::HasWebStorage,
3838
DriverExtensions::PrintsPage].freeze
3939

40+
def initialize(service: nil, url: nil, **opts)
41+
raise ArgumentError, "Can't initialize #{self.class} with :url" if url
42+
43+
url = service_url(service || Service.firefox)
44+
super(url: url, **opts)
45+
end
46+
4047
def browser
4148
:firefox
4249
end

rb/lib/selenium/webdriver/ie/driver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ module IE
3030
class Driver < WebDriver::Driver
3131
EXTENSIONS = [DriverExtensions::HasWebStorage].freeze
3232

33+
def initialize(service: nil, url: nil, **opts)
34+
raise ArgumentError, "Can't initialize #{self.class} with :url" if url
35+
36+
url = service_url(service || Service.ie)
37+
super(url: url, **opts)
38+
end
39+
3340
def browser
3441
:internet_explorer
3542
end

rb/lib/selenium/webdriver/remote/driver.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ class Driver < WebDriver::Driver
3030
include DriverExtensions::UploadsFiles
3131
include DriverExtensions::HasSessionId
3232

33-
def initialize(bridge: nil, listener: nil, **opts)
34-
opts[:url] ||= "http://#{Platform.localhost}:4444/wd/hub"
35-
super
33+
def initialize(service: nil, url: nil, **opts)
34+
raise ArgumentError, "Can not set :service object on #{self.class}" if service
35+
36+
url ||= "http://#{Platform.localhost}:4444/wd/hub"
37+
super(url: url, **opts)
3638
@bridge.file_detector = ->((filename, *)) { File.exist?(filename) && filename.to_s }
3739
end
3840

rb/lib/selenium/webdriver/safari/driver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class Driver < WebDriver::Driver
3131
DriverExtensions::HasApplePermissions,
3232
DriverExtensions::HasWebStorage].freeze
3333

34+
def initialize(service: nil, url: nil, **opts)
35+
raise ArgumentError, "Can't initialize #{self.class} with :url" if url
36+
37+
url = service_url(service || Service.safari)
38+
super(url: url, **opts)
39+
end
40+
3441
def browser
3542
:safari
3643
end

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ module Chrome
112112
allow(bridge).to receive(:browser).and_return(:chrome)
113113
end
114114

115-
it 'is not created when :url is provided' do
116-
expect(Service).not_to receive(:new)
117-
118-
driver.new(url: 'http://example.com:4321')
115+
it 'errors when :url is provided' do
116+
expect {
117+
driver.new(url: 'http://example.com:4321')
118+
}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Chrome::Driver with :url")
119119
end
120120

121121
it 'is created when :url is not provided' do

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ module Edge
119119
it 'is not created when :url is provided' do
120120
expect(Service).not_to receive(:new)
121121

122-
driver.new(url: 'http://example.com:4321')
122+
expect {
123+
driver.new(url: 'http://example.com:4321')
124+
}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Edge::Driver with :url")
123125
end
124126

125127
it 'is created when :url is not provided' do

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ module Firefox
109109
end
110110

111111
it 'is not created when :url is provided' do
112-
expect(Service).not_to receive(:new)
113-
114-
driver.new(url: 'http://example.com:4321')
112+
expect {
113+
driver.new(url: 'http://example.com:4321')
114+
}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Firefox::Driver with :url")
115115
end
116116

117117
it 'is created when :url is not provided' do

0 commit comments

Comments
 (0)