Path: blob/trunk/rb/spec/unit/selenium/webdriver/edge/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 Edge24describe 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_a).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('MicrosoftEdge')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 '#add_extension' do92it 'adds an extension' do93allow(File).to receive(:file?).and_return(true)94ext = 'foo.crx'95allow_any_instance_of(described_class)96.to receive(:encode_file).with(ext).and_return("encoded_#{ext[/([^.]*)/]}")9798options.add_extension(ext)99expect(options.extensions).to eq([ext])100end101102it 'raises error when the extension file is missing' do103allow(File).to receive(:file?).with('/foo/bar').and_return false104105expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)106end107108it 'raises error when the extension file is not .crx' do109allow(File).to receive(:file?).with('/foo/bar').and_return true110111expect { options.add_extension('/foo/bar') }.to raise_error(Error::WebDriverError)112end113end114115describe '#add_encoded_extension' do116it 'adds an encoded extension' do117options.add_encoded_extension('foo')118expect(options.instance_variable_get(:@encoded_extensions)).to include('foo')119end120end121122describe '#binary=' do123it 'sets the binary path' do124options.binary = '/foo/bar'125expect(options.binary).to eq('/foo/bar')126end127end128129describe '#add_argument' do130it 'adds a command-line argument' do131options.add_argument('foo')132expect(options.args.to_a).to eq(['foo'])133end134end135136describe '#add_option' do137it 'adds vendor namespaced options with ordered pairs' do138options.add_option('foo:bar', {bar: 'foo'})139expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})140end141142it 'adds vendor namespaced options with Hash' do143options.add_option('foo:bar' => {bar: 'foo'})144expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})145end146end147148describe 'uses webview2 for MS Edge Driver' do149it 'changes browserName to webview2' do150options.webview2!151expect(options.browser_name).to eq('webview2')152end153end154155describe '#add_preference' do156it 'adds a preference' do157options.add_preference(:foo, 'bar')158expect(options.prefs[:foo]).to eq('bar')159end160end161162describe '#add_emulation' do163it 'add an emulated device by name' do164options.add_emulation(device_name: 'iPhone 6')165expect(options.emulation).to eq(device_name: 'iPhone 6')166end167168it 'adds emulated device metrics' do169options.add_emulation(device_metrics: {width: 400})170expect(options.emulation).to eq(device_metrics: {width: 400})171end172173it 'adds emulated user agent' do174options.add_emulation(user_agent: 'foo')175expect(options.emulation).to eq(user_agent: 'foo')176end177end178179describe '#enable_android' do180it 'adds default android settings' do181options.enable_android182183expect(options.android_package).to eq('com.android.chrome')184expect(options.android_activity).to be_nil185expect(options.android_device_serial).to be_nil186expect(options.android_use_running_app).to be_nil187end188189it 'accepts parameters' do190options.enable_android(package: 'foo',191serial_number: '123',192activity: 'qualified',193use_running_app: true)194expect(options.android_package).to eq('foo')195expect(options.android_activity).to eq('qualified')196expect(options.android_device_serial).to eq('123')197expect(options.android_use_running_app).to be(true)198end199end200201describe '#as_json' do202it 'returns empty options by default' do203expect(options.as_json).to eq('browserName' => 'MicrosoftEdge', 'ms:edgeOptions' => {})204end205206it 'errors when unrecognized capability is passed' do207options.add_option(:foo, 'bar')208209expect {210options.as_json211}.to raise_error(Error::WebDriverError, /These options are not w3c compliant: \{:?foo[:=][ >]"bar"\}/)212end213214it 'returns added options' do215options.add_option('foo:bar', {foo: 'bar'})216expect(options.as_json).to eq('browserName' => 'MicrosoftEdge',217'foo:bar' => {'foo' => 'bar'},218'ms:edgeOptions' => {})219end220221it 'returns a JSON hash' do222allow(File).to receive(:file?).and_return(true)223allow_any_instance_of(described_class)224.to receive(:encode_extension).with('foo.crx').and_return('encoded_foo')225allow_any_instance_of(described_class)226.to receive(:encode_extension).with('bar.crx').and_return('encoded_bar')227228opts = described_class.new(browser_version: '75',229platform_name: 'win10',230accept_insecure_certs: false,231page_load_strategy: 'eager',232unhandled_prompt_behavior: 'accept',233strict_file_interactability: true,234timeouts: {script: 40000,235page_load: 400000,236implicit: 1},237set_window_rect: false,238args: %w[foo bar],239prefs: {foo: 'bar',240key_that_should_not_be_camelcased: 'baz'},241binary: '/foo/bar',242extensions: ['foo.crx', 'bar.crx'],243encoded_extensions: ['encoded_foobar'],244emulation: {device_name: :mine},245local_state: {foo: 'bar'},246detach: true,247debugger_address: '127.0.0.1:8181',248exclude_switches: %w[foobar barfoo],249minidump_path: 'linux/only',250perf_logging_prefs: {enable_network: true},251window_types: %w[normal devtools],252android_package: 'package',253android_activity: 'activity',254android_device_serial: '123',255android_use_running_app: true,256'custom:options': {foo: 'bar'})257258key = 'ms:edgeOptions'259expect(opts.as_json).to eq('browserName' => 'MicrosoftEdge',260'browserVersion' => '75',261'platformName' => 'win10',262'acceptInsecureCerts' => false,263'pageLoadStrategy' => 'eager',264'unhandledPromptBehavior' => 'accept',265'strictFileInteractability' => true,266'timeouts' => {'script' => 40000,267'pageLoad' => 400000,268'implicit' => 1},269'setWindowRect' => false,270'custom:options' => {'foo' => 'bar'},271key => {'args' => %w[foo bar],272'prefs' => {'foo' => 'bar',273'key_that_should_not_be_camelcased' => 'baz'},274'binary' => '/foo/bar',275'extensions' => %w[encoded_foobar encoded_foo encoded_bar],276'mobileEmulation' => {'deviceName' => 'mine'},277'localState' => {'foo' => 'bar'},278'detach' => true,279'debuggerAddress' => '127.0.0.1:8181',280'excludeSwitches' => %w[foobar barfoo],281'minidumpPath' => 'linux/only',282'perfLoggingPrefs' => {'enableNetwork' => true},283'windowTypes' => %w[normal devtools],284'androidPackage' => 'package',285'androidActivity' => 'activity',286'androidDeviceSerial' => '123',287'androidUseRunningApp' => true})288end289end290end291end # Edge292end # WebDriver293end # Selenium294295296