Path: blob/trunk/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb
4135 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('../spec_helper', __dir__)2021module Selenium22module WebDriver23module Firefox24describe Service do25describe '#new' do26let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }2728before do29allow(Platform).to receive(:assert_executable)30allow(WebDriver.logger).to receive(:debug?).and_return(false)31end3233it 'uses default port and nil path' do34service = described_class.new3536expect(service.port).to eq Service::DEFAULT_PORT37expect(service.host).to eq Platform.localhost38expect(service.executable_path).to be_nil39end4041it 'uses provided path and port' do42path = 'foo'43port = 56784445service = described_class.new(path: path, port: port)4647expect(service.executable_path).to eq path48expect(service.port).to eq port49end5051it 'creates websocket args by default' do52service = described_class.new5354expect(service.extra_args.count).to eq 255end5657it 'uses sets log path to stdout' do58service = described_class.new(log: :stdout)5960expect(service.log).to eq $stdout61end6263it 'uses sets log path to stderr' do64service = described_class.new(log: :stderr)6566expect(service.log).to eq $stderr67end6869it 'sets log path as file location' do70service = described_class.new(log: '/path/to/log.txt')7172expect(service.log).to eq '/path/to/log.txt'73end7475it 'uses provided args' do76service = described_class.new(args: %w[--foo --bar])77expect(service.extra_args).to include(*%w[--foo --bar])78end7980it 'there is a zero port for websocket' do81service = described_class.new82ws_index = service.extra_args.index('--websocket-port')83port = service.extra_args[ws_index + 1].to_i84expect(port).to be_zero85end8687context 'with connect existing' do88it 'does not uses websocket-port' do89service = described_class.new(args: ['--connect-existing'])90expect(service.extra_args).not_to include('--websocket-port')91expect(service.extra_args).to eq(['--connect-existing'])92end93end9495context 'with websocket port' do96it 'does not add websocket-port' do97service = described_class.new(args: ['--websocket-port=1234'])98expect(service.extra_args).not_to include('--websocket-port=0')99expect(service.extra_args).to eq(['--websocket-port=1234'])100end101end102103context 'when SE_DEBUG is set' do104around do |example|105ENV['SE_DEBUG'] = '1'106example.run107ensure108ENV.delete('SE_DEBUG')109end110111it 'adds -v flag' do112service = described_class.new113114expect(service.extra_args).to include('-v')115end116117it 'removes conflicting --log args with value' do118service = described_class.new(args: ['--log', 'info'])119120expect(service.extra_args).to include('-v')121expect(service.extra_args).not_to include('--log')122expect(service.extra_args).not_to include('info')123end124125it 'removes conflicting --log= args' do126service = described_class.new(args: ['--log=info'])127128expect(service.extra_args).to include('-v')129expect(service.extra_args).not_to include('--log=info')130end131132it 'does not remove next arg if --log has no value' do133service = described_class.new(args: ['--log', '--other-flag'])134135expect(service.extra_args).to include('-v')136expect(service.extra_args).to include('--other-flag')137end138end139end140141context 'when initializing driver' do142let(:driver) { Firefox::Driver }143let(:service) do144instance_double(described_class, launch: service_manager, executable_path: nil, 'executable_path=': nil,145class: described_class)146end147let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }148let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }149let(:finder) { instance_double(DriverFinder, browser_path?: false, driver_path: '/path/to/driver') }150151before do152allow(Remote::Bridge).to receive(:new).and_return(bridge)153allow(bridge).to receive(:browser).and_return(:firefox)154end155156it 'is not created when :url is provided' do157expect {158driver.new(url: 'http://example.com:4321')159}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Firefox::Driver with :url")160end161162it 'is created when :url is not provided' do163allow(DriverFinder).to receive(:new).and_return(finder)164allow(described_class).to receive(:new).and_return(service)165166driver.new167168expect(described_class).to have_received(:new).with(no_args)169end170171it 'accepts :service without creating a new instance' do172allow(DriverFinder).to receive(:new).and_return(finder)173allow(described_class).to receive(:new)174175driver.new(service: service)176177expect(described_class).not_to have_received(:new)178end179180context 'with a path env variable' do181let(:service) { described_class.new }182let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }183184before do185ENV['SE_GECKODRIVER'] = service_path186end187188after { ENV.delete('SE_GECKODRIVER') }189190it 'uses the path from the environment' do191expect(service.executable_path).to match(/geckodriver/)192end193194it 'updates the path after setting the environment variable' do195ENV['SE_GECKODRIVER'] = '/foo/bar'196service.executable_path = service_path197198expect(service.executable_path).to match(/geckodriver/)199end200end201end202end203end # Firefox204end # WebDriver205end # Selenium206207208