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
1865 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
end
32
33
it 'uses default port and nil path' do
34
service = described_class.new
35
36
expect(service.port).to eq Service::DEFAULT_PORT
37
expect(service.host).to eq Platform.localhost
38
expect(service.executable_path).to be_nil
39
end
40
41
it 'uses provided path and port' do
42
path = 'foo'
43
port = 5678
44
45
service = described_class.new(path: path, port: port)
46
47
expect(service.executable_path).to eq path
48
expect(service.port).to eq port
49
end
50
51
it 'does not create args by default' do
52
service = described_class.new
53
54
expect(service.extra_args.count).to eq 2
55
end
56
57
it 'uses sets log path to stdout' do
58
service = described_class.new(log: :stdout)
59
60
expect(service.log).to eq $stdout
61
end
62
63
it 'uses sets log path to stderr' do
64
service = described_class.new(log: :stderr)
65
66
expect(service.log).to eq $stderr
67
end
68
69
it 'sets log path as file location' do
70
service = described_class.new(log: '/path/to/log.txt')
71
72
expect(service.log).to eq '/path/to/log.txt'
73
end
74
75
it 'uses provided args' do
76
service = described_class.new(args: %w[--foo --bar])
77
expect(service.extra_args).to include(*%w[--foo --bar])
78
end
79
80
it 'there is a zero port for websocket' do
81
service = described_class.new
82
ws_index = service.extra_args.index('--websocket-port')
83
port = service.extra_args[ws_index + 1].to_i
84
expect(port).to be_zero
85
end
86
87
context 'with connect existing' do
88
it 'does not uses websocket-port' do
89
service = described_class.new(args: ['--connect-existing'])
90
expect(service.extra_args).not_to include('--websocket-port')
91
expect(service.extra_args).to eq(['--connect-existing'])
92
end
93
end
94
95
context 'with websocket port' do
96
it 'does not add websocket-port' do
97
service = described_class.new(args: ['--websocket-port=1234'])
98
expect(service.extra_args).not_to include('--websocket-port=0')
99
expect(service.extra_args).to eq(['--websocket-port=1234'])
100
end
101
end
102
end
103
104
context 'when initializing driver' do
105
let(:driver) { Firefox::Driver }
106
let(:service) do
107
instance_double(described_class, launch: service_manager, executable_path: nil, 'executable_path=': nil,
108
class: described_class)
109
end
110
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
111
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }
112
let(:finder) { instance_double(DriverFinder, browser_path?: false, driver_path: '/path/to/driver') }
113
114
before do
115
allow(Remote::Bridge).to receive(:new).and_return(bridge)
116
allow(bridge).to receive(:browser).and_return(:firefox)
117
end
118
119
it 'is not created when :url is provided' do
120
expect {
121
driver.new(url: 'http://example.com:4321')
122
}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Firefox::Driver with :url")
123
end
124
125
it 'is created when :url is not provided' do
126
allow(DriverFinder).to receive(:new).and_return(finder)
127
allow(described_class).to receive(:new).and_return(service)
128
129
driver.new
130
131
expect(described_class).to have_received(:new).with(no_args)
132
end
133
134
it 'accepts :service without creating a new instance' do
135
allow(DriverFinder).to receive(:new).and_return(finder)
136
allow(described_class).to receive(:new)
137
138
driver.new(service: service)
139
140
expect(described_class).not_to have_received(:new)
141
end
142
143
context 'with a path env variable' do
144
let(:service) { described_class.new }
145
let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }
146
147
before do
148
ENV['SE_GECKODRIVER'] = service_path
149
end
150
151
after { ENV.delete('SE_GECKODRIVER') }
152
153
it 'uses the path from the environment' do
154
expect(service.executable_path).to match(/geckodriver/)
155
end
156
157
it 'updates the path after setting the environment variable' do
158
ENV['SE_GECKODRIVER'] = '/foo/bar'
159
service.executable_path = service_path
160
161
expect(service.executable_path).to match(/geckodriver/)
162
end
163
end
164
end
165
end
166
end # Firefox
167
end # WebDriver
168
end # Selenium
169
170