Path: blob/trunk/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require File.expand_path('../spec_helper', __dir__)2021module Selenium22module WebDriver23module Chrome24describe Options do25subject(:options) { described_class.new }2627describe '#initialize' do28it 'accepts defined parameters' do29allow(File).to receive(:file?).and_return(true)3031opts = described_class.new(browser_version: '75',32platform_name: 'win10',33accept_insecure_certs: false,34page_load_strategy: 'eager',35unhandled_prompt_behavior: 'accept',36strict_file_interactability: true,37timeouts: {script: 40000,38page_load: 400000,39implicit: 1},40set_window_rect: false,41args: %w[foo bar],42prefs: {foo: 'bar'},43binary: '/foo/bar',44extensions: ['foo.crx', 'bar.crx'],45encoded_extensions: ['encoded_foobar'],46foo: 'bar',47emulation: {device_name: :bar},48local_state: {foo: 'bar'},49detach: true,50debugger_address: '127.0.0.1:8181',51exclude_switches: %w[foobar barfoo],52minidump_path: 'linux/only',53perf_logging_prefs: {enable_network: true},54window_types: %w[normal devtools],55android_package: 'package',56android_activity: 'activity',57android_device_serial: '123',58android_use_running_app: true,59'custom:options': {foo: 'bar'})6061expect(opts.args).to eq(%w[foo bar])62expect(opts.prefs[:foo]).to eq('bar')63expect(opts.binary).to eq('/foo/bar')64expect(opts.extensions).to eq(['foo.crx', 'bar.crx'])65expect(opts.instance_variable_get(:@options)[:foo]).to eq('bar')66expect(opts.emulation[:device_name]).to eq(:bar)67expect(opts.local_state[:foo]).to eq('bar')68expect(opts.detach).to be(true)69expect(opts.debugger_address).to eq('127.0.0.1:8181')70expect(opts.exclude_switches).to eq(%w[foobar barfoo])71expect(opts.minidump_path).to eq('linux/only')72expect(opts.perf_logging_prefs[:enable_network]).to be(true)73expect(opts.window_types).to eq(%w[normal devtools])74expect(opts.browser_name).to eq('chrome')75expect(opts.browser_version).to eq('75')76expect(opts.platform_name).to eq('win10')77expect(opts.accept_insecure_certs).to be(false)78expect(opts.page_load_strategy).to eq('eager')79expect(opts.unhandled_prompt_behavior).to eq('accept')80expect(opts.strict_file_interactability).to be(true)81expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1)82expect(opts.set_window_rect).to be(false)83expect(opts.android_package).to eq('package')84expect(opts.android_activity).to eq('activity')85expect(opts.android_device_serial).to eq('123')86expect(opts.android_use_running_app).to be(true)87expect(opts.options[:'custom:options']).to eq(foo: 'bar')88end89end9091describe 'accessors' do92it 'adds a command-line argument' do93options.args << 'foo'94expect(options.args).to eq(['foo'])95end9697it 'adds an extension' do98allow(File).to receive(:file?).and_return(true)99ext = 'foo.crx'100allow_any_instance_of(described_class)101.to receive(:encode_file).with(ext).and_return("encoded_#{ext[/([^.]*)/]}")102103options.extensions << ext104expect(options.extensions).to eq([ext])105end106107it 'sets the binary path' do108options.binary = '/foo/bar'109expect(options.binary).to eq('/foo/bar')110end111112it 'adds a preference' do113options.prefs[:foo] = 'bar'114expect(options.prefs[:foo]).to eq('bar')115end116117it 'add an emulated device by name' do118options.emulation[:device_name] = 'iPhone 6'119expect(options.emulation).to eq(device_name: 'iPhone 6')120end121122it 'adds local state' do123options.local_state[:foo] = 'bar'124expect(options.local_state).to eq(foo: 'bar')125end126127it 'adds a switch to exclude' do128options.exclude_switches << 'exclude-this'129expect(options.exclude_switches).to eq(['exclude-this'])130end131132it 'adds performance logging preferences' do133options.perf_logging_prefs[:enable_network] = true134expect(options.perf_logging_prefs).to eq(enable_network: true)135end136137it 'adds a window type' do138options.window_types << 'normal'139expect(options.window_types).to eq(['normal'])140end141end142143describe '#add_extension' do144it 'adds an extension' do145allow(File).to receive(:file?).and_return(true)146ext = 'foo.crx'147allow_any_instance_of(described_class)148.to receive(:encode_file).with(ext).and_return("encoded_#{ext[/([^.]*)/]}")149150options.add_extension(ext)151expect(options.extensions).to eq([ext])152end153154it 'raises error when the extension file is missing' do155allow(File).to receive(:file?).with('/foo/bar').and_return false156157expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)158end159160it 'raises error when the extension file is not .crx' do161allow(File).to receive(:file?).with('/foo/bar').and_return true162163expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)164end165end166167describe '#add_encoded_extension' do168it 'adds an encoded extension' do169options.add_encoded_extension('foo')170expect(options.instance_variable_get(:@encoded_extensions)).to include('foo')171end172end173174describe '#add_argument' do175it 'adds a command-line argument' do176options.add_argument('foo')177expect(options.args).to eq(['foo'])178end179end180181describe '#add_option' do182it 'adds vendor namespaced options with ordered pairs' do183options.add_option('foo:bar', {bar: 'foo'})184expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})185end186187it 'adds vendor namespaced options with Hash' do188options.add_option('foo:bar' => {bar: 'foo'})189expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})190end191end192193describe '#add_preference' do194it 'adds a preference' do195options.add_preference(:foo, 'bar')196expect(options.prefs[:foo]).to eq('bar')197end198end199200describe '#add_emulation' do201it 'add an emulated device by name' do202options.add_emulation(device_name: 'iPhone 6')203expect(options.emulation).to eq(device_name: 'iPhone 6')204end205206it 'adds emulated device metrics' do207options.add_emulation(device_metrics: {width: 400})208expect(options.emulation).to eq(device_metrics: {width: 400})209end210211it 'adds emulated user agent' do212options.add_emulation(user_agent: 'foo')213expect(options.emulation).to eq(user_agent: 'foo')214end215end216217describe '#enable_android' do218it 'adds default android settings' do219options.enable_android220221expect(options.android_package).to eq('com.android.chrome')222expect(options.android_activity).to be_nil223expect(options.android_device_serial).to be_nil224expect(options.android_use_running_app).to be_nil225end226227it 'accepts parameters' do228options.enable_android(package: 'foo',229serial_number: '123',230activity: 'qualified',231use_running_app: true)232expect(options.android_package).to eq('foo')233expect(options.android_activity).to eq('qualified')234expect(options.android_device_serial).to eq('123')235expect(options.android_use_running_app).to be(true)236end237end238239describe '#as_json' do240it 'returns empty options by default' do241expect(options.as_json).to eq('browserName' => 'chrome', 'goog:chromeOptions' => {})242end243244it 'errors when unrecognized capability is passed' do245options.add_option(:foo, 'bar')246247expect {248options.as_json249}.to raise_error(Error::WebDriverError, /These options are not w3c compliant: \{:?foo[:=][ >]"bar"\}/)250end251252it 'returns added options' do253options.add_option('foo:bar', {foo: 'bar'})254expect(options.as_json).to eq('browserName' => 'chrome',255'foo:bar' => {'foo' => 'bar'},256'goog:chromeOptions' => {})257end258259it 'converts profile' do260profile = Profile.new261directory = profile.directory262263opts = described_class.new(profile: profile)264expect(opts.as_json).to eq('browserName' => 'chrome',265'goog:chromeOptions' =>266{'args' => ["--user-data-dir=#{directory}"]})267end268269it 'returns a JSON hash' do270allow(File).to receive(:file?).and_return(true)271allow_any_instance_of(described_class)272.to receive(:encode_extension).with('foo.crx').and_return('encoded_foo')273allow_any_instance_of(described_class)274.to receive(:encode_extension).with('bar.crx').and_return('encoded_bar')275276opts = described_class.new(browser_version: '75',277platform_name: 'win10',278accept_insecure_certs: false,279page_load_strategy: :eager,280unhandled_prompt_behavior: :accept_and_notify,281strict_file_interactability: true,282timeouts: {script: 40000,283page_load: 400000,284implicit: 1},285set_window_rect: false,286args: %w[foo bar],287prefs: {foo: 'bar',288key_that_should_not_be_camelcased: 'baz',289nested_one: {nested_two: 'bazbar'}},290binary: '/foo/bar',291extensions: ['foo.crx', 'bar.crx'],292encoded_extensions: ['encoded_foobar'],293emulation: {device_name: :mine},294local_state: {295foo: 'bar',296key_that_should_not_be_camelcased: 'baz'297},298detach: true,299debugger_address: '127.0.0.1:8181',300exclude_switches: %w[foobar barfoo],301minidump_path: 'linux/only',302perf_logging_prefs: {enable_network: true},303window_types: %w[normal devtools],304android_package: 'package',305android_activity: 'activity',306android_device_serial: '123',307android_use_running_app: true,308'custom:options': {foo: 'bar'})309310key = 'goog:chromeOptions'311expect(opts.as_json).to eq('browserName' => 'chrome',312'browserVersion' => '75',313'platformName' => 'win10',314'acceptInsecureCerts' => false,315'pageLoadStrategy' => 'eager',316'unhandledPromptBehavior' => 'accept and notify',317'strictFileInteractability' => true,318'timeouts' => {'script' => 40000,319'pageLoad' => 400000,320'implicit' => 1},321'setWindowRect' => false,322'custom:options' => {'foo' => 'bar'},323key => {'args' => %w[foo bar],324'prefs' => {'foo' => 'bar',325'key_that_should_not_be_camelcased' => 'baz',326'nested_one' => {'nested_two' => 'bazbar'}},327'binary' => '/foo/bar',328'extensions' => %w[encoded_foobar encoded_foo encoded_bar],329'mobileEmulation' => {'deviceName' => 'mine'},330'localState' => {331'foo' => 'bar',332'key_that_should_not_be_camelcased' => 'baz'333},334'detach' => true,335'debuggerAddress' => '127.0.0.1:8181',336'excludeSwitches' => %w[foobar barfoo],337'minidumpPath' => 'linux/only',338'perfLoggingPrefs' => {'enableNetwork' => true},339'windowTypes' => %w[normal devtools],340'androidPackage' => 'package',341'androidActivity' => 'activity',342'androidDeviceSerial' => '123',343'androidUseRunningApp' => true})344end345end346end347end # Chrome348end # WebDriver349end # Selenium350351352