Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/edge/options_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 Edge
25
describe Options do
26
subject(:options) { described_class.new }
27
28
describe '#initialize' do
29
it 'accepts defined parameters' do
30
allow(File).to receive(:file?).and_return(true)
31
32
opts = described_class.new(browser_version: '75',
33
platform_name: 'win10',
34
accept_insecure_certs: false,
35
page_load_strategy: 'eager',
36
unhandled_prompt_behavior: 'accept',
37
strict_file_interactability: true,
38
timeouts: {script: 40000,
39
page_load: 400000,
40
implicit: 1},
41
set_window_rect: false,
42
args: %w[foo bar],
43
prefs: {foo: 'bar'},
44
binary: '/foo/bar',
45
extensions: ['foo.crx', 'bar.crx'],
46
encoded_extensions: ['encoded_foobar'],
47
foo: 'bar',
48
emulation: {device_name: :bar},
49
local_state: {foo: 'bar'},
50
detach: true,
51
debugger_address: '127.0.0.1:8181',
52
exclude_switches: %w[foobar barfoo],
53
minidump_path: 'linux/only',
54
perf_logging_prefs: {enable_network: true},
55
window_types: %w[normal devtools],
56
android_package: 'package',
57
android_activity: 'activity',
58
android_device_serial: '123',
59
android_use_running_app: true,
60
'custom:options': {foo: 'bar'})
61
62
expect(opts.args.to_a).to eq(%w[foo bar])
63
expect(opts.prefs[:foo]).to eq('bar')
64
expect(opts.binary).to eq('/foo/bar')
65
expect(opts.extensions).to eq(['foo.crx', 'bar.crx'])
66
expect(opts.instance_variable_get(:@options)[:foo]).to eq('bar')
67
expect(opts.emulation[:device_name]).to eq(:bar)
68
expect(opts.local_state[:foo]).to eq('bar')
69
expect(opts.detach).to be(true)
70
expect(opts.debugger_address).to eq('127.0.0.1:8181')
71
expect(opts.exclude_switches).to eq(%w[foobar barfoo])
72
expect(opts.minidump_path).to eq('linux/only')
73
expect(opts.perf_logging_prefs[:enable_network]).to be(true)
74
expect(opts.window_types).to eq(%w[normal devtools])
75
expect(opts.browser_name).to eq('MicrosoftEdge')
76
expect(opts.browser_version).to eq('75')
77
expect(opts.platform_name).to eq('win10')
78
expect(opts.accept_insecure_certs).to be(false)
79
expect(opts.page_load_strategy).to eq('eager')
80
expect(opts.unhandled_prompt_behavior).to eq('accept')
81
expect(opts.strict_file_interactability).to be(true)
82
expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1)
83
expect(opts.set_window_rect).to be(false)
84
expect(opts.android_package).to eq('package')
85
expect(opts.android_activity).to eq('activity')
86
expect(opts.android_device_serial).to eq('123')
87
expect(opts.android_use_running_app).to be(true)
88
expect(opts.options[:'custom:options']).to eq(foo: 'bar')
89
end
90
end
91
92
describe '#add_extension' do
93
it 'adds an extension' do
94
allow(File).to receive(:file?).and_return(true)
95
ext = 'foo.crx'
96
allow_any_instance_of(described_class)
97
.to receive(:encode_file).with(ext).and_return("encoded_#{ext[/([^.]*)/]}")
98
99
options.add_extension(ext)
100
expect(options.extensions).to eq([ext])
101
end
102
103
it 'raises error when the extension file is missing' do
104
allow(File).to receive(:file?).with('/foo/bar').and_return false
105
106
expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)
107
end
108
109
it 'raises error when the extension file is not .crx' do
110
allow(File).to receive(:file?).with('/foo/bar').and_return true
111
112
expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)
113
end
114
end
115
116
describe '#add_encoded_extension' do
117
it 'adds an encoded extension' do
118
options.add_encoded_extension('foo')
119
expect(options.instance_variable_get(:@encoded_extensions)).to include('foo')
120
end
121
end
122
123
describe '#binary=' do
124
it 'sets the binary path' do
125
options.binary = '/foo/bar'
126
expect(options.binary).to eq('/foo/bar')
127
end
128
end
129
130
describe '#add_argument' do
131
it 'adds a command-line argument' do
132
options.add_argument('foo')
133
expect(options.args.to_a).to eq(['foo'])
134
end
135
end
136
137
describe '#add_option' do
138
it 'adds vendor namespaced options with ordered pairs' do
139
options.add_option('foo:bar', {bar: 'foo'})
140
expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
141
end
142
143
it 'adds vendor namespaced options with Hash' do
144
options.add_option('foo:bar' => {bar: 'foo'})
145
expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
146
end
147
end
148
149
describe 'uses webview2 for MS Edge Driver' do
150
it 'changes browserName to webview2' do
151
options.webview2!
152
expect(options.browser_name).to eq('webview2')
153
end
154
end
155
156
describe '#add_preference' do
157
it 'adds a preference' do
158
options.add_preference(:foo, 'bar')
159
expect(options.prefs[:foo]).to eq('bar')
160
end
161
end
162
163
describe '#add_emulation' do
164
it 'add an emulated device by name' do
165
options.add_emulation(device_name: 'iPhone 6')
166
expect(options.emulation).to eq(device_name: 'iPhone 6')
167
end
168
169
it 'adds emulated device metrics' do
170
options.add_emulation(device_metrics: {width: 400})
171
expect(options.emulation).to eq(device_metrics: {width: 400})
172
end
173
174
it 'adds emulated user agent' do
175
options.add_emulation(user_agent: 'foo')
176
expect(options.emulation).to eq(user_agent: 'foo')
177
end
178
end
179
180
describe '#enable_android' do
181
it 'adds default android settings' do
182
options.enable_android
183
184
expect(options.android_package).to eq('com.android.chrome')
185
expect(options.android_activity).to be_nil
186
expect(options.android_device_serial).to be_nil
187
expect(options.android_use_running_app).to be_nil
188
end
189
190
it 'accepts parameters' do
191
options.enable_android(package: 'foo',
192
serial_number: '123',
193
activity: 'qualified',
194
use_running_app: true)
195
expect(options.android_package).to eq('foo')
196
expect(options.android_activity).to eq('qualified')
197
expect(options.android_device_serial).to eq('123')
198
expect(options.android_use_running_app).to be(true)
199
end
200
end
201
202
describe '#as_json' do
203
it 'returns empty options by default' do
204
expect(options.as_json).to eq('browserName' => 'MicrosoftEdge', 'ms:edgeOptions' => {})
205
end
206
207
it 'errors when unrecognized capability is passed' do
208
options.add_option(:foo, 'bar')
209
210
expect {
211
options.as_json
212
}.to raise_error(Error::WebDriverError, /These options are not w3c compliant: \{:?foo[:=][ >]"bar"\}/)
213
end
214
215
it 'returns added options' do
216
options.add_option('foo:bar', {foo: 'bar'})
217
expect(options.as_json).to eq('browserName' => 'MicrosoftEdge',
218
'foo:bar' => {'foo' => 'bar'},
219
'ms:edgeOptions' => {})
220
end
221
222
it 'returns a JSON hash' do
223
allow(File).to receive(:file?).and_return(true)
224
allow_any_instance_of(described_class)
225
.to receive(:encode_extension).with('foo.crx').and_return('encoded_foo')
226
allow_any_instance_of(described_class)
227
.to receive(:encode_extension).with('bar.crx').and_return('encoded_bar')
228
229
opts = described_class.new(browser_version: '75',
230
platform_name: 'win10',
231
accept_insecure_certs: false,
232
page_load_strategy: 'eager',
233
unhandled_prompt_behavior: 'accept',
234
strict_file_interactability: true,
235
timeouts: {script: 40000,
236
page_load: 400000,
237
implicit: 1},
238
set_window_rect: false,
239
args: %w[foo bar],
240
prefs: {foo: 'bar',
241
key_that_should_not_be_camelcased: 'baz'},
242
binary: '/foo/bar',
243
extensions: ['foo.crx', 'bar.crx'],
244
encoded_extensions: ['encoded_foobar'],
245
emulation: {device_name: :mine},
246
local_state: {foo: 'bar'},
247
detach: true,
248
debugger_address: '127.0.0.1:8181',
249
exclude_switches: %w[foobar barfoo],
250
minidump_path: 'linux/only',
251
perf_logging_prefs: {enable_network: true},
252
window_types: %w[normal devtools],
253
android_package: 'package',
254
android_activity: 'activity',
255
android_device_serial: '123',
256
android_use_running_app: true,
257
'custom:options': {foo: 'bar'})
258
259
key = 'ms:edgeOptions'
260
expect(opts.as_json).to eq('browserName' => 'MicrosoftEdge',
261
'browserVersion' => '75',
262
'platformName' => 'win10',
263
'acceptInsecureCerts' => false,
264
'pageLoadStrategy' => 'eager',
265
'unhandledPromptBehavior' => 'accept',
266
'strictFileInteractability' => true,
267
'timeouts' => {'script' => 40000,
268
'pageLoad' => 400000,
269
'implicit' => 1},
270
'setWindowRect' => false,
271
'custom:options' => {'foo' => 'bar'},
272
key => {'args' => %w[foo bar],
273
'prefs' => {'foo' => 'bar',
274
'key_that_should_not_be_camelcased' => 'baz'},
275
'binary' => '/foo/bar',
276
'extensions' => %w[encoded_foobar encoded_foo encoded_bar],
277
'mobileEmulation' => {'deviceName' => 'mine'},
278
'localState' => {'foo' => 'bar'},
279
'detach' => true,
280
'debuggerAddress' => '127.0.0.1:8181',
281
'excludeSwitches' => %w[foobar barfoo],
282
'minidumpPath' => 'linux/only',
283
'perfLoggingPrefs' => {'enableNetwork' => true},
284
'windowTypes' => %w[normal devtools],
285
'androidPackage' => 'package',
286
'androidActivity' => 'activity',
287
'androidDeviceSerial' => '123',
288
'androidUseRunningApp' => true})
289
end
290
end
291
end
292
end # Edge
293
end # WebDriver
294
end # Selenium
295
296