Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/common/selenium_manager_spec.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require File.expand_path('../spec_helper', __dir__)
21
22
module Selenium
23
module WebDriver
24
describe SeleniumManager do
25
describe '.binary' do
26
before do
27
described_class.instance_variable_set(:@binary, nil)
28
end
29
30
it 'uses environment variable' do
31
allow(Platform).to receive(:assert_executable).with('/path/to/selenium-manager').and_return(true)
32
allow(ENV).to receive(:fetch).with('SE_MANAGER_PATH', nil).and_return('/path/to/selenium-manager')
33
34
expect(described_class.send(:binary)).to match(%r{/path/to/selenium-manager})
35
end
36
37
it 'detects Windows' do
38
allow(Platform).to receive(:assert_executable).with(a_string_ending_with('/windows/selenium-manager.exe'))
39
.and_return(true)
40
allow(Platform).to receive(:windows?).and_return(true)
41
42
expect(described_class.send(:binary)).to match(%r{/windows/selenium-manager\.exe$})
43
end
44
45
it 'detects Mac' do
46
allow(Platform).to receive(:assert_executable).with(a_string_ending_with('/macos/selenium-manager'))
47
.and_return(true)
48
allow(Platform).to receive_messages(windows?: false, mac?: true)
49
50
expect(described_class.send(:binary)).to match(%r{/macos/selenium-manager$})
51
end
52
53
it 'detects Linux' do
54
allow(Platform).to receive(:assert_executable).with(a_string_ending_with('/linux/selenium-manager'))
55
.and_return(true)
56
allow(Platform).to receive_messages(windows?: false, mac?: false, linux?: true)
57
58
expect(described_class.send(:binary)).to match(%r{/linux/selenium-manager$})
59
end
60
61
it 'errors if cannot find' do
62
allow(File).to receive(:exist?).with(a_string_including('selenium-manager')).and_return(false)
63
64
expect { described_class.send(:binary) }.to raise_error(Error::WebDriverError, /not a file/)
65
end
66
end
67
68
describe '.run' do
69
it 'returns result if positive exit status' do
70
status = instance_double(Process::Status, exitstatus: 0)
71
stdout = '{"result": "value", "logs": []}'
72
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
73
74
expect(described_class.send(:run, 'anything')).to eq 'value'
75
end
76
77
it 'errors if a problem with command' do
78
allow(Open3).to receive(:capture3).and_raise(StandardError)
79
80
expect {
81
described_class.send(:run, 'anything')
82
}.to raise_error(Error::WebDriverError, /Unsuccessful command executed: \["anything"\]/)
83
end
84
85
it 'errors if exit status greater than 0' do
86
status = instance_double(Process::Status, exitstatus: 1)
87
stdout = '{"result": "value", "logs": []}'
88
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
89
90
msg = /Unsuccessful command executed: \["anything"\] - Code 1\nvalue\nstderr/
91
expect {
92
described_class.send(:run, 'anything')
93
}.to raise_error(Error::WebDriverError, msg)
94
end
95
96
it 'succeeds when exitstatus is nil and result is present' do
97
status = instance_double(Process::Status, exitstatus: nil)
98
stdout = '{"result": "value", "logs": []}'
99
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
100
101
expect {
102
expect(described_class.send(:run, 'anything')).to eq 'value'
103
}.to have_info(:selenium_manager)
104
end
105
106
it 'raises if result is nil even with successful exitstatus' do
107
status = instance_double(Process::Status, exitstatus: 0)
108
stdout = '{"logs": []}'
109
allow(Open3).to receive(:capture3).and_return([stdout, 'stderr', status])
110
111
expect {
112
described_class.send(:run, 'anything')
113
}.to raise_error(Error::WebDriverError, /Unsuccessful command executed/)
114
end
115
end
116
117
describe '.binary_paths' do
118
it 'returns exact output from #run' do
119
return_map = {}
120
allow(described_class).to receive_messages(binary: 'binary', run: return_map)
121
expect(described_class.binary_paths('something')).to eq(return_map)
122
end
123
end
124
end
125
end # WebDriver
126
end # Selenium
127
128