Skip to content

Commit ab27dab

Browse files
committed
[rb] nest service unit tests properly
1 parent ebe1ec1 commit ab27dab

File tree

6 files changed

+423
-377
lines changed

6 files changed

+423
-377
lines changed

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

Lines changed: 79 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -21,116 +21,118 @@
2121

2222
module Selenium
2323
module WebDriver
24-
describe Service do
25-
describe '#new' do
26-
let(:service_path) { "/path/to/#{Chrome::Service::EXECUTABLE}" }
24+
module Chrome
25+
describe Service do
26+
describe '#new' do
27+
let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }
2728

28-
before do
29-
allow(Platform).to receive(:assert_executable).and_return(true)
30-
end
29+
before do
30+
allow(Platform).to receive(:assert_executable).and_return(true)
31+
end
3132

32-
after { Chrome::Service.driver_path = nil }
33+
after { Service.driver_path = nil }
3334

34-
it 'uses default path and port' do
35-
allow(Platform).to receive(:find_binary).and_return(service_path)
35+
it 'uses default path and port' do
36+
allow(Platform).to receive(:find_binary).and_return(service_path)
3637

37-
service = Service.chrome
38+
service = Service.chrome
3839

39-
expect(service.executable_path).to include Chrome::Service::EXECUTABLE
40-
expected_port = Chrome::Service::DEFAULT_PORT
41-
expect(service.port).to eq expected_port
42-
expect(service.host).to eq Platform.localhost
43-
end
40+
expect(service.executable_path).to include Service::EXECUTABLE
41+
expected_port = Service::DEFAULT_PORT
42+
expect(service.port).to eq expected_port
43+
expect(service.host).to eq Platform.localhost
44+
end
4445

45-
it 'uses provided path and port' do
46-
path = 'foo'
47-
port = 5678
46+
it 'uses provided path and port' do
47+
path = 'foo'
48+
port = 5678
4849

49-
service = Service.chrome(path: path, port: port)
50+
service = Service.chrome(path: path, port: port)
5051

51-
expect(service.executable_path).to eq path
52-
expect(service.port).to eq port
53-
expect(service.host).to eq Platform.localhost
54-
end
52+
expect(service.executable_path).to eq path
53+
expect(service.port).to eq port
54+
expect(service.host).to eq Platform.localhost
55+
end
5556

56-
it 'allows #driver_path= with String value' do
57-
path = '/path/to/driver'
58-
Chrome::Service.driver_path = path
57+
it 'allows #driver_path= with String value' do
58+
path = '/path/to/driver'
59+
Service.driver_path = path
5960

60-
service = Service.chrome
61+
service = Service.chrome
6162

62-
expect(service.executable_path).to eq path
63-
end
63+
expect(service.executable_path).to eq path
64+
end
6465

65-
it 'allows #driver_path= with Proc value' do
66-
path = '/path/to/driver'
67-
proc = proc { path }
68-
Chrome::Service.driver_path = proc
66+
it 'allows #driver_path= with Proc value' do
67+
path = '/path/to/driver'
68+
proc = proc { path }
69+
Service.driver_path = proc
6970

70-
service = Service.chrome
71+
service = Service.chrome
7172

72-
expect(service.executable_path).to eq path
73-
end
73+
expect(service.executable_path).to eq path
74+
end
7475

75-
it 'does not create args by default' do
76-
allow(Platform).to receive(:find_binary).and_return(service_path)
76+
it 'does not create args by default' do
77+
allow(Platform).to receive(:find_binary).and_return(service_path)
7778

78-
service = Service.chrome
79+
service = Service.new
7980

80-
expect(service.extra_args).to be_empty
81-
end
81+
expect(service.extra_args).to be_empty
82+
end
8283

83-
it 'uses provided args' do
84-
allow(Platform).to receive(:find_binary).and_return(service_path)
84+
it 'uses provided args' do
85+
allow(Platform).to receive(:find_binary).and_return(service_path)
8586

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

88-
expect(service.extra_args).to eq ['--foo', '--bar']
89-
end
89+
expect(service.extra_args).to eq ['--foo', '--bar']
90+
end
9091

