Skip to content

Commit

Permalink
[rb] remove support for capabilities in local drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Aug 16, 2023
1 parent e02dd2e commit d9bd8e3
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 515 deletions.
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/chrome/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module Chrome
class Driver < Chromium::Driver
include LocalDriver

def initialize(capabilities: nil, options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(capabilities, options, service, url)
def initialize(options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(options, service, url)
super(caps: caps, url: url, **opts)
end

Expand Down
12 changes: 0 additions & 12 deletions rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,6 @@ def create_bridge(caps:, url:, http_client: nil)
end
end

def generate_capabilities(capabilities)
Array(capabilities).map { |cap|
if cap.is_a? Symbol
cap = WebDriver::Options.send(cap)
elsif !cap.respond_to? :as_json
msg = ":capabilities parameter only accepts objects responding to #as_json which #{cap.class} does not"
raise ArgumentError, msg
end
cap.as_json
}.inject(:merge)
end

def service_url(service)
@service_manager = service.launch
@service_manager.uri
Expand Down
25 changes: 8 additions & 17 deletions rb/lib/selenium/webdriver/common/local_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,26 @@
module Selenium
module WebDriver
module LocalDriver
def initialize_local_driver(capabilities, options, service, url)
def initialize_local_driver(options, service, url)
raise ArgumentError, "Can't initialize #{self.class} with :url" if url

service ||= Service.send(browser)
caps = process_options(options, capabilities, service)
caps = process_options(options, service)
url = service_url(service)

[caps, url]
end

def process_options(options, capabilities, service)
def process_options(options, service)
default_options = Options.send(browser)
options ||= default_options

if options && capabilities
msg = "Don't use both :options and :capabilities when initializing #{self.class}, prefer :options"
raise ArgumentError, msg
elsif options && !options.is_a?(default_options.class)
unless options.is_a?(default_options.class)
raise ArgumentError, ":options must be an instance of #{default_options.class}"
elsif capabilities
WebDriver.logger.deprecate("The :capabilities parameter for #{self.class}",
"an :options parameter instead to create a local #{self.class}",
id: :capabilities)
service.executable_path ||= WebDriver::DriverFinder.path(capabilities, service.class)
generate_capabilities(capabilities)
else
options ||= default_options
service.executable_path ||= WebDriver::DriverFinder.path(options, service.class)
options.as_json
end

service.executable_path ||= WebDriver::DriverFinder.path(options, service.class)
options.as_json
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/edge/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module Edge
class Driver < Chromium::Driver
include LocalDriver

def initialize(capabilities: nil, options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(capabilities, options, service, url)
def initialize(options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(options, service, url)
super(caps: caps, url: url, **opts)
end

Expand Down
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/firefox/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Driver < WebDriver::Driver

include LocalDriver

def initialize(capabilities: nil, options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(capabilities, options, service, url)
def initialize(options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(options, service, url)
super(caps: caps, url: url, **opts)
end

Expand Down
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/ie/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Driver < WebDriver::Driver

include LocalDriver

def initialize(capabilities: nil, options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(capabilities, options, service, url)
def initialize(options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(options, service, url)
super(caps: caps, url: url, **opts)
end

Expand Down
12 changes: 12 additions & 0 deletions rb/lib/selenium/webdriver/remote/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def process_options(options, capabilities)
end
options ? options.as_json : generate_capabilities(capabilities)
end

def generate_capabilities(capabilities)
Array(capabilities).map { |cap|
if cap.is_a? Symbol
cap = WebDriver::Options.send(cap)
elsif !cap.respond_to? :as_json
msg = ":capabilities parameter only accepts objects responding to #as_json which #{cap.class} does not"
raise ArgumentError, msg
end
cap.as_json
}.inject(:merge)
end
end # Driver
end # Remote
end # WebDriver
Expand Down
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/safari/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Driver < WebDriver::Driver

include LocalDriver

def initialize(capabilities: nil, options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(capabilities, options, service, url)
def initialize(options: nil, service: nil, url: nil, **opts)
caps, url = initialize_local_driver(options, service, url)
super(caps: caps, url: url, **opts)
end

Expand Down
110 changes: 0 additions & 110 deletions rb/spec/unit/selenium/webdriver/chrome/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,116 +88,6 @@ def expect_request(body: nil, endpoint: nil)
described_class.new(options: Options.firefox)
}.to raise_exception(ArgumentError, ':options must be an instance of Selenium::WebDriver::Chrome::Options')
end

it 'does not allow both Options and Capabilities' do
msg = "Don't use both :options and :capabilities when initializing Selenium::WebDriver::Chrome::Driver, " \
'prefer :options'
expect {
described_class.new(options: Options.new, capabilities: Remote::Capabilities.new(browser_name: 'chrome'))
}.to raise_exception(ArgumentError, msg)
end

context 'with :capabilities' do
before { allow(DriverFinder).to receive(:path) }

it 'accepts value as a Symbol' do
expect_request
expect { described_class.new(capabilities: :chrome) }.to have_deprecated(:capabilities)
end

it 'accepts constructed Capabilities with Snake Case as Symbols' do
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})

expect {
capabilities = Remote::Capabilities.new(browser_name: 'chrome', invalid: 'foobar')
expect { described_class.new(capabilities: capabilities) }.not_to raise_exception
}.to have_deprecated(:capabilities)
end

it 'accepts constructed Capabilities with Camel Case as Symbols' do
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})

expect {
capabilities = Remote::Capabilities.new(browserName: 'chrome', invalid: 'foobar')
expect { described_class.new(capabilities: capabilities) }.not_to raise_exception
}.to have_deprecated(:capabilities)
end

it 'accepts constructed Capabilities with Camel Case as Strings' do
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})

expect {
capabilities = Remote::Capabilities.new('browserName' => 'chrome', 'invalid' => 'foobar')
expect { described_class.new(capabilities: capabilities) }.not_to raise_exception
}.to have_deprecated(:capabilities)
end

context 'when value is an Array' do
let(:as_json_object) do
Class.new do
def as_json(*)
{'company:key': 'value'}
end
end
end

it 'with Options instance' do
options = Options.new(args: ['-f'])
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome',
'goog:chromeOptions': {args: ['-f']}}}})

