Path: blob/trunk/rb/spec/unit/selenium/server_spec.rb
1864 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require File.expand_path('webdriver/spec_helper', __dir__)20require 'selenium/server'2122module Selenium23describe Server do24let(:mock_process) { instance_double(WebDriver::ChildProcess).as_null_object }25let(:mock_poller) { instance_double(WebDriver::SocketPoller, connected?: true, closed?: true) }26let(:repo) { 'https://api.github.com/repos/seleniumhq/selenium/releases' }27let(:port) { WebDriver::PortProber.above(4444) }28let(:example_json) do29[{url: "#{repo}/41272273",30assets: {31name: 'selenium-server-3.141.59.jar',32browser_download_url: "#{repo}/selenium-3.141.59/selenium-server-standalone-3.141.59.jar"33}},34{url: "#{repo}/51272273",35assets: {36name: 'selenium-server-10.0.1.jar',37browser_download_url: "#{repo}/selenium-10.0.1/selenium-server-10.0.1.jar"38}}].to_json39end4041it 'raises an error if the jar file does not exist' do42expect {43described_class.new('doesnt-exist.jar')44}.to raise_error(Errno::ENOENT)45end4647it 'uses the given jar file and port' do48allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)49allow(WebDriver::ChildProcess).to receive(:build)50.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')51.and_return(mock_process)5253server = described_class.new('selenium_server_deploy.jar', port: 1234, background: true)54allow(server).to receive(:socket).and_return(mock_poller)5556server.start57expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')58expect(WebDriver::ChildProcess).to have_received(:build)59.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')60end6162it 'waits for the server process by default' do63allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)64allow(WebDriver::ChildProcess).to receive(:build)65.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)66.and_return(mock_process)6768server = described_class.new('selenium_server_deploy.jar', port: port)69allow(server).to receive(:socket).and_return(mock_poller)70allow(mock_process).to receive(:wait)7172server.start7374expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')75expect(WebDriver::ChildProcess).to have_received(:build)76.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)77expect(mock_process).to have_received(:wait)78end7980it 'adds additional args' do81allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)82allow(WebDriver::ChildProcess).to receive(:build)83.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s, 'foo', 'bar')84.and_return(mock_process)8586server = described_class.new('selenium_server_deploy.jar', port: port, background: true)87allow(server).to receive(:socket).and_return(mock_poller)8889server << %w[foo bar]9091server.start92expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')93expect(WebDriver::ChildProcess).to have_received(:build)94.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone',95'--port', port.to_s, 'foo', 'bar')96end9798it 'adds additional JAVA options args' do99allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)100allow(WebDriver::ChildProcess).to receive(:build)101.with('java',102'-Dwebdriver.chrome.driver=/bin/chromedriver',103'-jar', 'selenium_server_deploy.jar',104'standalone',105'--port', port.to_s,106'foo',107'bar')108.and_return(mock_process)109110server = described_class.new('selenium_server_deploy.jar', background: true)111allow(server).to receive(:socket).and_return(mock_poller)112113server << %w[foo bar]114server << '-Dwebdriver.chrome.driver=/bin/chromedriver'115116server.start117expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')118expect(WebDriver::ChildProcess).to have_received(:build)119.with('java',120'-Dwebdriver.chrome.driver=/bin/chromedriver',121'-jar', 'selenium_server_deploy.jar',122'standalone',123'--port', port.to_s,124'foo',125'bar')126end127128it 'downloads the specified version from the selenium site' do129required_version = '10.0.1'130expected_download_file_name = "selenium-server-#{required_version}.jar"131132stub_request(:get, repo).to_return(body: example_json)133134stub_request(:get, "#{repo}/selenium-10.0.1/#{expected_download_file_name}")135.to_return(headers: {location: 'https://github-releases.githubusercontent.com/something'})136137stub_request(:get, 'https://github-releases.githubusercontent.com/something')138.to_return(body: 'this is pretending to be a jar file for testing purposes')139140begin141actual_download_file_name = described_class.download(required_version)142expect(actual_download_file_name).to eq(expected_download_file_name)143expect(File).to exist(expected_download_file_name)144ensure145FileUtils.rm_rf expected_download_file_name146end147end148149it 'gets a server instance and downloads the specified version' do150required_version = '10.4.0'151expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"152expected_options = {port: 5555}153fake_server = Object.new154155allow(described_class).to receive(:download).with(required_version).and_return(expected_download_file_name)156allow(described_class).to receive(:new).with(expected_download_file_name,157expected_options).and_return(fake_server)158159server = described_class.get required_version, expected_options160expect(server).to eq(fake_server)161expect(described_class).to have_received(:download).with(required_version)162expect(described_class).to have_received(:new).with(expected_download_file_name, expected_options)163end164165it 'automatically repairs http_proxy settings that do not start with http://' do166with_env('http_proxy' => 'proxy.com') do167expect(described_class.net_http_start('example.com', &:proxy_address)).to eq('proxy.com')168end169170with_env('HTTP_PROXY' => 'proxy.com') do171expect(described_class.net_http_start('example.com', &:proxy_address)).to eq('proxy.com')172end173end174175it 'only downloads a jar if it is not present in the current directory' do176required_version = '10.2.0'177expected_download_file_name = "selenium-server-#{required_version}.jar"178179allow(File).to receive(:exist?).with(expected_download_file_name).and_return true180181described_class.download required_version182expect(File).to have_received(:exist?).with(expected_download_file_name)183end184185it 'knows what the latest version available is' do186expected_latest = '10.0.1'187188stub_request(:get, repo).to_return(body: example_json)189190expect(described_class.latest).to eq(expected_latest)191end192193it 'raises Selenium::Server::Error if the server is not launched within the timeout' do194allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)195allow(WebDriver::ChildProcess).to receive(:build)196.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)197.and_return(mock_process)198199poller = instance_double(WebDriver::SocketPoller)200allow(poller).to receive(:connected?).and_return(false)201202server = described_class.new('selenium_server_deploy.jar', background: true)203allow(server).to receive(:socket).and_return(poller)204205expect { server.start }.to raise_error(Selenium::Server::Error)206expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')207end208209it 'sets options after instantiation' do210allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)211server = described_class.new('selenium_server_deploy.jar', port: port)212expect(server.port).to eq(port)213expect(server.timeout).to eq(30)214expect(server.background).to be false215expect(server.log).to be_nil216217server.port = 1234218server.timeout = 5219server.background = true220server.log = '/tmp/server.log'221222aggregate_failures do223expect(server.port).to eq(1234)224expect(server.timeout).to eq(5)225expect(server.background).to be_truthy226expect(server.log).to eq('/tmp/server.log')227end228expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')229end230end231end # Selenium232233234