| 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(:port) { 4321 } |
| 27 | let(:jar) { 'selenium_server_deploy.jar' } |
| 28 | |
| 29 | before { allow(File).to receive(:exist?).with(jar).and_return(true) } |
| 30 | |
| 31 | it 'raises an error if the jar file does not exist' do |
| 32 | expect { |
| 33 | described_class.new('doesnt-exist.jar') |
| 34 | }.to raise_error(Errno::ENOENT) |
| 35 | end |
| 36 | |
| 37 | describe '#start' do |
| 38 | let(:status_url) { "http://127.0.0.1:#{port}/status" } |
| 39 | |
| 40 | before do |
| 41 | allow(WebDriver::ChildProcess).to receive(:build).and_return(mock_process) |
| 42 | allow(WebDriver::SocketPoller).to receive(:new) |
| 43 | .and_return(instance_double(WebDriver::SocketPoller, connected?: true)) |
| 44 | stub_request(:get, status_url).to_return(body: {value: {ready: true}}.to_json) |
| 45 | end |
| 46 | |
| 47 | it 'uses the given jar file and port' do |
| 48 | described_class.new(jar, port: port).start |
| 49 | |
| 50 | expect(WebDriver::ChildProcess).to have_received(:build) |
| 51 | .with('java', '-jar', jar, 'standalone', '--port', port.to_s) |
| 52 | end |
| 53 | |
| 54 | it 'waits for the server process by default' do |
| 55 | described_class.new(jar, port: port).start |
| 56 | |
| 57 | expect(mock_process).to have_received(:wait) |
| 58 | end |
| 59 | |
| 60 | it 'does not wait for the server process when running in the background' do |
| 61 | described_class.new(jar, port: port, background: true).start |
| 62 | |
| 63 | expect(mock_process).not_to have_received(:wait) |
| 64 | end |
| 65 | |
| 66 | it 'adds additional args' do |
| 67 | server = described_class.new(jar, port: port) |
| 68 | server << %w[foo bar] |
| 69 | server.start |
| 70 | |
| 71 | expect(WebDriver::ChildProcess).to have_received(:build) |
| 72 | .with('java', '-jar', jar, 'standalone', '--port', port.to_s, 'foo', 'bar') |
| 73 | end |
| 74 | |
| 75 | it 'adds additional JAVA options args' do |
| 76 | server = described_class.new(jar, port: port) |
| 77 | server << %w[foo bar] |
| 78 | server << '-Dwebdriver.chrome.driver=/bin/chromedriver' |
| 79 | server.start |
| 80 | |