Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
4103 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 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 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('chrome')
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 'accessors' do
93
it 'adds a command-line argument' do
94
options.args << 'foo'
95
expect(options.args).to eq(['foo'])
96
end
97
98
it 'adds an extension' do
99
allow(File).to receive(:file?).and_return(true)
100
ext = 'foo.crx'
101
allow_any_instance_of(described_class)
102
.to receive(:encode_file).with(ext).and_return("encoded_#{ext[/([^.]*)/]}")
103
104
options.extensions << ext
105
expect(options.extensions).to eq([ext])
106
end
107
108
it 'sets the binary path' do
109
options.binary = '/foo/bar'
110
expect(options.binary).to eq('/foo/bar')
111
end
112
113
it 'adds a preference' do
114
options.prefs[:foo] = 'bar'
115
expect(options.prefs[:foo]).to eq('bar')
116
end
117
118
it 'add an emulated device by name' do
119
options.emulation[:device_name] = 'iPhone 6'
120
expect(options.emulation).to eq(device_name: 'iPhone 6')
121
end
122
123
it 'adds local state' do
124
options.local_state[:foo] = 'bar'
125
expect(options.local_state).to eq(foo: 'bar')
126
end
127
128
it 'adds a switch to exclude' do
129
options.exclude_switches << 'exclude-this'
130
expect(options.exclude_switches).to eq(['exclude-this'])
131
end
132
133
it 'adds performance logging preferences' do
134
options.perf_logging_prefs[:enable_network] = true
135
expect(options.perf_logging_prefs).to eq(enable_network: true)
136
end
137
138
it 'adds a window type' do
139
options.window_types << 'normal'
140
expect(options.window_types).to eq(['normal'])
141
end
142
end
143
144
describe '#enable_bidi!' do
145
it 'allows setting and querying bidi' do
146
expect(options.web_socket_url).to be_nil
147
expect(options.bidi?).to be false
148
149
options.enable_bidi!
150
151
expect(options.bidi?).to be true
152
expect(options.web_socket_url).to be true
153
end
154
end
155
156
describe '#add_extension' do
157
it 'adds an extension' do
158
allow(File).to receive(:file?).and_return(true)
159
ext = 'foo.crx'
160
allow_any_instance_of(described_class)
161
.to receive(:encode_file).with(ext).and_return("encoded_#{ext[/([^.]*)/]}")
162
163
options.add_extension(ext)
164
expect(options.extensions).to eq([ext])
165
end
166
167
it 'raises error when the extension file is missing' do
168
allow(File).to receive(:file?).with('/foo/bar').and_return false
169
170
expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)
171
end
172
173
it 'raises error when the extension file is not .crx' do
174
allow(File).to receive(:file?).with('/foo/bar').and_return true
175
176
expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)
177
end
178
end
179
180
describe '#add_encoded_extension' do
181
it 'adds an encoded extension' do
182
options.add_encoded_extension('foo')
183
expect(options.instance_variable_get(:@encoded_extensions)).to include('foo')
184
end
185
end
186
187
describe '#add_argument' do
188
it 'adds a command-line argument' do
189
options.add_argument('foo')
190
expect(options.args).to eq(['foo'])
191
end
192
end
193
194
describe '#add_option' do
195
it 'adds vendor namespaced options with ordered pairs' do
196
options.add_option('foo:bar', {bar: 'foo'})
197
expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
198
end
199
200
it 'adds vendor namespaced options with Hash' do
201
options.add_option('foo:bar' => {bar: 'foo'})
202
expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
203
end
204
end
205
206
describe '#add_preference' do
207
it 'adds a preference' do
208
options.add_preference(:foo, 'bar')
209
expect(options.prefs[:foo]).to eq('bar')
210
end
211
end
212
213
describe '#add_emulation' do
214
it 'add an emulated device by name' do
215
options.add_emulation(device_name: 'iPhone 6')
216
expect(options.emulation).to eq(device_name: 'iPhone 6')
217
end
218
219
it 'adds emulated device metrics' do
220
options.add_emulation(device_metrics: {width: 400})
221
expect(options.emulation).to eq(device_metrics: {width: 400})
222
end
223
224
it 'adds emulated user agent' do
225
options.add_emulation(user_agent: 'foo')
226
expect(options.emulation).to eq(user_agent: 'foo')
227
end
228
end
229
230
describe '#enable_android' do
231
it 'adds default android settings' do
232
options.enable_android
233
234
expect(options.android_package).to eq('com.android.chrome')
235
expect(options.android_activity).to be_nil
236
expect(options.android_device_serial).to be_nil
237
expect(options.android_use_running_app).to be_nil
238
end
239
240
it 'accepts parameters' do
241
options.enable_android(package: 'foo',
242
serial_number: '123',
243
activity: 'qualified',
244
use_running_app: true)
245
expect(options.android_package).to eq('foo')
246
expect(options.android_activity).to eq('qualified')
247
expect(options.android_device_serial).to eq('123')
248
expect(options.android_use_running_app).to be(true)
249
end
250
end
251
252
describe '#as_json' do
253
it 'returns empty options by default' do
254
expect(options.as_json).to eq('browserName' => 'chrome', 'goog:chromeOptions' => {})
255
end
256
257
it 'errors when unrecognized capability is passed' do
258
options.add_option(:foo, 'bar')
259
260
expect {
261
options.as_json
262
}.to raise_error(Error::WebDriverError, /These options are not w3c compliant: \{:?foo[:=][ >]"bar"\}/)
263
end
264
265
it 'returns added options' do
266
options.add_option('foo:bar', {foo: 'bar'})
267
expect(options.as_json).to eq('browserName' => 'chrome',
268
'foo:bar' => {'foo' => 'bar'},
269
'goog:chromeOptions' => {})
270
end
271
272
it 'converts profile' do
273
profile = Profile.new
274
directory = profile.directory
275
276
opts = described_class.new(profile: profile)
277
expect(opts.as_json).to eq('browserName' => 'chrome',
278
'goog:chromeOptions' =>
279
{'args' => ["--user-data-dir=#{directory}"]})
280
end
281
282
it 'processes unhandled_prompt_behavior hash values' do
283
opts = described_class.new(unhandled_prompt_behavior: {
284
alert: :accept_and_notify,
285
confirm: 'dismiss_and_notify',
286
prompt: :ignore,
287
before_unload: 'accept',
288
default: :dismiss
289
})
290
291
expect(opts.as_json).to eq('browserName' => 'chrome',
292
'unhandledPromptBehavior' => {
293
'alert' => 'accept and notify',
294
'confirm' => 'dismiss and notify',
295
'prompt' => 'ignore',
296
'beforeUnload' => 'accept',
297
'default' => 'dismiss'
298
},
299
'goog:chromeOptions' => {})
300
end
301
302
it 'returns a JSON hash' do
303
allow(File).to receive(:file?).and_return(true)
304
allow_any_instance_of(described_class)
305
.to receive(:encode_extension).with('foo.crx').and_return('encoded_foo')
306
allow_any_instance_of(described_class)
307
.to receive(:encode_extension).with('bar.crx').and_return('encoded_bar')
308
309
opts = described_class.new(browser_version: '75',
310
platform_name: 'win10',
311
accept_insecure_certs: false,
312
page_load_strategy: :eager,
313
unhandled_prompt_behavior: :accept_and_notify,
314
strict_file_interactability: true,
315
timeouts: {script: 40000,
316
page_load: 400000,
317
implicit: 1},
318
set_window_rect: false,
319
args: %w[foo bar],
320
prefs: {foo: 'bar',
321
key_that_should_not_be_camelcased: 'baz',
322
nested_one: {nested_two: 'bazbar'}},
323
binary: '/foo/bar',
324
extensions: ['foo.crx', 'bar.crx'],
325
encoded_extensions: ['encoded_foobar'],
326
emulation: {device_name: :mine},
327
local_state: {
328
foo: 'bar',
329
key_that_should_not_be_camelcased: 'baz'
330
},
331
detach: true,
332
debugger_address: '127.0.0.1:8181',
333
exclude_switches: %w[foobar barfoo],
334
minidump_path: 'linux/only',
335
perf_logging_prefs: {enable_network: true},
336
window_types: %w[normal devtools],
337
android_package: 'package',
338
android_activity: 'activity',
339
android_device_serial: '123',
340
android_use_running_app: true,
341
'custom:options': {foo: 'bar'})
342
343
key = 'goog:chromeOptions'
344
expect(opts.as_json).to eq('browserName' => 'chrome',
345
'browserVersion' => '75',
346
'platformName' => 'win10',
347
'acceptInsecureCerts' => false,
348
'pageLoadStrategy' => 'eager',
349
'unhandledPromptBehavior' => 'accept and notify',
350
'strictFileInteractability' => true,
351
'timeouts' => {'script' => 40000,
352
'pageLoad' => 400000,
353
'implicit' => 1},
354
'setWindowRect' => false,
355
'custom:options' => {'foo' => 'bar'},
356
key => {'args' => %w[foo bar],
357
'prefs' => {'foo' => 'bar',
358
'key_that_should_not_be_camelcased' => 'baz',
359
'nested_one' => {'nested_two' => 'bazbar'}},
360
'binary' => '/foo/bar',
361
'extensions' => %w[encoded_foobar encoded_foo encoded_bar],
362
'mobileEmulation' => {'deviceName' => 'mine'},
363
'localState' => {
364
'foo' => 'bar',
365
'key_that_should_not_be_camelcased' => 'baz'
366
},
367
'detach' => true,
368
'debuggerAddress' => '127.0.0.1:8181',
369
'excludeSwitches' => %w[foobar barfoo],
370
'minidumpPath' => 'linux/only',
371
'perfLoggingPrefs' => {'enableNetwork' => true},
372
'windowTypes' => %w[normal devtools],
373
'androidPackage' => 'package',
374
'androidActivity' => 'activity',
375
'androidDeviceSerial' => '123',
376
'androidUseRunningApp' => true})
377
end
378
end
379
end
380
end # Chrome
381
end # WebDriver
382
end # Selenium
383
384