Path: blob/trunk/rb/spec/unit/selenium/webdriver/firefox/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 Firefox24describe Options do25subject(:options) { described_class.new }2627describe '#initialize' do28it 'sets provided parameters' do29profile = Profile.new30allow(profile).to receive(:encoded).and_return('encoded_profile')3132opts = described_class.new(browser_version: '66',33platform_name: 'win10',34accept_insecure_certs: false,35page_load_strategy: 'eager',36unhandled_prompt_behavior: 'accept',37strict_file_interactability: true,38timeouts: {script: 40000,39page_load: 400000,40implicit: 1},41set_window_rect: false,42args: %w[foo bar],43binary: '/foo/bar',44prefs: {foo: 'bar'},45env: {'FOO' => 'bar'},46foo: 'bar',47profile: profile,48log_level: :debug,49android_package: 'package',50android_activity: 'activity',51android_device_serial: '123',52android_intent_arguments: %w[foo bar],53'custom:options': {foo: 'bar'})5455expect(opts.args.to_a).to eq(%w[foo bar])56expect(opts.binary).to eq('/foo/bar')57expect(opts.prefs[:foo]).to eq('bar')58expect(opts.env['FOO']).to eq('bar')59expect(opts.instance_variable_get(:@options)[:foo]).to eq('bar')60expect(opts.profile).to eq(profile)61expect(opts.log_level).to eq(:debug)62expect(opts.browser_name).to eq('firefox')63expect(opts.browser_version).to eq('66')64expect(opts.platform_name).to eq('win10')65expect(opts.accept_insecure_certs).to be(false)66expect(opts.page_load_strategy).to eq('eager')67expect(opts.unhandled_prompt_behavior).to eq('accept')68expect(opts.strict_file_interactability).to be(true)69expect(opts.timeouts).to eq(script: 40000, page_load: 400000, implicit: 1)70expect(opts.set_window_rect).to be(false)71expect(opts.android_package).to eq('package')72expect(opts.android_activity).to eq('activity')73expect(opts.android_device_serial).to eq('123')74expect(opts.android_intent_arguments).to eq(%w[foo bar])75expect(opts.options[:'custom:options']).to eq(foo: 'bar')76end77end7879describe 'accessors' do80it 'adds a command-line argument' do81options.args << 'foo'82expect(options.args).to eq(['foo'])83end8485it 'sets the binary path' do86options.binary = '/foo/bar'87expect(options.binary).to eq('/foo/bar')88end8990it 'adds a preference' do91options.prefs[:foo] = 'bar'92expect(options.prefs[:foo]).to eq('bar')93end9495it 'adds an ENV' do96options.env['FOO'] = 'bar'97expect(options.env['FOO']).to eq('bar')98end99end100101describe '#log_level=' do102it 'sets the log level' do103options.log_level = :debug104expect(options.log_level).to eq(:debug)105end106end107108describe '#profile=' do109it 'sets a new profile' do110profile = Profile.new111allow(profile).to receive(:encoded).and_return('encoded_profile')112113options.profile = profile114expect(options.profile).to eq(profile)115end116117it 'sets an existing profile' do118profile = Profile.new119allow(profile).to receive(:encoded).and_return('encoded_profile')120121allow(Profile).to receive(:from_name).with('custom_profile_name').and_return(profile)122options.profile = 'custom_profile_name'123expect(options.profile).to eq(profile)124end125end126127describe '#add_argument' do128it 'adds a command-line argument' do129options.add_argument('foo')130expect(options.args.to_a).to eq(['foo'])131end132end133134describe '#add_option' do135it 'adds vendor namespaced options with ordered pairs' do136options.add_option('foo:bar', {bar: 'foo'})137expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})138end139140it 'adds vendor namespaced options with Hash' do141options.add_option('foo:bar' => {bar: 'foo'})142expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})143end144end145146describe '#add_preference' do147it 'adds a preference' do148options.add_preference(:foo, 'bar')149expect(options.prefs[:foo]).to eq('bar')150end151152it 'does not camelize preferences' do153options.add_preference('intl.accepted_languages', 'en-US')154155prefs = options.as_json['moz:firefoxOptions']['prefs']156expected = {'intl.accepted_languages' => 'en-US', 'remote.active-protocols' => 1}157expect(prefs).to eq(expected)158end159end160161describe '#enable_android' do162it 'adds default android settings' do163options.enable_android164165expect(options.android_package).to eq('org.mozilla.firefox')166expect(options.android_activity).to be_nil167expect(options.android_device_serial).to be_nil168expect(options.android_intent_arguments).to be_nil169end170171it 'accepts parameters' do172options.enable_android(package: 'foo',173serial_number: '123',174activity: 'qualified',175intent_arguments: %w[foo bar])176expect(options.android_package).to eq('foo')177expect(options.android_activity).to eq('qualified')178expect(options.android_device_serial).to eq('123')179expect(options.android_intent_arguments).to eq(%w[foo bar])180end181end182183describe '#as_json' do184it 'returns empty options by default' do185expect(options.as_json).to eq('browserName' => 'firefox',186'acceptInsecureCerts' => true,187'moz:firefoxOptions' => {'prefs' => {'remote.active-protocols' => 1}},188'moz:debuggerAddress' => true)189end190191it 'returns added options' do192options.add_option('foo:bar', {foo: 'bar'})193expect(options.as_json).to eq('acceptInsecureCerts' => true,194'browserName' => 'firefox',195'foo:bar' => {'foo' => 'bar'},196'moz:debuggerAddress' => true,197'moz:firefoxOptions' => {'prefs' => {'remote.active-protocols' => 1}})198end199200it 'converts to a json hash' do201profile = Profile.new202allow(profile).to receive(:as_json).and_return('encoded_profile')203204opts = described_class.new(browser_version: '66',205platform_name: 'win10',206accept_insecure_certs: false,207page_load_strategy: 'eager',208unhandled_prompt_behavior: 'accept',209strict_file_interactability: true,210timeouts: {script: 40000,211page_load: 400000,212implicit: 1},213set_window_rect: false,214args: %w[foo bar],215binary: '/foo/bar',216prefs: {foo: 'bar'},217env: {'FOO' => 'bar'},218profile: profile,219log_level: :debug,220android_package: 'package',221android_activity: 'activity',222android_device_serial: '123',223android_intent_arguments: %w[foo bar],224'custom:options': {foo: 'bar'})225226key = 'moz:firefoxOptions'227expect(opts.as_json).to eq('browserName' => 'firefox',228'browserVersion' => '66',229'platformName' => 'win10',230'acceptInsecureCerts' => false,231'pageLoadStrategy' => 'eager',232'unhandledPromptBehavior' => 'accept',233'strictFileInteractability' => true,234'timeouts' => {'script' => 40000,235'pageLoad' => 400000,236'implicit' => 1},237'setWindowRect' => false,238'custom:options' => {'foo' => 'bar'},239'moz:debuggerAddress' => true,240key => {'args' => %w[foo bar],241'binary' => '/foo/bar',242'prefs' => {'foo' => 'bar', 'remote.active-protocols' => 1},243'env' => {'FOO' => 'bar'},244'profile' => 'encoded_profile',245'log' => {'level' => 'debug'},246'androidPackage' => 'package',247'androidActivity' => 'activity',248'androidDeviceSerial' => '123',249'androidIntentArguments' => %w[foo bar]})250end251end252end # Options253end # Chrome254end # WebDriver255end # Selenium256257258