"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/rb/spec/unit/selenium/webdriver/chrome/options_spec.rb" (17 Feb 2023, 17664 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "options_spec.rb": 4.7.0_vs_4.8.0.

    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 '#headless!' do
  183           it 'adds necessary command-line arguments' do
  184             expect {
  185               options.headless!
  186             }.to have_deprecated(:headless)
  187             expect(options.args).to eql(['--headless'])
  188           end
  189         end
  190 
  191         describe '#add_option' do
  192           it 'adds an option with ordered pairs' do
  193             expect {
  194               options.add_option(:foo, 'bar')
  195             }.to have_deprecated(:add_option)
  196             expect(options.instance_variable_get(:@options)[:foo]).to eq('bar')
  197           end
  198 
  199           it 'adds an option with Hash' do
  200             expect {
  201               options.add_option(foo: 'bar')
  202             }.to have_deprecated(:add_option)
  203             expect(options.instance_variable_get(:@options)[:foo]).to eq('bar')
  204           end
  205 
  206           it 'adds vendor namespaced options with ordered pairs' do
  207             options.add_option('foo:bar', {bar: 'foo'})
  208             expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
  209           end
  210 
  211           it 'adds vendor namespaced options with Hash' do
  212             options.add_option('foo:bar' => {bar: 'foo'})
  213             expect(options.instance_variable_get(:@options)['foo:bar']).to eq({bar: 'foo'})
  214           end
  215         end
  216 
  217         describe '#add_preference' do
  218           it 'adds a preference' do
  219             options.add_preference(:foo, 'bar')
  220             expect(options.prefs[:foo]).to eq('bar')
  221           end
  222         end
  223 
  224         describe '#add_emulation' do
  225           it 'add an emulated device by name' do
  226             options.add_emulation(device_name: 'iPhone 6')
  227             expect(options.emulation).to eq(device_name: 'iPhone 6')
  228           end
  229 
  230           it 'adds emulated device metrics' do
  231             options.add_emulation(device_metrics: {width: 400})
  232             expect(options.emulation).to eq(device_metrics: {width: 400})
  233           end
  234 
  235           it 'adds emulated user agent' do
  236             options.add_emulation(user_agent: 'foo')
  237             expect(options.emulation).to eq(user_agent: 'foo')
  238           end
  239         end
  240 
  241         describe '#enable_android' do
  242           it 'adds default android settings' do
  243             options.enable_android
  244 
  245             expect(options.android_package).to eq('com.android.chrome')
  246             expect(options.android_activity).to be_nil
  247             expect(options.android_device_serial).to be_nil
  248             expect(options.android_use_running_app).to be_nil
  249           end
  250 
  251           it 'accepts parameters' do
  252             options.enable_android(package: 'foo',
  253                                    serial_number: '123',
  254                                    activity: 'qualified',
  255                                    use_running_app: true)
  256             expect(options.android_package).to eq('foo')
  257             expect(options.android_activity).to eq('qualified')
  258             expect(options.android_device_serial).to eq('123')
  259             expect(options.android_use_running_app).to be(true)
  260           end
  261         end
  262 
  263         describe '#as_json' do
  264           it 'returns empty options by default' do
  265             expect(options.as_json).to eq('browserName' => 'chrome', 'goog:chromeOptions' => {})
  266           end
  267 
  268           it 'errors when unrecognized capability is passed' do
  269             expect {
  270               options.add_option(:foo, 'bar')
  271             }.to have_deprecated(:add_option)
  272 
  273             expect {
  274               options.as_json
  275             }.to output(/WARN Selenium These options are not w3c compliant/).to_stdout_from_any_process
  276           end
  277 
  278           it 'returns added options' do
  279             expect {
  280               options.add_option(:detach, true)
  281             }.to have_deprecated(:add_option)
  282             options.add_option('foo:bar', {foo: 'bar'})
  283             expect(options.as_json).to eq('browserName' => 'chrome',
  284                                           'foo:bar' => {'foo' => 'bar'},
  285                                           'goog:chromeOptions' => {'detach' => true})
  286           end
  287 
  288           it 'converts profile' do
  289             profile = Profile.new
  290             directory = profile.directory
  291 
  292             opts = described_class.new(profile: profile)
  293             expect(opts.as_json).to eq('browserName' => 'chrome',
  294                                        'goog:chromeOptions' =>
  295                                          {'args' => ["--user-data-dir=#{directory}"]})
  296           end
  297 
  298           it 'returns a JSON hash' do
  299             allow(File).to receive(:file?).and_return(true)
  300             allow_any_instance_of(described_class)
  301               .to receive(:encode_extension).with('foo.crx').and_return('encoded_foo')
  302             allow_any_instance_of(described_class)
  303               .to receive(:encode_extension).with('bar.crx').and_return('encoded_bar')
  304 
  305             opts = described_class.new(browser_version: '75',
  306                                        platform_name: 'win10',
  307                                        accept_insecure_certs: false,
  308                                        page_load_strategy: :eager,
  309                                        unhandled_prompt_behavior: :accept_and_notify,
  310                                        strict_file_interactability: true,
  311                                        timeouts: {script: 40000,
  312                                                   page_load: 400000,
  313                                                   implicit: 1},
  314                                        set_window_rect: false,
  315                                        args: %w[foo bar],
  316                                        prefs: {foo: 'bar',
  317                                                key_that_should_not_be_camelcased: 'baz',
  318                                                nested_one: {nested_two: 'bazbar'}},
  319                                        binary: '/foo/bar',
  320                                        extensions: ['foo.crx', 'bar.crx'],
  321                                        encoded_extensions: ['encoded_foobar'],
  322                                        emulation: {device_name: :mine},
  323                                        local_state: {
  324                                          foo: 'bar',
  325                                          key_that_should_not_be_camelcased: 'baz'
  326                                        },
  327                                        detach: true,
  328                                        debugger_address: '127.0.0.1:8181',
  329                                        exclude_switches: %w[foobar barfoo],
  330                                        minidump_path: 'linux/only',
  331                                        perf_logging_prefs: {enable_network: true},
  332                                        window_types: %w[normal devtools],
  333                                        android_package: 'package',
  334                                        android_activity: 'activity',
  335                                        android_device_serial: '123',
  336                                        android_use_running_app: true,
  337                                        'custom:options': {foo: 'bar'})
  338 
  339             key = 'goog:chromeOptions'
  340             expect(opts.as_json).to eq('browserName' => 'chrome',
  341                                        'browserVersion' => '75',
  342                                        'platformName' => 'win10',
  343                                        'acceptInsecureCerts' => false,
  344                                        'pageLoadStrategy' => 'eager',
  345                                        'unhandledPromptBehavior' => 'accept and notify',
  346                                        'strictFileInteractability' => true,
  347                                        'timeouts' => {'script' => 40000,
  348                                                       'pageLoad' => 400000,
  349                                                       'implicit' => 1},
  350                                        'setWindowRect' => false,
  351                                        'custom:options' => {'foo' => 'bar'},
  352                                        key => {'args' => %w[foo bar],
  353                                                'prefs' => {'foo' => 'bar',
  354                                                            'key_that_should_not_be_camelcased' => 'baz',
  355                                                            'nested_one' => {'nested_two' => 'bazbar'}},
  356                                                'binary' => '/foo/bar',
  357                                                'extensions' => %w[encoded_foobar encoded_foo encoded_bar],
  358                                                'mobileEmulation' => {'deviceName' => 'mine'},
  359                                                'localState' => {
  360                                                  'foo' => 'bar',
  361                                                  'key_that_should_not_be_camelcased' => 'baz'
  362                                                },
  363                                                'detach' => true,
  364                                                'debuggerAddress' => '127.0.0.1:8181',
  365                                                'excludeSwitches' => %w[foobar barfoo],
  366                                                'minidumpPath' => 'linux/only',
  367                                                'perfLoggingPrefs' => {'enableNetwork' => true},
  368                                                'windowTypes' => %w[normal devtools],
  369                                                'androidPackage' => 'package',
  370                                                'androidActivity' => 'activity',
  371                                                'androidDeviceSerial' => '123',
  372                                                'androidUseRunningApp' => true})
  373           end
  374         end
  375       end
  376     end # Chrome
  377   end # WebDriver
  378 end # Selenium