Skip to content

Commit 22638e0

Browse files
committed
1 parent 294d1c9 commit 22638e0

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

rb/lib/selenium/webdriver/common/timeouts.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,49 @@ def initialize(bridge)
2424
@bridge = bridge
2525
end
2626

27+
#
28+
# Gets the amount of time the driver should wait when searching for elements.
29+
#
30+
31+
def implicit_wait
32+
Float(@bridge.timeouts['implicit']) / 1000
33+
end
34+
2735
#
2836
# Set the amount of time the driver should wait when searching for elements.
2937
#
3038

3139
def implicit_wait=(seconds)
32-
@bridge.implicit_wait_timeout = Integer(seconds * 1000)
40+
@bridge.timeouts = {'implicit' => Integer(seconds * 1000)}
3341
end
3442

43+
#
44+
# Gets the amount of time to wait for an asynchronous script to finish
45+
# execution before throwing an error.
46+
#
47+
48+
def script
49+
Float(@bridge.timeouts['script']) / 1000
50+
end
51+
alias_method :script_timeout, :script
52+
3553
#
3654
# Sets the amount of time to wait for an asynchronous script to finish
3755
# execution before throwing an error. If the timeout is negative, then the
3856
# script will be allowed to run indefinitely.
3957
#
4058

41-
def script_timeout=(seconds)
42-
@bridge.script_timeout = Integer(seconds * 1000)
59+
def script=(seconds)
60+
@bridge.timeouts = {'script' => Integer(seconds * 1000)}
61+
end
62+
alias_method :script_timeout=, :script=
63+
64+
#
65+
# Gets the amount of time to wait for a page load to complete before throwing an error.
66+
#
67+
68+
def page_load
69+
Float(@bridge.timeouts['pageLoad']) / 1000
4370
end
4471

4572
#
@@ -48,7 +75,7 @@ def script_timeout=(seconds)
4875
#
4976

5077
def page_load=(seconds)
51-
@bridge.timeout 'page load', Integer(seconds * 1000)
78+
@bridge.timeouts = {'pageLoad' => Integer(seconds * 1000)}
5279
end
5380
end # Timeouts
5481
end # WebDriver

rb/lib/selenium/webdriver/remote/bridge.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,16 @@ def get(url)
9393
execute :get, {}, {url: url}
9494
end
9595

96-
def implicit_wait_timeout=(milliseconds)
97-
timeout('implicit', milliseconds)
98-
end
96+
#
97+
# timeouts
98+
#
9999

100-
def script_timeout=(milliseconds)
101-
timeout('script', milliseconds)
100+
def timeouts
101+
execute :get_timeouts, {}
102102
end
103103

104-
def timeout(type, milliseconds)
105-
type = 'pageLoad' if type == 'page load'
106-
execute :set_timeout, {}, {type => milliseconds}
104+
def timeouts=(timeouts)
105+
execute :set_timeout, {}, timeouts
107106
end
108107

109108
#

rb/lib/selenium/webdriver/remote/commands.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class Bridge
114114
# timeouts
115115
#
116116

117+
get_timeouts: [:get, 'session/:session_id/timeouts'],
117118
set_timeout: [:post, 'session/:session_id/timeouts'],
118119

119120
#

rb/spec/integration/selenium/webdriver/driver_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ module WebDriver
324324

325325
describe 'execute async script' do
326326
before do
327-
driver.manage.timeouts.script_timeout = 1
327+
driver.manage.timeouts.script = 1
328328
driver.navigate.to url_for('ajaxy_page.html')
329329
end
330330

rb/spec/integration/selenium/webdriver/timeout_spec.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,64 +22,66 @@
2222
module Selenium
2323
module WebDriver
2424
describe Timeouts do
25-
context 'implicit waits' do
26-
before do
27-
driver.manage.timeouts.implicit_wait = 0
28-
driver.navigate.to url_for('dynamic.html')
29-
end
25+
before do
26+
driver.manage.timeouts.implicit_wait = 6
27+
driver.manage.timeouts.page_load = 2
28+
driver.manage.timeouts.script = 1.5
29+
driver.navigate.to url_for('dynamic.html')
30+
end
3031

31-
after { driver.manage.timeouts.implicit_wait = 0 }
32+
after do
33+
driver.manage.timeouts.implicit_wait = 0
34+
driver.manage.timeouts.page_load = 300
35+
driver.manage.timeouts.script = 30
36+
end
3237

33-
it 'should implicitly wait for a single element' do
34-
driver.manage.timeouts.implicit_wait = 6
38+
it 'gets all the timeouts' do
39+
expect(driver.manage.timeouts.implicit_wait).to eq(6)
40+
expect(driver.manage.timeouts.page_load).to eq(2)
41+
expect(driver.manage.timeouts.script).to eq(1.5)
42+
end
3543

44+
context 'implicit waits' do
45+
it 'should implicitly wait for a single element' do
3646
driver.find_element(id: 'adder').click
3747
expect { driver.find_element(id: 'box0') }.not_to raise_error
3848
end
3949

4050
it 'should still fail to find an element with implicit waits enabled' do
41-
driver.manage.timeouts.implicit_wait = 0.5
51+
driver.manage.timeouts.implicit_wait = 0.1
4252
expect { driver.find_element(id: 'box0') }.to raise_error(WebDriver::Error::NoSuchElementError)
4353
end
4454

4555
it 'should return after first attempt to find one after disabling implicit waits' do
46-
driver.manage.timeouts.implicit_wait = 3
4756
driver.manage.timeouts.implicit_wait = 0
48-
4957
expect { driver.find_element(id: 'box0') }.to raise_error(WebDriver::Error::NoSuchElementError)
5058
end
5159

5260
it 'should implicitly wait until at least one element is found when searching for many' do
5361
add = driver.find_element(id: 'adder')
5462

55-
driver.manage.timeouts.implicit_wait = 6
5663
add.click
5764
add.click
5865

5966
expect(driver.find_elements(class_name: 'redbox')).not_to be_empty
6067
end
6168

6269
it 'should still fail to find elements when implicit waits are enabled' do
63-
driver.manage.timeouts.implicit_wait = 0.5
70+
driver.manage.timeouts.implicit_wait = 0.1
6471
expect(driver.find_elements(class_name: 'redbox')).to be_empty
6572
end
6673

6774
it 'should return after first attempt to find many after disabling implicit waits' do
6875
add = driver.find_element(id: 'adder')
6976

70-
driver.manage.timeouts.implicit_wait = 3
7177
driver.manage.timeouts.implicit_wait = 0
7278
add.click
7379

7480
expect(driver.find_elements(class_name: 'redbox')).to be_empty
7581
end
7682
end
7783

78-
context 'page loads' do
79-
before { driver.manage.timeouts.page_load = 2 }
80-
81-
after { driver.manage.timeouts.page_load = 300 }
82-
84+
context 'page load' do
8385
it 'should timeout if page takes too long to load' do
8486
expect { driver.navigate.to url_for('sleep?time=3') }.to raise_error(WebDriver::Error::TimeoutError)
8587
end

0 commit comments

Comments
 (0)