Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/remote/driver_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 Remote
25
describe Driver do
26
let(:valid_response) do
27
{status: 200,
28
body: {value: {sessionId: 0, capabilities: {browserName: 'chrome'}}}.to_json,
29
headers: {content_type: 'application/json'}}
30
end
31
32
def expect_request(body: nil, endpoint: nil)
33
body = (body || {capabilities: {alwaysMatch: {browserName: 'chrome', 'goog:chromeOptions': {}}}}).to_json
34
endpoint ||= 'http://127.0.0.1:4444/wd/hub/session'
35
stub_request(:post, endpoint).with(body: body).to_return(valid_response)
36
end
37
38
it 'requires parameters' do
39
expect { described_class.new }
40
.to raise_exception(ArgumentError, 'Selenium::WebDriver::Remote::Driver needs :options to be set')
41
end
42
43
it 'uses provided URL' do
44
server = 'https://example.com:4646/wd/hub'
45
expect_request(endpoint: "#{server}/session")
46
47
expect { described_class.new(options: Options.chrome, url: server) }.not_to raise_exception
48
end
49
50
it 'uses provided HTTP Client' do
51
client = Remote::Http::Default.new
52
expect_request
53
54
driver = described_class.new(options: Options.chrome, http_client: client)
55
expect(driver.send(:bridge).http).to eq client
56
end
57
58
it 'accepts Options as sole parameter' do
59
opts = {args: ['-f']}
60
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', 'goog:chromeOptions': opts}}})
61
62
expect { described_class.new(options: Options.chrome(**opts)) }.not_to raise_exception
63
end
64
65
it 'does not allow both Options and Capabilities' do
66
msg = "Don't use both :options and :capabilities when initializing Selenium::WebDriver::Remote::Driver, " \
67
'prefer :options'
68
expect {
69
described_class.new(options: Options.chrome, capabilities: Remote::Capabilities.new(browser_name: 'chrome'))
70
}.to raise_exception(ArgumentError, msg)
71
end
72
73
context 'with :capabilities' do
74
it 'accepts value as a Symbol' do
75
expect_request
76
expect { described_class.new(capabilities: :chrome) }.not_to raise_exception
77
end
78
79
it 'accepts constructed Capabilities with Snake Case as Symbols' do
80
capabilities = Remote::Capabilities.new(browser_name: 'chrome', invalid: 'foobar')
81
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})
82
83
expect { described_class.new(capabilities: capabilities) }.not_to raise_exception
84
end
85
86
it 'accepts constructed Capabilities with Camel Case as Symbols' do
87
capabilities = Remote::Capabilities.new(browserName: 'chrome', invalid: 'foobar')
88
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})
89
90
expect { described_class.new(capabilities: capabilities) }.not_to raise_exception
91
end
92
93
it 'accepts constructed Capabilities with Camel Case as Strings' do
94
capabilities = Remote::Capabilities.new('browserName' => 'chrome', 'invalid' => 'foobar')
95
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})
96
97
expect { described_class.new(capabilities: capabilities) }.not_to raise_exception
98
end
99
100
context 'when value is an Array' do
101
let(:as_json_object) do
102
Class.new do
103
def as_json(*)
104
{'company:key': 'value'}
105
end
106
end
107
end
108
109
it 'with Options instance' do
110
options = Options.chrome(args: ['-f'])
111
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome',
112
'goog:chromeOptions': {args: ['-f']}}}})
113
114
expect { described_class.new(capabilities: [options]) }.not_to raise_exception
115
end
116
117
it 'with Capabilities instance' do
118
capabilities = Remote::Capabilities.new(browser_name: 'chrome', invalid: 'foobar')
119
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar'}}})
120
121
expect { described_class.new(capabilities: [capabilities]) }.not_to raise_exception
122
end
123
124
it 'with Options instance and an instance of a custom object responding to #as_json' do
125
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome',
126
'goog:chromeOptions': {},
127
'company:key': 'value'}}})
128
expect { described_class.new(capabilities: [Options.chrome, as_json_object.new]) }.not_to raise_exception
129
end
130
131
it 'with Options instance, Capabilities instance and instance of a custom object responding to #as_json' do
132
capabilities = Remote::Capabilities.new(browser_name: 'chrome', invalid: 'foobar')
133
options = Options.chrome(args: ['-f'])
134
expect_request(body: {capabilities: {alwaysMatch: {browserName: 'chrome', invalid: 'foobar',
135
'goog:chromeOptions': {args: ['-f']},
136
'company:key': 'value'}}})
137
138
expect {
139
described_class.new(capabilities: [capabilities, options, as_json_object.new])
140
}.not_to raise_exception
141
end
142
end
143
end
144
end
145
end # Remote
146
end # WebDriver
147
end # Selenium
148
149