From 22638e01b1fcf6911ef81f8fdc78b9b3f47bd11f Mon Sep 17 00:00:00 2001 From: Alex Rodionov Date: Tue, 5 Oct 2021 11:35:54 -0700 Subject: [PATCH] Support getting timeouts from the driver https://www.w3.org/TR/webdriver/#get-timeouts --- rb/lib/selenium/webdriver/common/timeouts.rb | 35 ++++++++++++++-- rb/lib/selenium/webdriver/remote/bridge.rb | 15 ++++--- rb/lib/selenium/webdriver/remote/commands.rb | 1 + .../selenium/webdriver/driver_spec.rb | 2 +- .../selenium/webdriver/timeout_spec.rb | 40 ++++++++++--------- 5 files changed, 61 insertions(+), 32 deletions(-) diff --git a/rb/lib/selenium/webdriver/common/timeouts.rb b/rb/lib/selenium/webdriver/common/timeouts.rb index faba0f7b674e8..45a719e8cc27c 100644 --- a/rb/lib/selenium/webdriver/common/timeouts.rb +++ b/rb/lib/selenium/webdriver/common/timeouts.rb @@ -24,22 +24,49 @@ def initialize(bridge) @bridge = bridge end + # + # Gets the amount of time the driver should wait when searching for elements. + # + + def implicit_wait + Float(@bridge.timeouts['implicit']) / 1000 + end + # # Set the amount of time the driver should wait when searching for elements. # def implicit_wait=(seconds) - @bridge.implicit_wait_timeout = Integer(seconds * 1000) + @bridge.timeouts = {'implicit' => Integer(seconds * 1000)} end + # + # Gets the amount of time to wait for an asynchronous script to finish + # execution before throwing an error. + # + + def script + Float(@bridge.timeouts['script']) / 1000 + end + alias_method :script_timeout, :script + # # Sets the amount of time to wait for an asynchronous script to finish # execution before throwing an error. If the timeout is negative, then the # script will be allowed to run indefinitely. # - def script_timeout=(seconds) - @bridge.script_timeout = Integer(seconds * 1000) + def script=(seconds) + @bridge.timeouts = {'script' => Integer(seconds * 1000)} + end + alias_method :script_timeout=, :script= + + # + # Gets the amount of time to wait for a page load to complete before throwing an error. + # + + def page_load + Float(@bridge.timeouts['pageLoad']) / 1000 end # @@ -48,7 +75,7 @@ def script_timeout=(seconds) # def page_load=(seconds) - @bridge.timeout 'page load', Integer(seconds * 1000) + @bridge.timeouts = {'pageLoad' => Integer(seconds * 1000)} end end # Timeouts end # WebDriver diff --git a/rb/lib/selenium/webdriver/remote/bridge.rb b/rb/lib/selenium/webdriver/remote/bridge.rb index 6e88a391e1cac..0ed572bb81bc5 100644 --- a/rb/lib/selenium/webdriver/remote/bridge.rb +++ b/rb/lib/selenium/webdriver/remote/bridge.rb @@ -93,17 +93,16 @@ def get(url) execute :get, {}, {url: url} end - def implicit_wait_timeout=(milliseconds) - timeout('implicit', milliseconds) - end + # + # timeouts + # - def script_timeout=(milliseconds) - timeout('script', milliseconds) + def timeouts + execute :get_timeouts, {} end - def timeout(type, milliseconds) - type = 'pageLoad' if type == 'page load' - execute :set_timeout, {}, {type => milliseconds} + def timeouts=(timeouts) + execute :set_timeout, {}, timeouts end # diff --git a/rb/lib/selenium/webdriver/remote/commands.rb b/rb/lib/selenium/webdriver/remote/commands.rb index 3338170533d7a..2622effe97314 100644 --- a/rb/lib/selenium/webdriver/remote/commands.rb +++ b/rb/lib/selenium/webdriver/remote/commands.rb @@ -114,6 +114,7 @@ class Bridge # timeouts # + get_timeouts: [:get, 'session/:session_id/timeouts'], set_timeout: [:post, 'session/:session_id/timeouts'], # diff --git a/rb/spec/integration/selenium/webdriver/driver_spec.rb b/rb/spec/integration/selenium/webdriver/driver_spec.rb index cf9ab3839d0fd..473a3d0e2c84e 100644 --- a/rb/spec/integration/selenium/webdriver/driver_spec.rb +++ b/rb/spec/integration/selenium/webdriver/driver_spec.rb @@ -324,7 +324,7 @@ module WebDriver describe 'execute async script' do before do - driver.manage.timeouts.script_timeout = 1 + driver.manage.timeouts.script = 1 driver.navigate.to url_for('ajaxy_page.html') end diff --git a/rb/spec/integration/selenium/webdriver/timeout_spec.rb b/rb/spec/integration/selenium/webdriver/timeout_spec.rb index 28eaa3666e626..c0179809c5ba3 100644 --- a/rb/spec/integration/selenium/webdriver/timeout_spec.rb +++ b/rb/spec/integration/selenium/webdriver/timeout_spec.rb @@ -22,37 +22,44 @@ module Selenium module WebDriver describe Timeouts do - context 'implicit waits' do - before do - driver.manage.timeouts.implicit_wait = 0 - driver.navigate.to url_for('dynamic.html') - end + before do + driver.manage.timeouts.implicit_wait = 6 + driver.manage.timeouts.page_load = 2 + driver.manage.timeouts.script = 1.5 + driver.navigate.to url_for('dynamic.html') + end - after { driver.manage.timeouts.implicit_wait = 0 } + after do + driver.manage.timeouts.implicit_wait = 0 + driver.manage.timeouts.page_load = 300 + driver.manage.timeouts.script = 30 + end - it 'should implicitly wait for a single element' do - driver.manage.timeouts.implicit_wait = 6 + it 'gets all the timeouts' do + expect(driver.manage.timeouts.implicit_wait).to eq(6) + expect(driver.manage.timeouts.page_load).to eq(2) + expect(driver.manage.timeouts.script).to eq(1.5) + end + context 'implicit waits' do + it 'should implicitly wait for a single element' do driver.find_element(id: 'adder').click expect { driver.find_element(id: 'box0') }.not_to raise_error end it 'should still fail to find an element with implicit waits enabled' do - driver.manage.timeouts.implicit_wait = 0.5 + driver.manage.timeouts.implicit_wait = 0.1 expect { driver.find_element(id: 'box0') }.to raise_error(WebDriver::Error::NoSuchElementError) end it 'should return after first attempt to find one after disabling implicit waits' do - driver.manage.timeouts.implicit_wait = 3 driver.manage.timeouts.implicit_wait = 0 - expect { driver.find_element(id: 'box0') }.to raise_error(WebDriver::Error::NoSuchElementError) end it 'should implicitly wait until at least one element is found when searching for many' do add = driver.find_element(id: 'adder') - driver.manage.timeouts.implicit_wait = 6 add.click add.click @@ -60,14 +67,13 @@ module WebDriver end it 'should still fail to find elements when implicit waits are enabled' do - driver.manage.timeouts.implicit_wait = 0.5 + driver.manage.timeouts.implicit_wait = 0.1 expect(driver.find_elements(class_name: 'redbox')).to be_empty end it 'should return after first attempt to find many after disabling implicit waits' do add = driver.find_element(id: 'adder') - driver.manage.timeouts.implicit_wait = 3 driver.manage.timeouts.implicit_wait = 0 add.click @@ -75,11 +81,7 @@ module WebDriver end end - context 'page loads' do - before { driver.manage.timeouts.page_load = 2 } - - after { driver.manage.timeouts.page_load = 300 } - + context 'page load' do it 'should timeout if page takes too long to load' do expect { driver.navigate.to url_for('sleep?time=3') }.to raise_error(WebDriver::Error::TimeoutError) end