Skip to content

Commit 40c34ab

Browse files
authored
feat: Add Ferrum::Network#wait_for_idle! (#445)
BREAKING CHANGE: `wait_for_idle` now doesn't raise an error. Check your code and replace it with counterpart wait_for_idle!
1 parent 63d1e79 commit 40c34ab

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
### Added
44

5+
- `Ferrum::Network#wait_for_idle!` raises an error if timeout reached.
6+
57
### Changed
68

9+
- `Ferrum::Network#wait_for_idle` now returns true or false. Doesn't raise an error [BREAKING CHANGE].
10+
711
### Fixed
812

913
### Removed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ page.go_to("https://github.com/")
504504
page.network.status # => 200
505505
```
506506

507-
#### wait_for_idle(\*\*options)
507+
#### wait_for_idle(\*\*options) : `Boolean`
508508

509-
Waits for network idle or raises `Ferrum::TimeoutError` error
509+
Waits for network idle, returns `true` in case of success and `false` if there are still connections.
510510

511511
* options `Hash`
512512
* :connections `Integer` how many connections are allowed for network to be
@@ -519,7 +519,17 @@ Waits for network idle or raises `Ferrum::TimeoutError` error
519519
```ruby
520520
page.go_to("https://example.com/")
521521
page.at_xpath("//a[text() = 'No UI changes button']").click
522-
page.network.wait_for_idle
522+
page.network.wait_for_idle # => true
523+
```
524+
525+
#### wait_for_idle!(\*\*options)
526+
527+
Waits for network idle or raises `Ferrum::TimeoutError` error. Accepts same arguments as `wait_for_idle`.
528+
529+
```ruby
530+
page.go_to("https://example.com/")
531+
page.at_xpath("//a[text() = 'No UI changes button']").click
532+
page.network.wait_for_idle! # might raise an error
523533
```
524534

525535
#### clear(type)

lib/ferrum/network.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def initialize(page)
4141
end
4242

4343
#
44-
# Waits for network idle or raises {Ferrum::TimeoutError} error.
44+
# Waits for network idle.
4545
#
4646
# @param [Integer] connections
4747
# how many connections are allowed for network to be idling,
@@ -52,21 +52,33 @@ def initialize(page)
5252
# @param [Float] timeout
5353
# During what time we try to check idle.
5454
#
55-
# @raise [Ferrum::TimeoutError]
55+
# @return [Boolean]
5656
#
5757
# @example
5858
# browser.go_to("https://example.com/")
5959
# browser.at_xpath("//a[text() = 'No UI changes button']").click
60-
# browser.network.wait_for_idle
60+
# browser.network.wait_for_idle # => false
6161
#
6262
def wait_for_idle(connections: 0, duration: 0.05, timeout: @page.timeout)
6363
start = Utils::ElapsedTime.monotonic_time
6464

6565
until idle?(connections)
66-
raise TimeoutError if Utils::ElapsedTime.timeout?(start, timeout)
66+
return false if Utils::ElapsedTime.timeout?(start, timeout)
6767

6868
sleep(duration)
6969
end
70+
71+
true
72+
end
73+
74+
#
75+
# Waits for network idle or raises {Ferrum::TimeoutError} error.
76+
# Accepts same arguments as `wait_for_idle`.
77+
#
78+
# @raise [Ferrum::TimeoutError]
79+
def wait_for_idle!(...)
80+
result = wait_for_idle(...)
81+
raise TimeoutError unless result
7082
end
7183

7284
def idle?(connections = 0)

spec/network_spec.rb

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,46 @@
3636
end
3737
end
3838

39-
it "#wait_for_idle" do
40-
page.go_to("/show_cookies")
41-
expect(page.body).not_to include("test_cookie")
39+
describe "#wait_for_idle" do
40+
it "returns true" do
41+
page.go_to("/show_cookies")
42+
expect(page.body).not_to include("test_cookie")
4243

43-
page.at_xpath("//button[text() = 'Set cookie slow']").click
44-
network.wait_for_idle
45-
page.refresh
44+
page.at_xpath("//button[text() = 'Set cookie slow']").click
45+
result = network.wait_for_idle
46+
page.refresh
47+
48+
expect(result).to eq(true)
49+
expect(page.body).to include("test_cookie")
50+
end
51+
52+
it "returns false" do
53+
page.go_to("/show_cookies")
54+
expect(page.body).not_to include("test_cookie")
4655

47-
expect(page.body).to include("test_cookie")
56+
page.at_xpath("//button[text() = 'Set cookie slow']").click
57+
result = network.wait_for_idle(timeout: 0.2)
58+
59+
expect(result).to eq(false)
60+
end
61+
end
62+
63+
describe "#wait_for_idle!" do
64+
it "raises an error" do
65+
page.go_to("/show_cookies")
66+
expect(page.body).not_to include("test_cookie")
67+
68+
page.at_xpath("//button[text() = 'Set cookie slow']").click
69+
expect { network.wait_for_idle!(timeout: 0.2) }.to raise_error(Ferrum::TimeoutError)
70+
end
71+
72+
it "raises no error" do
73+
page.go_to("/show_cookies")
74+
expect(page.body).not_to include("test_cookie")
75+
76+
page.at_xpath("//button[text() = 'Set cookie slow']").click
77+
expect { network.wait_for_idle! }.not_to raise_error
78+
end
4879
end
4980

5081
describe "#idle?" do

0 commit comments

Comments
 (0)