Path: blob/trunk/rb/spec/unit/selenium/webdriver/chrome/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 Chrome24describe Service do25describe '#new' do26let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }2728before do29allow(Platform).to receive(:assert_executable)30end3132after { described_class.driver_path = nil }3334it 'uses nil path and default port' do35service = described_class.new3637expect(service.port).to eq Service::DEFAULT_PORT38expect(service.host).to eq Platform.localhost39expect(service.executable_path).to be_nil40end4142it 'uses provided path and port' do43path = 'foo'44port = 56784546service = described_class.new(path: path, port: port)4748expect(service.executable_path).to eq path49expect(service.port).to eq port50expect(service.host).to eq Platform.localhost51end5253it 'does not create args by default' do54service = described_class.new5556expect(service.extra_args).to be_empty57end5859it 'uses sets log path to stdout' do60service = described_class.new(log: :stdout)6162expect(service.log).to eq $stdout63end6465it 'uses sets log path to stderr' do66service = described_class.new(log: :stderr)6768expect(service.log).to eq $stderr69end7071it 'setting log output as a file converts to argument' do72service = described_class.new(log: '/path/to/log.txt')7374expect(service.log).to be_nil75expect(service.args).to eq ['--log-path=/path/to/log.txt']76end7778it 'uses provided args' do79service = described_class.new(args: ['--foo', '--bar'])8081expect(service.extra_args).to eq ['--foo', '--bar']82end83end8485context 'when initializing driver' do86let(:driver) { Chrome::Driver }87let(:service) do88instance_double(described_class, launch: service_manager, executable_path: nil, 'executable_path=': nil,89class: described_class)90end91let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }92let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }93let(:finder) { instance_double(DriverFinder, browser_path?: false, driver_path: '/path/to/driver') }9495before do96allow(Remote::Bridge).to receive(:new).and_return(bridge)97allow(bridge).to receive(:browser).and_return(:chrome)98end99100it 'errors when :url is provided' do101expect {102driver.new(url: 'http://example.com:4321')103}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Chrome::Driver with :url")104end105106it 'is created when :url is not provided' do107allow(DriverFinder).to receive(:new).and_return(finder)108allow(described_class).to receive(:new).and_return(service)109110driver.new111expect(described_class).to have_received(:new).with(no_args)112end113114it 'accepts :service without creating a new instance' do115allow(DriverFinder).to receive(:new).and_return(finder)116allow(described_class).to receive(:new)117118driver.new(service: service)119expect(described_class).not_to have_received(:new)120end121122context 'with a path env variable' do123let(:service) { described_class.new }124let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }125126before do127ENV['SE_CHROMEDRIVER'] = service_path128end129130after { ENV.delete('SE_CHROMEDRIVER') }131132it 'uses the path from the environment' do133expect(service.executable_path).to match(/chromedriver/)134end135136it 'updates the path after setting the environment variable' do137ENV['SE_CHROMEDRIVER'] = '/foo/bar'138service.executable_path = service_path139140expect(service.executable_path).to match(/chromedriver/)141end142end143end144end145end # Chrome146end # WebDriver147end # Selenium148149150