expect {
expect {
described_class.new(capabilities: [options])
}.not_to raise_exception
}.to have_deprecated(:capabilities)
end

it 'with Options instance with profile' do
profile = Profile.new.tap(&:layout_on_disk)
allow(profile).to receive(:directory).and_return('PROF_DIR')
options = Options.new(profile: profile)

expect_request(body: {capabilities:
{alwaysMatch: {browserName: 'chrome',
'goog:chromeOptions': {args: ['--user-data-dir=PROF_DIR']}}}})

expect {
expect {
described_class.new(capabilities: [options])
}.not_to raise_exception
}.to have_deprecated(:capabilities)
end

it 'with Capabilities instance' do
capabilities = Remote::Capabilities.new(browser_name: 'chrome', invalid: 'foobar')
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})

expect { described_class.new(capabilities: [capabilities]) }.to have_deprecated(:capabilities)
end

it 'with Options instance and an instance of a custom object responding to #as_json' do
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome',
'goog:chromeOptions': {},
'company:key': 'value'}}})
expect {
described_class.new(capabilities: [Options.new, as_json_object.new])
}.to have_deprecated(:capabilities)
end

it 'with Options instance, Capabilities instance and instance of a custom object responding to #as_json' do
capabilities = Remote::Capabilities.new(browser_name: 'chrome', invalid: 'foobar')
options = Options.new(args: ['-f'])
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar',
'goog:chromeOptions': {args: ['-f']},
'company:key': 'value'}}})

