Skip to content

Commit

Permalink
[rb] nest service unit tests properly
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Dec 12, 2022
1 parent ebe1ec1 commit ab27dab
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 377 deletions.
156 changes: 79 additions & 77 deletions rb/spec/unit/selenium/webdriver/chrome/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,116 +21,118 @@

module Selenium
module WebDriver
describe Service do
describe '#new' do
let(:service_path) { "/path/to/#{Chrome::Service::EXECUTABLE}" }
module Chrome
describe Service do
describe '#new' do
let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }

before do
allow(Platform).to receive(:assert_executable).and_return(true)
end
before do
allow(Platform).to receive(:assert_executable).and_return(true)
end

after { Chrome::Service.driver_path = nil }
after { Service.driver_path = nil }

it 'uses default path and port' do
allow(Platform).to receive(:find_binary).and_return(service_path)
it 'uses default path and port' do
allow(Platform).to receive(:find_binary).and_return(service_path)

service = Service.chrome
service = Service.chrome

expect(service.executable_path).to include Chrome::Service::EXECUTABLE
expected_port = Chrome::Service::DEFAULT_PORT
expect(service.port).to eq expected_port
expect(service.host).to eq Platform.localhost
end
expect(service.executable_path).to include Service::EXECUTABLE
expected_port = Service::DEFAULT_PORT
expect(service.port).to eq expected_port
expect(service.host).to eq Platform.localhost
end

it 'uses provided path and port' do
path = 'foo'
port = 5678
it 'uses provided path and port' do
path = 'foo'
port = 5678

service = Service.chrome(path: path, port: port)
service = Service.chrome(path: path, port: port)

expect(service.executable_path).to eq path
expect(service.port).to eq port
expect(service.host).to eq Platform.localhost
end
expect(service.executable_path).to eq path
expect(service.port).to eq port
expect(service.host).to eq Platform.localhost
end

it 'allows #driver_path= with String value' do
path = '/path/to/driver'
Chrome::Service.driver_path = path
it 'allows #driver_path= with String value' do
path = '/path/to/driver'
Service.driver_path = path

service = Service.chrome
service = Service.chrome

expect(service.executable_path).to eq path
end
expect(service.executable_path).to eq path
end

it 'allows #driver_path= with Proc value' do
path = '/path/to/driver'
proc = proc { path }
Chrome::Service.driver_path = proc
it 'allows #driver_path= with Proc value' do
path = '/path/to/driver'
proc = proc { path }
Service.driver_path = proc

service = Service.chrome
service = Service.chrome

expect(service.executable_path).to eq path
end
expect(service.executable_path).to eq path
end

it 'does not create args by default' do
allow(Platform).to receive(:find_binary).and_return(service_path)
it 'does not create args by default' do
allow(Platform).to receive(:find_binary).and_return(service_path)

service = Service.chrome
service = Service.new

expect(service.extra_args).to be_empty
end
expect(service.extra_args).to be_empty
end

it 'uses provided args' do
allow(Platform).to receive(:find_binary).and_return(service_path)
it 'uses provided args' do
allow(Platform).to receive(:find_binary).and_return(service_path)

service = Service.chrome(args: ['--foo', '--bar'])
service = Service.chrome(args: ['--foo', '--bar'])

expect(service.extra_args).to eq ['--foo', '--bar']
end
expect(service.extra_args).to eq ['--foo', '--bar']
end

it 'uses args when passed in as a Hash' do
allow(Platform).to receive(:find_binary).and_return(service_path)
it 'uses args when passed in as a Hash' do
allow(Platform).to receive(:find_binary).and_return(service_path)

expect {
service = Service.chrome(args: {log_path: '/path/to/log',
verbose: true})
expect {
service = Service.new(args: {log_path: '/path/to/log',
verbose: true})

expect(service.extra_args).to eq ['--log-path=/path/to/log', '--verbose']
}.to have_deprecated(:driver_opts)
expect(service.extra_args).to eq ['--log-path=/path/to/log', '--verbose']
}.to have_deprecated(:driver_opts)
end
end
end

context 'when initializing driver' do
let(:driver) { Chrome::Driver }
let(:service) { instance_double(Service, launch: service_manager) }
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }
context 'when initializing driver' do
let(:driver) { Chrome::Driver }
let(:service) { instance_double(Service, launch: service_manager) }
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }

before do
allow(Remote::Bridge).to receive(:new).and_return(bridge)
allow(bridge).to receive(:browser).and_return(:chrome)
end
before do
allow(Remote::Bridge).to receive(:new).and_return(bridge)
allow(bridge).to receive(:browser).and_return(:chrome)
end

it 'is not created when :url is provided' do
expect(Service).not_to receive(:new)
it 'is not created when :url is provided' do
expect(Service).not_to receive(:new)

driver.new(url: 'http://example.com:4321')
end
driver.new(url: 'http://example.com:4321')
end

it 'is created when :url is not provided' do
allow(Service).to receive(:new).and_return(service)
it 'is created when :url is not provided' do
allow(Service).to receive(:new).and_return(service)

driver.new
expect(Service).to have_received(:new).with(no_args)
end
driver.new
expect(Service).to have_received(:new).with(no_args)
end

it 'accepts :service without creating a new instance' do
allow(Service).to receive(:new)
it 'accepts :service without creating a new instance' do
allow(Service).to receive(:new)

driver.new(service: service)
expect(Service).not_to have_received(:new)
driver.new(service: service)
expect(Service).not_to have_received(:new)
end
end
end
end
end # Chrome
end # WebDriver
end # Selenium
36 changes: 36 additions & 0 deletions rb/spec/unit/selenium/webdriver/common/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ module WebDriver
stub_const('Selenium::WebDriver::Service::EXECUTABLE', 'service')
end

describe 'browser shortcuts' do
before { allow(Platform).to receive(:find_binary).and_return(service_path) }

let(:args) { %w[--foo --bar] }

it 'creates Chrome instance' do
service = Service.chrome(args: args)
expect(service).to be_a(Chrome::Service)
expect(service.args).to eq args
end

it 'creates Edge instance' do
service = Service.edge(args: args)
expect(service).to be_a(Edge::Service)
expect(service.args).to eq args
end

it 'creates Firefox instance' do
service = Service.firefox(args: args)
expect(service).to be_a(Firefox::Service)
expect(service.args).to eq args
end

it 'creates IE instance' do
service = Service.internet_explorer(args: args)
expect(service).to be_a(IE::Service)
expect(service.args).to eq args
end

it 'creates Safari instance' do
service = Service.safari(args: args)
expect(service).to be_a(Safari::Service)
expect(service.args).to eq args
end
end

describe '#new' do
it 'uses default path and port' do
allow(Platform).to receive(:find_binary).and_return(service_path)
Expand Down
Loading

0 comments on commit ab27dab

Please sign in to comment.