91-
it 'uses args when passed in as a Hash' do
92-
allow(Platform).to receive(:find_binary).and_return(service_path)
92+
it 'uses args when passed in as a Hash' do
93+
allow(Platform).to receive(:find_binary).and_return(service_path)
9394

94-
expect {
95-
service = Service.chrome(args: {log_path: '/path/to/log',
96-
verbose: true})
95+
expect {
96+
service = Service.new(args: {log_path: '/path/to/log',
97+
verbose: true})
9798

98-
expect(service.extra_args).to eq ['--log-path=/path/to/log', '--verbose']
99-
}.to have_deprecated(:driver_opts)
99+
expect(service.extra_args).to eq ['--log-path=/path/to/log', '--verbose']
100+
}.to have_deprecated(:driver_opts)
101+
end
100102
end
101-
end
102103

103-
context 'when initializing driver' do
104-
let(:driver) { Chrome::Driver }
105-
let(:service) { instance_double(Service, launch: service_manager) }
106-
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
107-
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }
104+
context 'when initializing driver' do
105+
let(:driver) { Chrome::Driver }
106+
let(:service) { instance_double(Service, launch: service_manager) }
107+
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
108+
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }
108109

109-
before do
110-
allow(Remote::Bridge).to receive(:new).and_return(bridge)
111-
allow(bridge).to receive(:browser).and_return(:chrome)
112-
end
110+
before do
111+
allow(Remote::Bridge).to receive(:new).and_return(bridge)
112+
allow(bridge).to receive(:browser).and_return(:chrome)
113+
end
113114

114-
it 'is not created when :url is provided' do
115-
expect(Service).not_to receive(:new)
115+
it 'is not created when :url is provided' do
116+
expect(Service).not_to receive(:new)
116117

117-
driver.new(url: 'http://example.com:4321')
118-
end
118+
driver.new(url: 'http://example.com:4321')
119+
end
119120

120-
it 'is created when :url is not provided' do
121-
allow(Service).to receive(:new).and_return(service)
121+
it 'is created when :url is not provided' do
122+
allow(Service).to receive(:new).and_return(service)
122123

123-
driver.new
124-
expect(Service).to have_received(:new).with(no_args)
125-
end
124+
driver.new
125+
expect(Service).to have_received(:new).with(no_args)
126+
end
126127

127-
it 'accepts :service without creating a new instance' do
128-
allow(Service).to receive(:new)
128+
it 'accepts :service without creating a new instance' do
129+
allow(Service).to receive(:new)
129130

130-
driver.new(service: service)
131-
expect(Service).not_to have_received(:new)
131+
driver.new(service: service)
132+
expect(Service).not_to have_received(:new)
133+
end
132134
end
133135
end
134-
end
136+
end # Chrome
135137
end # WebDriver
136138
end # Selenium

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,42 @@ module WebDriver
3030
stub_const('Selenium::WebDriver::Service::EXECUTABLE', 'service')
3131
end
3232

33+
describe 'browser shortcuts' do
34+
before { allow(Platform).to receive(:find_binary).and_return(service_path) }
35+
36+
let(:args) { %w[--foo --bar] }
37+
38+
it 'creates Chrome instance' do
39+
service = Service.chrome(args: args)
40+
expect(service).to be_a(Chrome::Service)
41+
expect(service.args).to eq args
42+
end
43+
44+
it 'creates Edge instance' do
45+
service = Service.edge(args: args)
46+
expect(service).to be_a(Edge::Service)
47+
expect(service.args).to eq args
48+
end
49+
50+
it 'creates Firefox instance' do
51+
service = Service.firefox(args: args)
52+
expect(service).to be_a(Firefox::Service)
53+
expect(service.args).to eq args
54+
end
55+
56+
it 'creates IE instance' do
57+
service = Service.internet_explorer(args: args)
58+
expect(service).to be_a(IE::Service)
59+
expect(service.args).to eq args
60+
end
61+
62+
it 'creates Safari instance' do
63+
service = Service.safari(args: args)
64+
expect(service).to be_a(Safari::Service)
65+
expect(service.args).to eq args
66+
end
67+
end
68+
3369
describe '#new' do
3470
it 'uses default path and port' do
3571
allow(Platform).to receive(:find_binary).and_return(service_path)

0 commit comments

Comments
 (0)