Path: blob/trunk/rb/spec/unit/selenium/webdriver/firefox/service_spec.rb
1865 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)30end3132it 'uses default port and nil path' do33service = described_class.new3435expect(service.port).to eq Service::DEFAULT_PORT36expect(service.host).to eq Platform.localhost37expect(service.executable_path).to be_nil38end3940it 'uses provided path and port' do41path = 'foo'42port = 56784344service = described_class.new(path: path, port: port)4546expect(service.executable_path).to eq path47expect(service.port).to eq port48end4950it 'does not create args by default' do51service = described_class.new5253expect(service.extra_args.count).to eq 254end5556it 'uses sets log path to stdout' do57service = described_class.new(log: :stdout)5859expect(service.log).to eq $stdout60end6162it 'uses sets log path to stderr' do63service = described_class.new(log: :stderr)6465expect(service.log).to eq $stderr66end6768it 'sets log path as file location' do69service = described_class.new(log: '/path/to/log.txt')7071expect(service.log).to eq '/path/to/log.txt'72end7374it 'uses provided args' do75service = described_class.new(args: %w[--foo --bar])76expect(service.extra_args).to include(*%w[--foo --bar])77end7879it 'there is a zero port for websocket' do80service = described_class.new81ws_index = service.extra_args.index('--websocket-port')82port = service.extra_args[ws_index + 1].to_i83expect(port).to be_zero84end8586context 'with connect existing' do87it 'does not uses websocket-port' do88service = described_class.new(args: ['--connect-existing'])89expect(service.extra_args).not_to include('--websocket-port')90expect(service.extra_args).to eq(['--connect-existing'])91end92end9394context 'with websocket port' do95it 'does not add websocket-port' do96service = described_class.new(args: ['--websocket-port=1234'])97expect(service.extra_args).not_to include('--websocket-port=0')98expect(service.extra_args).to eq(['--websocket-port=1234'])99end100end101end102103context 'when initializing driver' do104let(:driver) { Firefox::Driver }105let(:service) do106instance_double(described_class, launch: service_manager, executable_path: nil, 'executable_path=': nil,107class: described_class)108end109let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }110let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }111let(:finder) { instance_double(DriverFinder, browser_path?: false, driver_path: '/path/to/driver') }112113before do114allow(Remote::Bridge).to receive(:new).and_return(bridge)115allow(bridge).to receive(:browser).and_return(:firefox)116end117118it 'is not created when :url is provided' do119expect {120driver.new(url: 'http://example.com:4321')121}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Firefox::Driver with :url")122end123124it 'is created when :url is not provided' do125allow(DriverFinder).to receive(:new).and_return(finder)126allow(described_class).to receive(:new).and_return(service)127128driver.new129130expect(described_class).to have_received(:new).with(no_args)131end132133it 'accepts :service without creating a new instance' do134allow(DriverFinder).to receive(:new).and_return(finder)135allow(described_class).to receive(:new)136137driver.new(service: service)138139expect(described_class).not_to have_received(:new)140end141142context 'with a path env variable' do143let(:service) { described_class.new }144let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }145146before do147ENV['SE_GECKODRIVER'] = service_path148end149150after { ENV.delete('SE_GECKODRIVER') }151152it 'uses the path from the environment' do153expect(service.executable_path).to match(/geckodriver/)154end155156it 'updates the path after setting the environment variable' do157ENV['SE_GECKODRIVER'] = '/foo/bar'158service.executable_path = service_path159160expect(service.executable_path).to match(/geckodriver/)161end162end163end164end165end # Firefox166end # WebDriver167end # Selenium168169170