Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb
4135 views
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 File.expand_path('../spec_helper', __dir__)
21
22
module Selenium
23
module WebDriver
24
module Firefox
25
describe Service do
26
describe '#new' do
27
let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }
28
29
before do
30
allow(Platform).to receive(:assert_executable)
31
allow(WebDriver.logger).to receive(:debug?).and_return(false)
32
end
33
34
it 'uses default port and nil path' do
35
service = described_class.new
36
37
expect(service.port).to eq Service::DEFAULT_PORT
38
expect(service.host).to eq Platform.localhost
39
expect(service.executable_path).to be_nil
40
end
41
42
it 'uses provided path and port' do
43
path = 'foo'
44
port = 5678
45
46
service = described_class.new(path: path, port: port)
47
48
expect(service.executable_path).to eq path
49
expect(service.port).to eq port
50
end
51
52
it 'creates websocket args by default' do
53
service = described_class.new
54
55
expect(service.extra_args.count).to eq 2
56
end
57
58
it 'uses sets log path to stdout' do
59
service = described_class.new(log: :stdout)
60
61
expect(service.log).to eq $stdout
62
end
63
64
it 'uses sets log path to stderr' do
65
service = described_class.new(log: :stderr)
66
67
expect(service.log).to eq $stderr
68
end
69
70
it 'sets log path as file location' do
71
service = described_class.new(log: '/path/to/log.txt')
72
73
expect(service.log).to eq '/path/to/log.txt'
74
end
75
76
it 'uses provided args' do
77
service = described_class.new(args: %w[--foo --bar])
78
expect(service.extra_args).to include(*%w[--foo --bar])
79
end
80
81
it 'there is a zero port for websocket' 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_zero
86
end
87
88
context 'with connect existing' do
89
it 'does not uses websocket-port' 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
94
end
95
96
context 'with websocket port' do
97
it 'does not add websocket-port' do
98
service = described_class.new(args: ['--websocket-port=1234'])
99
expect(service.extra_args).not_to include('--websocket-port=0')
100
expect(service.extra_args).to eq(['--websocket-port=1234'])
101
end
102
end
103
104
context 'when SE_DEBUG is set' do
105
around do |example|
106
ENV['SE_DEBUG'] = '1'
107
example.run
108
ensure
109
ENV.delete('SE_DEBUG')
110
end
111
112
it 'adds -v flag' do
113
service = described_class.new
114
115
expect(service.extra_args).to include('-v')
116
end
117
118
it 'removes conflicting --log args with value' do
119
service = described_class.new(args: ['--log', 'info'])
120
121
expect(service.extra_args).to include('-v')
122
expect(service.extra_args).not_to include('--log')
123
expect(service.extra_args).not_to include('info')
124
end
125
126
it 'removes conflicting --log= args' do
127
service = described_class.new(args: ['--log=info'])
128
129
expect(service.extra_args).to include('-v')
130
expect(service.extra_args).not_to include('--log=info')
131
end
132
133
it 'does not remove next arg if --log has no value' do
134
service = described_class.new(args: ['--log', '--other-flag'])
135
136
expect(service.extra_args).to include('-v')
137
expect(service.extra_args).to include('--other-flag')
138
end
139
end
140
end
141
142
context 'when initializing driver' do
143
let(:driver) { Firefox::Driver }
144
let(:service) do
145
instance_double(described_class, launch: service_manager, executable_path: nil, 'executable_path=': nil,
146
class: described_class)
147
end
148
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
149
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }
150
let(:finder) { instance_double(DriverFinder, browser_path?: false, driver_path: '/path/to/driver') }
151
152
before do
153
allow(Remote::Bridge).to receive(:new).and_return(bridge)
154
allow(bridge).to receive(:browser).and_return(:firefox)
155
end
156
157
it 'is not created when :url is provided' do
158
expect {
159
driver.new(url: 'http://example.com:4321')
160
}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Firefox::Driver with :url")
161
end
162
163
it 'is created when :url is not provided' do
164
allow(DriverFinder).to receive(:new).and_return(finder)
165
allow(described_class).to receive(:new).and_return(service)
166
167
driver.new
168
169
expect(described_class).to have_received(:new).with(no_args)
170
end
171
172
it 'accepts :service without creating a new instance' do
173
allow(DriverFinder).to receive(:new).and_return(finder)
174
allow(described_class).to receive(:new)
175
176
driver.new(service: service)
177
178
expect(described_class).not_to have_received(:new)
179
end
180
181
context 'with a path env variable' do
182
let(:service) { described_class.new }
183
let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }
184
185
before do
186
ENV['SE_GECKODRIVER'] = service_path
187
end
188
189
after { ENV.delete('SE_GECKODRIVER') }
190
191
it 'uses the path from the environment' do
192
expect(service.executable_path).to match(/geckodriver/)
193
end
194
195
it 'updates the path after setting the environment variable' do
196
ENV['SE_GECKODRIVER'] = '/foo/bar'
197
service.executable_path = service_path
198
199
expect(service.executable_path).to match(/geckodriver/)
200
end
201
end
202
end
203
end
204
end # Firefox
205
end # WebDriver
206
end # Selenium
207
208