Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/chrome/service_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
module Chrome
25
describe Service do
26
describe '#new' do
27
let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }
28
29
before do
30
allow(Platform).to receive(:assert_executable)
31
end
32
33
after { described_class.driver_path = nil }
34
35
it 'uses nil path and default port' do
36
service = described_class.new
37
38
expect(service.port).to eq Service::DEFAULT_PORT
39
expect(service.host).to eq Platform.localhost
40
expect(service.executable_path).to be_nil
41
end
42
43
it 'uses provided path and port' do
44
path = 'foo'
45
port = 5678
46
47
service = described_class.new(path: path, port: port)
48
49
expect(service.executable_path).to eq path
50
expect(service.port).to eq port
51
expect(service.host).to eq Platform.localhost
52
end
53
54
it 'does not create args by default' do
55
service = described_class.new
56
57
expect(service.extra_args).to be_empty
58
end
59
60
it 'uses sets log path to stdout' do
61
service = described_class.new(log: :stdout)
62
63
expect(service.log).to eq $stdout
64
end
65
66
it 'uses sets log path to stderr' do
67
service = described_class.new(log: :stderr)
68
69
expect(service.log).to eq $stderr
70
end
71
72
it 'setting log output as a file converts to argument' do
73
service = described_class.new(log: '/path/to/log.txt')
74
75
expect(service.log).to be_nil
76
expect(service.args).to eq ['--log-path=/path/to/log.txt']
77
end
78
79
it 'uses provided args' do
80
service = described_class.new(args: ['--foo', '--bar'])
81
82
expect(service.extra_args).to eq ['--foo', '--bar']
83
end
84
end
85
86
context 'when initializing driver' do
87
let(:driver) { Chrome::Driver }
88
let(:service) do
89
instance_double(described_class, launch: service_manager, executable_path: nil, 'executable_path=': nil,
90
class: described_class)
91
end
92
let(:service_manager) { instance_double(ServiceManager, uri: 'http://example.com') }
93
let(:bridge) { instance_double(Remote::Bridge, quit: nil, create_session: {}) }
94
let(:finder) { instance_double(DriverFinder, browser_path?: false, driver_path: '/path/to/driver') }
95
96
before do
97
allow(Remote::Bridge).to receive(:new).and_return(bridge)
98
allow(bridge).to receive(:browser).and_return(:chrome)
99
end
100
101
it 'errors when :url is provided' do
102
expect {
103
driver.new(url: 'http://example.com:4321')
104
}.to raise_error(ArgumentError, "Can't initialize Selenium::WebDriver::Chrome::Driver with :url")
105
end
106
107
it 'is created when :url is not provided' do
108
allow(DriverFinder).to receive(:new).and_return(finder)
109
allow(described_class).to receive(:new).and_return(service)
110
111
driver.new
112
expect(described_class).to have_received(:new).with(no_args)
113
end
114
115
it 'accepts :service without creating a new instance' do
116
allow(DriverFinder).to receive(:new).and_return(finder)
117
allow(described_class).to receive(:new)
118
119
driver.new(service: service)
120
expect(described_class).not_to have_received(:new)
121
end
122
123
context 'with a path env variable' do
124
let(:service) { described_class.new }
125
let(:service_path) { "/path/to/#{Service::EXECUTABLE}" }
126
127
before do
128
ENV['SE_CHROMEDRIVER'] = service_path
129
end
130
131
after { ENV.delete('SE_CHROMEDRIVER') }
132
133
it 'uses the path from the environment' do
134
expect(service.executable_path).to match(/chromedriver/)
135
end
136
137
it 'updates the path after setting the environment variable' do
138
ENV['SE_CHROMEDRIVER'] = '/foo/bar'
139
service.executable_path = service_path
140
141
expect(service.executable_path).to match(/chromedriver/)
142
end
143
end
144
end
145
end
146
end # Chrome
147
end # WebDriver
148
end # Selenium
149
150