Path: blob/trunk/rb/spec/unit/selenium/webdriver/common/selenium_manager_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 WebDriver23describe SeleniumManager do24describe '.binary' do25before do26described_class.instance_variable_set(:@binary, nil)27end2829it 'uses environment variable' do30allow(Platform).to receive(:assert_executable).with('/path/to/selenium-manager').and_return(true)31allow(ENV).to receive(:fetch).with('SE_MANAGER_PATH', nil).and_return('/path/to/selenium-manager')3233expect(described_class.send(:binary)).to match(%r{/path/to/selenium-manager})34end3536it 'detects Windows' do37allow(Platform).to receive(:assert_executable).with(a_string_ending_with('/windows/selenium-manager.exe'))38.and_return(true)39allow(Platform).to receive(:windows?).and_return(true)4041expect(described_class.send(:binary)).to match(%r{/windows/selenium-manager\.exe$})42end4344it 'detects Mac' do45allow(Platform).to receive(:assert_executable).with(a_string_ending_with('/macos/selenium-manager'))46.and_return(true)47allow(Platform).to receive_messages(windows?: false, mac?: true)4849expect(described_class.send(:binary)).to match(%r{/macos/selenium-manager$})50end5152it 'detects Linux' do53allow(Platform).to receive(:assert_executable).with(a_string_ending_with('/linux/selenium-manager'))54.and_return(true)55allow(Platform).to receive_messages(windows?: false, mac?: false, linux?: true)5657expect(described_class.send(:binary)).to match(%r{/linux/selenium-manager$})58end5960it 'errors if cannot find' do61allow(File).to receive(:exist?).with(a_string_including('selenium-manager')).and_return(false)6263expect { described_class.send(:binary) }.to raise_error(Error::WebDriverError, /not a file/)64end65end6667describe '.run' do68it 'returns result if positive exit status' do69status = instance_double(Process::Status, exitstatus: 0)70stdout = '{"result": "value", "logs": []}'71allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])7273expect(described_class.send(:run, 'anything')).to eq 'value'74end7576it 'errors if a problem with command' do77allow(Open3).to receive(:capture3).and_raise(StandardError)7879expect {80described_class.send(:run, 'anything')81}.to raise_error(Error::WebDriverError, /Unsuccessful command executed: \["anything"\]/)82end8384it 'errors if exit status greater than 0' do85status = instance_double(Process::Status, exitstatus: 1)86stdout = '{"result": "value", "logs": []}'87allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])8889msg = /Unsuccessful command executed: \["anything"\] - Code 1\nvalue\nstderr/90expect {91described_class.send(:run, 'anything')92}.to raise_error(Error::WebDriverError, msg)93end9495it 'succeeds when exitstatus is nil and result is present' do96status = instance_double(Process::Status, exitstatus: nil)97stdout = '{"result": "value", "logs": []}'98allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])99100expect {101expect(described_class.send(:run, 'anything')).to eq 'value'102}.to have_info(:selenium_manager)103end104105it 'raises if result is nil even with successful exitstatus' do106status = instance_double(Process::Status, exitstatus: 0)107stdout = '{"logs": []}'108allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])109110expect {111described_class.send(:run, 'anything')112}.to raise_error(Error::WebDriverError, /Unsuccessful command executed/)113end114end115116describe '.binary_paths' do117it 'returns exact output from #run' do118return_map = {}119allow(described_class).to receive_messages(binary: 'binary', run: return_map)120expect(described_class.binary_paths('something')).to eq(return_map)121end122end123end124end # WebDriver125end # Selenium126127128