Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/server_spec.rb
1864 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('webdriver/spec_helper', __dir__)
21
require 'selenium/server'
22
23
module Selenium
24
describe Server do
25
let(:mock_process) { instance_double(WebDriver::ChildProcess).as_null_object }
26
let(:mock_poller) { instance_double(WebDriver::SocketPoller, connected?: true, closed?: true) }
27
let(:repo) { 'https://api.github.com/repos/seleniumhq/selenium/releases' }
28
let(:port) { WebDriver::PortProber.above(4444) }
29
let(:example_json) do
30
[{url: "#{repo}/41272273",
31
assets: {
32
name: 'selenium-server-3.141.59.jar',
33
browser_download_url: "#{repo}/selenium-3.141.59/selenium-server-standalone-3.141.59.jar"
34
}},
35
{url: "#{repo}/51272273",
36
assets: {
37
name: 'selenium-server-10.0.1.jar',
38
browser_download_url: "#{repo}/selenium-10.0.1/selenium-server-10.0.1.jar"
39
}}].to_json
40
end
41
42
it 'raises an error if the jar file does not exist' do
43
expect {
44
described_class.new('doesnt-exist.jar')
45
}.to raise_error(Errno::ENOENT)
46
end
47
48
it 'uses the given jar file and port' do
49
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
50
allow(WebDriver::ChildProcess).to receive(:build)
51
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
52
.and_return(mock_process)
53
54
server = described_class.new('selenium_server_deploy.jar', port: 1234, background: true)
55
allow(server).to receive(:socket).and_return(mock_poller)
56
57
server.start
58
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
59
expect(WebDriver::ChildProcess).to have_received(:build)
60
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
61
end
62
63
it 'waits for the server process by default' do
64
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
65
allow(WebDriver::ChildProcess).to receive(:build)
66
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
67
.and_return(mock_process)
68
69
server = described_class.new('selenium_server_deploy.jar', port: port)
70
allow(server).to receive(:socket).and_return(mock_poller)
71
allow(mock_process).to receive(:wait)
72
73
server.start
74
75
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
76
expect(WebDriver::ChildProcess).to have_received(:build)
77
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
78
expect(mock_process).to have_received(:wait)
79
end
80
81
it 'adds additional args' do
82
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
83
allow(WebDriver::ChildProcess).to receive(:build)
84
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s, 'foo', 'bar')
85
.and_return(mock_process)
86
87
server = described_class.new('selenium_server_deploy.jar', port: port, background: true)
88
allow(server).to receive(:socket).and_return(mock_poller)
89
90
server << %w[foo bar]
91
92
server.start
93
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
94
expect(WebDriver::ChildProcess).to have_received(:build)
95
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone',
96
'--port', port.to_s, 'foo', 'bar')
97
end
98
99
it 'adds additional JAVA options args' do
100
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
101
allow(WebDriver::ChildProcess).to receive(:build)
102
.with('java',
103
'-Dwebdriver.chrome.driver=/bin/chromedriver',
104
'-jar', 'selenium_server_deploy.jar',
105
'standalone',
106
'--port', port.to_s,
107
'foo',
108
'bar')
109
.and_return(mock_process)
110
111
server = described_class.new('selenium_server_deploy.jar', background: true)
112
allow(server).to receive(:socket).and_return(mock_poller)
113
114
server << %w[foo bar]
115
server << '-Dwebdriver.chrome.driver=/bin/chromedriver'
116
117
server.start
118
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
119
expect(WebDriver::ChildProcess).to have_received(:build)
120
.with('java',
121
'-Dwebdriver.chrome.driver=/bin/chromedriver',
122
'-jar', 'selenium_server_deploy.jar',
123
'standalone',
124
'--port', port.to_s,
125
'foo',
126
'bar')
127
end
128
129
it 'downloads the specified version from the selenium site' do
130
required_version = '10.0.1'
131
expected_download_file_name = "selenium-server-#{required_version}.jar"
132
133
stub_request(:get, repo).to_return(body: example_json)
134
135
stub_request(:get, "#{repo}/selenium-10.0.1/#{expected_download_file_name}")
136
.to_return(headers: {location: 'https://github-releases.githubusercontent.com/something'})
137
138
stub_request(:get, 'https://github-releases.githubusercontent.com/something')
139
.to_return(body: 'this is pretending to be a jar file for testing purposes')
140
141
begin
142
actual_download_file_name = described_class.download(required_version)
143
expect(actual_download_file_name).to eq(expected_download_file_name)
144
expect(File).to exist(expected_download_file_name)
145
ensure
146
FileUtils.rm_rf expected_download_file_name
147
end
148
end
149
150
it 'gets a server instance and downloads the specified version' do
151
required_version = '10.4.0'
152
expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"
153
expected_options = {port: 5555}
154
fake_server = Object.new
155
156
allow(described_class).to receive(:download).with(required_version).and_return(expected_download_file_name)
157
allow(described_class).to receive(:new).with(expected_download_file_name,
158
expected_options).and_return(fake_server)
159
160
server = described_class.get required_version, expected_options
161
expect(server).to eq(fake_server)
162
expect(described_class).to have_received(:download).with(required_version)
163
expect(described_class).to have_received(:new).with(expected_download_file_name, expected_options)
164
end
165
166
it 'automatically repairs http_proxy settings that do not start with http://' do
167
with_env('http_proxy' => 'proxy.com') do
168
expect(described_class.net_http_start('example.com', &:proxy_address)).to eq('proxy.com')
169
end
170
171
with_env('HTTP_PROXY' => 'proxy.com') do
172
expect(described_class.net_http_start('example.com', &:proxy_address)).to eq('proxy.com')
173
end
174
end
175
176
it 'only downloads a jar if it is not present in the current directory' do
177
required_version = '10.2.0'
178
expected_download_file_name = "selenium-server-#{required_version}.jar"
179
180
allow(File).to receive(:exist?).with(expected_download_file_name).and_return true
181
182
described_class.download required_version
183
expect(File).to have_received(:exist?).with(expected_download_file_name)
184
end
185
186
it 'knows what the latest version available is' do
187
expected_latest = '10.0.1'
188
189
stub_request(:get, repo).to_return(body: example_json)
190
191
expect(described_class.latest).to eq(expected_latest)
192
end
193
194
it 'raises Selenium::Server::Error if the server is not launched within the timeout' do
195
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
196
allow(WebDriver::ChildProcess).to receive(:build)
197
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
198
.and_return(mock_process)
199
200
poller = instance_double(WebDriver::SocketPoller)
201
allow(poller).to receive(:connected?).and_return(false)
202
203
server = described_class.new('selenium_server_deploy.jar', background: true)
204
allow(server).to receive(:socket).and_return(poller)
205
206
expect { server.start }.to raise_error(Selenium::Server::Error)
207
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
208
end
209
210
it 'sets options after instantiation' do
211
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
212
server = described_class.new('selenium_server_deploy.jar', port: port)
213
expect(server.port).to eq(port)
214
expect(server.timeout).to eq(30)
215
expect(server.background).to be false
216
expect(server.log).to be_nil
217
218
server.port = 1234
219
server.timeout = 5
220
server.background = true
221
server.log = '/tmp/server.log'
222
223
aggregate_failures do
224
expect(server.port).to eq(1234)
225
expect(server.timeout).to eq(5)
226
expect(server.background).to be_truthy
227
expect(server.log).to eq('/tmp/server.log')
228
end
229
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
230
end
231
end
232
end # Selenium
233
234