"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/rb/spec/integration/selenium/webdriver/driver_spec.rb" (17 Feb 2023, 13872 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 "driver_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_relative 'spec_helper'
   21 
   22 module Selenium
   23   module WebDriver
   24     describe Driver do
   25       it_behaves_like 'driver that can be started concurrently', exclude: {browser: %i[safari safari_preview]}
   26 
   27       it 'creates default capabilities' do
   28         reset_driver! do |driver|
   29           caps = driver.capabilities
   30           expect(caps.proxy).to be_nil
   31           expect(caps.browser_version).to match(/^\d\d\d?\./)
   32           expect(caps.platform_name).not_to be_nil
   33 
   34           expect(caps.accept_insecure_certs).to be == (caps.browser_name == 'firefox')
   35           expect(caps.page_load_strategy).to be == 'normal'
   36           expect(caps.implicit_timeout).to be_zero
   37           expect(caps.page_load_timeout).to be == 300000
   38           expect(caps.script_timeout).to be == 30000
   39         end
   40       end
   41 
   42       it 'gets driver status' do
   43         status = driver.status
   44         expect(status).to include('ready', 'message')
   45       end
   46 
   47       it 'gets the page title' do
   48         driver.navigate.to url_for('xhtmlTest.html')
   49         expect(driver.title).to eq('XHTML Test Page')
   50       end
   51 
   52       it 'gets the page source' do
   53         driver.navigate.to url_for('xhtmlTest.html')
   54         expect(driver.page_source).to match(%r{<title>XHTML Test Page</title>}i)
   55       end
   56 
   57       it 'refreshes the page' do
   58         driver.navigate.to url_for('javascriptPage.html')
   59         sleep 1 # javascript takes too long to load
   60         driver.find_element(id: 'updatediv').click
   61         expect(driver.find_element(id: 'dynamo').text).to eq('Fish and chips!')
   62         driver.navigate.refresh
   63         wait_for_element(id: 'dynamo')
   64         expect(driver.find_element(id: 'dynamo').text).to eq("What's for dinner?")
   65       end
   66 
   67       describe 'one element' do
   68         it 'finds by id' do
   69           driver.navigate.to url_for('xhtmlTest.html')
   70           element = driver.find_element(id: 'id1')
   71           expect(element).to be_a(WebDriver::Element)
   72           expect(element.text).to eq('Foo')
   73         end
   74 
   75         it 'finds by field name' do
   76           driver.navigate.to url_for('formPage.html')
   77           expect(driver.find_element(name: 'x').attribute('value')).to eq('name')
   78         end
   79 
   80         it 'finds by class name' do # rubocop:disable RSpec/RepeatedExample
   81           driver.navigate.to url_for('xhtmlTest.html')
   82           expect(driver.find_element(class: 'header').text).to eq('XHTML Might Be The Future')
   83         end
   84 
   85         # TODO: Rewrite this test so it's not a duplicate of above or remove
   86         it 'finds elements with a hash selector' do # rubocop:disable RSpec/RepeatedExample
   87           driver.navigate.to url_for('xhtmlTest.html')
   88           expect(driver.find_element(class: 'header').text).to eq('XHTML Might Be The Future')
   89         end
   90 
   91         it 'finds by link text' do
   92           driver.navigate.to url_for('xhtmlTest.html')
   93           expect(driver.find_element(link: 'Foo').text).to eq('Foo')
   94         end
   95 
   96         it 'finds by xpath' do
   97           driver.navigate.to url_for('xhtmlTest.html')
   98           expect(driver.find_element(xpath: '//h1').text).to eq('XHTML Might Be The Future')
   99         end
  100 
  101         it 'finds by css selector' do
  102           driver.navigate.to url_for('xhtmlTest.html')
  103           expect(driver.find_element(css: 'div.content').attribute('class')).to eq('content')
  104         end
  105 
  106         it 'finds by tag name' do
  107           driver.navigate.to url_for('xhtmlTest.html')
  108           expect(driver.find_element(tag_name: 'div').attribute('class')).to eq('navigation')
  109         end
  110 
  111         it 'finds above another' do
  112           driver.navigate.to url_for('relative_locators.html')
  113 
  114           above = driver.find_element(relative: {tag_name: 'td', above: {id: 'center'}})
  115           expect(above.attribute('id')).to eq('second')
  116         end
  117 
  118         it 'finds child element' do
  119           driver.navigate.to url_for('nestedElements.html')
  120 
  121           element = driver.find_element(name: 'form2')
  122           child = element.find_element(name: 'selectomatic')
  123 
  124           expect(child.attribute('id')).to eq('2')
  125         end
  126 
  127         it 'finds child element by tag name' do
  128           driver.navigate.to url_for('nestedElements.html')
  129 
  130           element = driver.find_element(name: 'form2')
  131           child = element.find_element(tag_name: 'select')
  132 
  133           expect(child.attribute('id')).to eq('2')
  134         end
  135 
  136         it 'finds elements with the shortcut syntax' do
  137           driver.navigate.to url_for('xhtmlTest.html')
  138 
  139           expect(driver[:id1]).to be_a(WebDriver::Element)
  140           expect(driver[xpath: '//h1']).to be_a(WebDriver::Element)
  141         end
  142       end
  143 
  144       describe 'many elements' do
  145         it 'finds by class name' do
  146           driver.navigate.to url_for('xhtmlTest.html')
  147           expect(driver.find_elements(class: 'nameC').size).to eq(2)
  148         end
  149 
  150         it 'finds by css selector' do
  151           driver.navigate.to url_for('xhtmlTest.html')
  152           expect(driver.find_elements(css: 'p').size).to be_positive
  153         end
  154 
  155         it 'finds above element' do
  156           driver.navigate.to url_for('relative_locators.html')
  157 
  158           lowest = driver.find_element(id: 'below')
  159           above = driver.find_elements(relative: {tag_name: 'p', above: lowest})
  160           expect(above.map { |e| e.attribute('id') }).to eq(%w[mid above])
  161         end
  162 
  163         it 'finds above another' do
  164           driver.navigate.to url_for('relative_locators.html')
  165 
  166           above = driver.find_elements(relative: {css: 'td', above: {id: 'center'}})
  167           expect(above.map { |e| e.attribute('id') }).to eq(%w[second first third])
  168         end
  169 
  170         it 'finds below element' do
  171           driver.navigate.to url_for('relative_locators.html')
  172 
  173           midpoint = driver.find_element(id: 'mid')
  174           above = driver.find_elements(relative: {id: 'below', below: midpoint})
  175           expect(above.map { |e| e.attribute('id') }).to eq(['below'])
  176         end
  177 
  178         it 'finds near another within default distance' do
  179           driver.navigate.to url_for('relative_locators.html')
  180 
  181           near = driver.find_elements(relative: {tag_name: 'td', near: {id: 'sixth'}})
  182           expect(near.map { |e| e.attribute('id') }).to eq(%w[third ninth center second eighth])
  183         end
  184 
  185         it 'finds near another within custom distance', except: {browser: %i[safari safari_preview]} do
  186           driver.navigate.to url_for('relative_locators.html')
  187 
  188           near = driver.find_elements(relative: {tag_name: 'td', near: {id: 'sixth', distance: 100}})
  189           expect(near.map { |e| e.attribute('id') }).to eq(%w[third ninth center second eighth])
  190         end
  191 
  192         it 'finds to the left of another' do
  193           driver.navigate.to url_for('relative_locators.html')
  194 
  195           left = driver.find_elements(relative: {tag_name: 'td', left: {id: 'center'}})
  196           expect(left.map { |e| e.attribute('id') }).to eq(%w[fourth first seventh])
  197         end
  198 
  199         it 'finds to the right of another' do
  200           driver.navigate.to url_for('relative_locators.html')
  201 
  202           right = driver.find_elements(relative: {tag_name: 'td', right: {id: 'center'}})
  203           expect(right.map { |e| e.attribute('id') }).to eq(%w[sixth third ninth])
  204         end
  205 
  206         it 'finds by combined relative locators' do
  207           driver.navigate.to url_for('relative_locators.html')
  208 
  209           found = driver.find_elements(relative: {tag_name: 'td', right: {id: 'second'}, above: {id: 'center'}})
  210           expect(found.map { |e| e.attribute('id') }).to eq(['third'])
  211         end
  212 
  213         it 'finds all by empty relative locator' do
  214           driver.navigate.to url_for('relative_locators.html')
  215 
  216           expected = driver.find_elements(tag_name: 'p')
  217           actual = driver.find_elements(relative: {tag_name: 'p'})
  218           expect(actual).to eq(expected)
  219         end
  220 
  221         it 'finds children by field name' do
  222           driver.navigate.to url_for('nestedElements.html')
  223           element = driver.find_element(name: 'form2')
  224           children = element.find_elements(name: 'selectomatic')
  225           expect(children.size).to eq(2)
  226         end
  227       end
  228 
  229       describe 'execute script' do
  230         it 'returns strings' do
  231           driver.navigate.to url_for('xhtmlTest.html')
  232           expect(driver.execute_script('return document.title;')).to eq('XHTML Test Page')
  233         end
  234 
  235         it 'returns numbers' do
  236           driver.navigate.to url_for('xhtmlTest.html')
  237           expect(driver.execute_script('return document.title.length;')).to eq(15)
  238         end
  239 
  240         it 'returns elements' do
  241           driver.navigate.to url_for('xhtmlTest.html')
  242           element = driver.execute_script("return document.getElementById('id1');")
  243           expect(element).to be_a(WebDriver::Element)
  244           expect(element.text).to eq('Foo')
  245         end
  246 
  247         it 'unwraps elements in deep objects' do
  248           driver.navigate.to url_for('xhtmlTest.html')
  249           result = driver.execute_script(<<~SCRIPT)
  250             var e1 = document.getElementById('id1');
  251             var body = document.body;
  252 
  253             return {
  254               elements: {'body' : body, other: [e1] }
  255             };
  256           SCRIPT
  257 
  258           expect(result).to be_a(Hash)
  259           expect(result['elements']['body']).to be_a(WebDriver::Element)
  260           expect(result['elements']['other'].first).to be_a(WebDriver::Element)
  261         end
  262 
  263         it 'returns booleans' do
  264           driver.navigate.to url_for('xhtmlTest.html')
  265           expect(driver.execute_script('return true;')).to be(true)
  266         end
  267 
  268         it 'raises if the script is bad' do
  269           driver.navigate.to url_for('xhtmlTest.html')
  270           expect {
  271             driver.execute_script('return squiggle();')
  272           }.to raise_error(Selenium::WebDriver::Error::JavascriptError)
  273         end
  274 
  275         it 'returns arrays' do
  276           driver.navigate.to url_for('xhtmlTest.html')
  277           expect(driver.execute_script('return ["zero", "one", "two"];')).to eq(%w[zero one two])
  278         end
  279 
  280         it 'is able to call functions on the page' do
  281           driver.navigate.to url_for('javascriptPage.html')
  282           driver.execute_script("displayMessage('I like cheese');")
  283           expect(driver.find_element(id: 'result').text.strip).to eq('I like cheese')
  284         end
  285 
  286         it 'is able to pass string arguments' do
  287           driver.navigate.to url_for('javascriptPage.html')
  288           expect(driver.execute_script("return arguments[0] == 'fish' ? 'fish' : 'not fish';", 'fish')).to eq('fish')
  289         end
  290 
  291         it 'is able to pass boolean arguments' do
  292           driver.navigate.to url_for('javascriptPage.html')
  293           expect(driver.execute_script('return arguments[0] == true;', true)).to be(true)
  294         end
  295 
  296         it 'is able to pass numeric arguments' do
  297           driver.navigate.to url_for('javascriptPage.html')
  298           expect(driver.execute_script('return arguments[0] == 1 ? 1 : 0;', 1)).to eq(1)
  299         end
  300 
  301         it 'is able to pass null arguments' do
  302           driver.navigate.to url_for('javascriptPage.html')
  303           expect(driver.execute_script('return arguments[0];', nil)).to be_nil
  304         end
  305 
  306         it 'is able to pass array arguments' do
  307           driver.navigate.to url_for('javascriptPage.html')
  308           expect(driver.execute_script('return arguments[0];', [1, '2', 3])).to eq([1, '2', 3])
  309         end
  310 
  311         it 'is able to pass element arguments' do
  312           driver.navigate.to url_for('javascriptPage.html')
  313           button = driver.find_element(id: 'plainButton')
  314           js = "arguments[0]['flibble'] = arguments[0].getAttribute('id'); return arguments[0]['flibble'];"
  315           expect(driver.execute_script(js, button))
  316             .to eq('plainButton')
  317         end
  318 
  319         it 'is able to pass in multiple arguments' do
  320           driver.navigate.to url_for('javascriptPage.html')
  321           expect(driver.execute_script('return arguments[0] + arguments[1];', 'one', 'two')).to eq('onetwo')
  322         end
  323       end
  324 
  325       describe 'execute async script' do
  326         before do
  327           driver.manage.timeouts.script = 1
  328           driver.navigate.to url_for('ajaxy_page.html')
  329         end
  330 
  331         it 'is able to return arrays of primitives from async scripts' do
  332           result = driver.execute_async_script "arguments[arguments.length - 1]([null, 123, 'abc', true, false]);"
  333           expect(result).to eq([nil, 123, 'abc', true, false])
  334         end
  335 
  336         it 'is able to pass multiple arguments to async scripts' do
  337           result = driver.execute_async_script 'arguments[arguments.length - 1](arguments[0] + arguments[1]);', 1, 2
  338           expect(result).to eq(3)
  339         end
  340 
  341         # Safari raises TimeoutError instead
  342         it 'times out if the callback is not invoked', except: {browser: %i[safari safari_preview]} do
  343           expect {
  344             # Script is expected to be async and explicitly callback, so this should timeout.
  345             driver.execute_async_script 'return 1 + 2;'
  346           }.to raise_error(Selenium::WebDriver::Error::ScriptTimeoutError)
  347         end
  348       end
  349     end
  350   end # WebDriver
  351 end # Selenium