expect {
described_class.new(capabilities: [capabilities, options, as_json_object.new])
}.to have_deprecated(:capabilities)
end
end
end
end
end # Chrome
end # WebDriver
Expand Down
99 changes: 0 additions & 99 deletions rb/spec/unit/selenium/webdriver/edge/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,105 +94,6 @@ def expect_request(body: nil, endpoint: nil)
described_class.new(options: Options.chrome)
}.to raise_exception(ArgumentError, ':options must be an instance of Selenium::WebDriver::Edge::Options')
end

it 'does not allow both Options and Capabilities' do
msg = "Don't use both :options and :capabilities when initializing Selenium::WebDriver::Edge::Driver, " \
'prefer :options'
expect {
described_class.new(options: Options.new, capabilities: Remote::Capabilities.new(browser_name: 'msedge'))
}.to raise_exception(ArgumentError, msg)
end

context 'with :capabilities' do
before { allow(DriverFinder).to receive(:path) }

it 'accepts value as a Symbol' do
expect_request
expect {
expect {
described_class.new(capabilities: :edge)
}.to have_deprecated(:capabilities)
}.not_to raise_exception
end

it 'accepts constructed Capabilities with Snake Case as Symbols' do
capabilities = Remote::Capabilities.new(browser_name: 'MicrosoftEdge', invalid: 'foobar')
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge', invalid: 'foobar'}}})

expect { described_class.new(capabilities: capabilities) }.to have_deprecated(:capabilities)
end

it 'accepts constructed Capabilities with Camel Case as Symbols' do
capabilities = Remote::Capabilities.new(browserName: 'MicrosoftEdge', invalid: 'foobar')
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge', invalid: 'foobar'}}})

expect { described_class.new(capabilities: capabilities) }.to have_deprecated(:capabilities)
end

it 'accepts constructed Capabilities with Camel Case as Strings' do
capabilities = Remote::Capabilities.new('browserName' => 'MicrosoftEdge', 'invalid' => 'foobar')
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge', invalid: 'foobar'}}})

expect { described_class.new(capabilities: capabilities) }.to have_deprecated(:capabilities)
end

context 'when value is an Array' do
let(:as_json_object) do
Class.new do
def as_json(*)
{'company:key': 'value'}
end
end
end

it 'with Options instance' do
options = Options.new(args: ['-f'])
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge',
'ms:edgeOptions': {args: ['-f']}}}})

expect { described_class.new(capabilities: [options]) }.to have_deprecated(:capabilities)
end

it 'with Options instance with profile' do
profile = Profile.new.tap(&:layout_on_disk)
allow(profile).to receive(:directory).and_return('PROF_DIR')
options = Options.new(profile: profile)
expect_request(body: {capabilities:
{alwaysMatch: {browserName: 'MicrosoftEdge',
'ms:edgeOptions': {args: ['--user-data-dir=PROF_DIR']}}}})

expect { described_class.new(capabilities: [options]) }.to have_deprecated(:capabilities)
end

it 'with Capabilities instance' do
capabilities = Remote::Capabilities.new(browser_name: 'MicrosoftEdge', invalid: 'foobar')
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge', invalid: 'foobar'}}})

expect { described_class.new(capabilities: [capabilities]) }.to have_deprecated(:capabilities)
end

it 'with Options instance and an instance of a custom object responding to #as_json' do
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge',
'ms:edgeOptions': {},
'company:key': 'value'}}})
expect {
described_class.new(capabilities: [Options.new, as_json_object.new])
}.to have_deprecated(:capabilities)
end

it 'with Options instance, Capabilities instance and instance of a custom object responding to #as_json' do
capabilities = Remote::Capabilities.new(browser_name: 'MicrosoftEdge', invalid: 'foobar')
options = Options.new(args: ['-f'])
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'MicrosoftEdge', invalid: 'foobar',
'ms:edgeOptions': {args: ['-f']},
'company:key': 'value'}}})

expect {
described_class.new(capabilities: [capabilities, options, as_json_object.new])
}.to have_deprecated(:capabilities)
end
end
end
end
end # Chrome
end # WebDriver
Expand Down
Loading

0 comments on commit d9bd8e3

Please sign in to comment.