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
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 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 '#add_extension' do
145
it 'adds an extension' do
146
allow(File).to receive(:file?).and_return(true)
147
ext = 'foo.crx'
148
allow_any_instance_of(described_class)
149
.to receive(:encode_file).with(ext).and_return("encoded_#{ext[/([^.]*)/]}")
150
151
options.add_extension(ext)
152
expect(options.extensions).to eq([ext])
153
end
154
155
it 'raises error when the extension file is missing' do
156
allow(File).to receive(:file?).with('/foo/bar').and_return false
157
158
expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)
159
end
160
161
it 'raises error when the extension file is not .crx' do
162
allow(File).to receive(:file?).with('/foo/bar').and_return true
163
164
expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)
165
end
166
end
167
168
describe '#add_encoded_extension' do
169
it 'adds an encoded extension' do
170
options.add_encoded_extension('foo')
171
expect(options.instance_variable_get(:@encoded_extensions)).to include('foo')
172
end
173
end
174
175
describe '#add_argument' do
176
it 'adds a command-line argument' do
177
options.add_argument('foo')
178
expect(options.args).to eq(['foo'])
179
end
180
end
181
182
describe '#add_option' do
183
it 'adds vendor namespaced options with ordered pairs' do
184
options.add_option('foo:bar', {bar: 'foo'})
185
expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
186
end
187
188
it 'adds vendor namespaced options with Hash' do
189
options.add_option('foo:bar' => {bar: 'foo'})
190
expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
191
end
192
end
193
194
describe '#add_preference' do
195
it 'adds a preference' do
196
options.add_preference(:foo, 'bar')
197
expect(options.prefs[:foo]).to eq('bar')
198
end
199
end
200
201
describe '#add_emulation' do
202
it 'add an emulated device by name' do
203
options.add_emulation(device_name: 'iPhone 6')
204
expect(options.emulation).to eq(device_name: 'iPhone 6')
205
end
206
207
it 'adds emulated device metrics' do
208
options.add_emulation(device_metrics: {width: 400})
209
expect(options.emulation).to eq(device_metrics: {width: 400})
210
end
211
212
it 'adds emulated user agent' do
213
options.add_emulation(user_agent: 'foo')
214
expect(options.emulation).to eq(user_agent: 'foo')
215
end
216
end
217
218
describe '#enable_android' do
219
it 'adds default android settings' do
220
options.enable_android
221
222
expect(options.android_package).to eq('com.android.chrome')
223
expect(options.android_activity).to be_nil
224
expect(options.android_device_serial).to be_nil
225
expect(options.android_use_running_app).to be_nil
226
end
227
228
it 'accepts parameters' do
229
options.enable_android(package: 'foo',
230
serial_number: '123',
231
activity: 'qualified',
232
use_running_app: true)
233
expect(options.android_package).to eq('foo')
234
expect(options.android_activity).to eq('qualified')
235
expect(options.android_device_serial).to eq('123')
236
expect(options.android_use_running_app).to be(true)
237
end
238
end
239
240
describe '#as_json' do
241
it 'returns empty options by default' do
242
expect(options.as_json).to eq('browserName' => 'chrome', 'goog:chromeOptions' => {})
243
end
244
245
it 'errors when unrecognized capability is passed' do
246
options.add_option(:foo, 'bar')
247
248
expect {
249
options.as_json
250
}.to raise_error(Error::WebDriverError, /These options are not w3c compliant: \{:?foo[:=][ >]"bar"\}/)
251
end
252
253
it 'returns added options' do
254
options.add_option('foo:bar', {foo: 'bar'})
255
expect(options.as_json).to eq('browserName' => 'chrome',
256
'foo:bar' => {'foo' => 'bar'},
257
'goog:chromeOptions' => {})
258
end
259
260
it 'converts profile' do
261
profile = Profile.new
262
directory = profile.directory
263
264
opts = described_class.new(profile: profile)
265
expect(opts.as_json).to eq('browserName' => 'chrome',
266
'goog:chromeOptions' =>
267
{'args' => ["--user-data-dir=#{directory}"]})
268
end
269
270
it 'returns a JSON hash' do
271
allow(File).to receive(:file?).and_return(true)
272
allow_any_instance_of(described_class)
273
.to receive(:encode_extension).with('foo.crx').and_return('encoded_foo')
274
allow_any_instance_of(described_class)
275
.to receive(:encode_extension).with('bar.crx').and_return('encoded_bar')
276
277
opts = described_class.new(browser_version: '75',
278
platform_name: 'win10',
279
accept_insecure_certs: false,
280
page_load_strategy: :eager,
281
unhandled_prompt_behavior: :accept_and_notify,
282
strict_file_interactability: true,
283
timeouts: {script: 40000,
284
page_load: 400000,
285
implicit: 1},
286
set_window_rect: false,
287
args: %w[foo bar],
288
prefs: {foo: 'bar',
289
key_that_should_not_be_camelcased: 'baz',
290
nested_one: {nested_two: 'bazbar'}},
291
binary: '/foo/bar',
292
extensions: ['foo.crx', 'bar.crx'],
293
encoded_extensions: ['encoded_foobar'],
294
emulation: {device_name: :mine},
295
local_state: {
296
foo: 'bar',
297
key_that_should_not_be_camelcased: 'baz'
298
},
299
detach: true,
300
debugger_address: '127.0.0.1:8181',
301
exclude_switches: %w[foobar barfoo],
302
minidump_path: 'linux/only',
303
perf_logging_prefs: {enable_network: true},
304
window_types: %w[normal devtools],
305
android_package: 'package',
306
android_activity: 'activity',
307
android_device_serial: '123',
308
android_use_running_app: true,
309
'custom:options': {foo: 'bar'})
310
311
key = 'goog:chromeOptions'
312
expect(opts.as_json).to eq('browserName' => 'chrome',
313
'browserVersion' => '75',
314
'platformName' => 'win10',
315
'acceptInsecureCerts' => false,
316
'pageLoadStrategy' => 'eager',
317
'unhandledPromptBehavior' => 'accept and notify',
318
'strictFileInteractability' => true,
319
'timeouts' => {'script' => 40000,
320
'pageLoad' => 400000,
321
'implicit' => 1},
322
'setWindowRect' => false,
323
'custom:options' => {'foo' => 'bar'},
324
key => {'args' => %w[foo bar],
325
'prefs' => {'foo' => 'bar',
326
'key_that_should_not_be_camelcased' => 'baz',
327
'nested_one' => {'nested_two' => 'bazbar'}},
328
'binary' => '/foo/bar',
329
'extensions' => %w[encoded_foobar encoded_foo encoded_bar],
330
'mobileEmulation' => {'deviceName' => 'mine'},
331
'localState' => {
332
'foo' => 'bar',
333
'key_that_should_not_be_camelcased' => 'baz'
334
},
335
'detach' => true,
336
'debuggerAddress' => '127.0.0.1:8181',
337
'excludeSwitches' => %w[foobar barfoo],
338
'minidumpPath' => 'linux/only',
339
'perfLoggingPrefs' => {'enableNetwork' => true},
340
'windowTypes' => %w[normal devtools],
341
'androidPackage' => 'package',
342
'androidActivity' => 'activity',
343
'androidDeviceSerial' => '123',
344
'androidUseRunningApp' => true})
345
end
346
end
347
end
348
end # Chrome
349
end # WebDriver
350
end # Selenium
351
352