Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/remote/bridge_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 Bridge do
26
describe '.add_command' do
27
let(:http) { WebDriver::Remote::Http::Default.new }
28
let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }
29
30
before do
31
allow(http).to receive(:request)
32
.with(any_args)
33
.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})
34
35
bridge.create_session({})
36
end
37
38
after do
39
described_class.extra_commands.clear
40
end
41
42
it 'adds new command' do
43
described_class.add_command(:highlight, :get, 'session/:session_id/highlight/:id') do |element|
44
execute :highlight, id: element
45
end
46
47
bridge.highlight('bar')
48
expect(http).to have_received(:request)
49
.with(:get, URI('http://localhost/session/foo/highlight/bar'), any_args)
50
end
51
end
52
53
describe '#initialize' do
54
it 'raises ArgumentError if passed invalid options' do
55
expect { described_class.new(foo: 'bar') }.to raise_error(ArgumentError)
56
end
57
end
58
59
describe '#create_session' do
60
let(:http) { WebDriver::Remote::Http::Default.new }
61
let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }
62
63
it 'accepts Hash' do
64
payload = JSON.generate(
65
capabilities: {
66
alwaysMatch: {
67
browserName: 'internet explorer'
68
}
69
}
70
)
71
72
allow(http).to receive(:request)
73
.with(any_args, payload)
74
.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})
75
76
bridge.create_session(browserName: 'internet explorer')
77
expect(http).to have_received(:request).with(any_args, payload)
78
end
79
80
it 'uses alwaysMatch when passed' do
81
payload = JSON.generate(
82
capabilities: {
83
alwaysMatch: {
84
browserName: 'chrome'
85
}
86
}
87
)
88
89
allow(http).to receive(:request)
90
.with(any_args, payload)
91
.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})
92
93
bridge.create_session('alwaysMatch' => {'browserName' => 'chrome'})
94
expect(http).to have_received(:request).with(any_args, payload)
95
end
96
97
it 'uses firstMatch when passed' do
98
payload = JSON.generate(
99
capabilities: {
100
firstMatch: [
101
{browserName: 'chrome'},
102
{browserName: 'firefox'}
103
]
104
}
105
)
106
107
allow(http).to receive(:request)
108
.with(any_args, payload)
109
.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})
110
111
bridge.create_session('firstMatch' => [
112
{'browserName' => 'chrome'},
113
{'browserName' => 'firefox'}
114
])
115
expect(http).to have_received(:request).with(any_args, payload)
116
end
117
118
it 'supports responses with "value" -> "capabilities" capabilities' do
119
allow(http).to receive(:request)
120
.and_return('value' => {'sessionId' => '', 'capabilities' => {'browserName' => 'firefox'}})
121
122
bridge.create_session(Capabilities.new)
123
expect(bridge.capabilities[:browser_name]).to eq('firefox')
124
end
125
end
126
127
describe '#upload' do
128
it 'raises WebDriverError if uploading non-files' do
129
expect {
130
bridge = described_class.new(url: 'http://localhost')
131
bridge.extend(WebDriver::Remote::Features)
132
bridge.upload('NotAFile')
133
}.to raise_error(Error::WebDriverError)
134
end
135
end
136
137
describe '#quit' do
138
it 'respects quit_errors' do
139
bridge = described_class.new(url: 'http://localhost')
140
allow(bridge).to receive(:execute).with(:delete_session).and_raise(IOError)
141
expect { bridge.quit }.not_to raise_error
142
end
143
end
144
145
describe 'finding elements' do
146
let(:http) { WebDriver::Remote::Http::Default.new }
147
let(:bridge) { described_class.new(http_client: http, url: 'http://localhost') }
148
149
before do
150
allow(http).to receive(:request)
151
.with(:post, URI('http://localhost/session'), any_args)
152
.and_return('status' => 200, 'value' => {'sessionId' => 'foo', 'capabilities' => {}})
153
bridge.create_session({})
154
end
155
156
describe '#find_element_by' do
157
before do
158
allow(http).to receive(:request)
159
.with(:post, URI('http://localhost/session/foo/element'), any_args)
160
.and_return('status' => 200, 'value' => {Element::ELEMENT_KEY => 'bar'})
161
end
162
163
it 'returns an element' do
164
expect(bridge.find_element_by(:id, 'test', nil)).to be_an_instance_of(Element)
165
end
166
167
context 'when custom element class is used' do
168
before do
169
stub_const('MyCustomElement', Class.new(Selenium::WebDriver::Element))
170
described_class.element_class = MyCustomElement
171
end
172
173
after do
174
described_class.element_class = nil
175
end
176
177
it 'returns a custom element' do
178
expect(bridge.find_element_by(:id, 'test', nil)).to be_an_instance_of(MyCustomElement)
179
end
180
end
181
end
182
183
describe '#find_elements_by' do
184
before do
185
allow(http).to receive(:request)
186
.with(:post, URI('http://localhost/session/foo/elements'), any_args)
187
.and_return('status' => 200, 'value' => [{Element::ELEMENT_KEY => 'bar'}])
188
end
189
190
it 'returns an element' do
191
expect(bridge.find_elements_by(:id, 'test', nil)).to all(be_an_instance_of(Element))
192
end
193
194
context 'when custom element class is used' do
195
before do
196
stub_const('MyCustomElement', Class.new(Selenium::WebDriver::Element))
197
described_class.element_class = MyCustomElement
198
end
199
200
after do
201
described_class.element_class = nil
202
end
203
204
it 'returns a custom element' do
205
expect(bridge.find_elements_by(:id, 'test', nil)).to all(be_an_instance_of(MyCustomElement))
206
end
207
end
208
end
209
end
210
end
211
end # Remote
212
end # WebDriver
213
end # Selenium
214
215