Skip to content

Commit

Permalink
Support getting timeouts from the driver
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Oct 5, 2021
1 parent 294d1c9 commit 22638e0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
35 changes: 31 additions & 4 deletions rb/lib/selenium/webdriver/common/timeouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions rb/lib/selenium/webdriver/remote/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/remote/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Bridge
# timeouts
#

get_timeouts: [:get, 'session/:session_id/timeouts'],
set_timeout: [:post, 'session/:session_id/timeouts'],

#
Expand Down
2 changes: 1 addition & 1 deletion rb/spec/integration/selenium/webdriver/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 21 additions & 19 deletions rb/spec/integration/selenium/webdriver/timeout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,66 @@
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

expect(driver.find_elements(class_name: 'redbox')).not_to be_empty
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

expect(driver.find_elements(class_name: 'redbox')).to be_empty
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
Expand Down

0 comments on commit 22638e0

Please sign in to comment.