Skip to content

Commit e6330fb

Browse files
committed
Fix #122 avoid reached server, but there are pending connections
1 parent 8ceec3d commit e6330fb

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ Ferrum::Browser.new(options)
157157
* `:timeout` (Numeric) - The number of seconds we'll wait for a response when
158158
communicating with browser. Default is 5.
159159
* `:js_errors` (Boolean) - When true, JavaScript errors get re-raised in Ruby.
160+
* `:pending_connection_errors` (Boolean) - When main frame is still waiting for slow responses while timeout is
161+
reached `PendingConnectionsError` is raised. It's better to figure out why you have slow responses and fix or
162+
block them rather than turn this setting off. Default is true.
160163
* `:browser_name` (Symbol) - `:chrome` by default, only experimental support
161164
for `:firefox` for now.
162165
* `:browser_path` (String) - Path to Chrome binary, you can also set ENV

lib/ferrum.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ class NoSuchTargetError < Error; end
1010
class NotImplementedError < Error; end
1111

1212
class StatusError < Error
13+
def initialize(url, message = nil)
14+
super(message || "Request to #{url} failed to reach server, check DNS and server status")
15+
end
16+
end
17+
18+
class PendingConnectionsError < StatusError
1319
attr_reader :pendings
1420

1521
def initialize(url, pendings = [])
1622
@pendings = pendings
17-
message = if pendings.empty?
18-
"Request to #{url} failed to reach server, check DNS and/or server status"
19-
else
20-
"Request to #{url} reached server, but there are still pending connections: #{pendings.join(', ')}"
21-
end
2223

23-
super(message)
24+
message = "Request to #{url} reached server, but there are still pending connections: #{pendings.join(', ')}"
25+
26+
super(url, message)
2427
end
2528
end
2629

lib/ferrum/browser.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Browser
2929
on goto] => :page
3030
delegate %i[default_user_agent] => :process
3131

32-
attr_reader :client, :process, :contexts, :logger, :js_errors,
32+
attr_reader :client, :process, :contexts, :logger, :js_errors, :pending_connection_errors,
3333
:slowmo, :base_url, :options, :window_size, :ws_max_receive_size
3434
attr_writer :timeout
3535

@@ -44,6 +44,7 @@ def initialize(options = nil)
4444
@logger, @timeout, @ws_max_receive_size =
4545
@options.values_at(:logger, :timeout, :ws_max_receive_size)
4646
@js_errors = @options.fetch(:js_errors, false)
47+
@pending_connection_errors = @options.fetch(:pending_connection_errors, true)
4748
@slowmo = @options[:slowmo].to_f
4849

4950
if @options.key?(:base_url)

lib/ferrum/page.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ def go_to(url = nil)
7878
end
7979
response["frameId"]
8080
rescue TimeoutError
81-
pendings = network.traffic.select(&:pending?).map { |e| e.request.url }
82-
raise StatusError.new(options[:url], pendings) unless pendings.empty?
81+
if @browser.pending_connection_errors
82+
pendings = network.traffic.select(&:pending?).map { |e| e.request.url }
83+
raise PendingConnectionsError.new(options[:url], pendings) unless pendings.empty?
84+
end
8385
end
8486
alias goto go_to
8587

spec/browser_spec.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,10 @@ module Ferrum
283283
end
284284

285285
it "handles when DNS incorrect" do
286-
expect { browser.go_to("http://nope:#{port}/") }.to raise_error(Ferrum::StatusError)
286+
expect { browser.go_to("http://nope:#{port}/") }.to raise_error(
287+
Ferrum::StatusError,
288+
%r{Request to http://nope:\d+/ failed to reach server, check DNS and server status}
289+
)
287290
end
288291

289292
it "has a descriptive message when DNS incorrect" do
@@ -292,7 +295,7 @@ module Ferrum
292295
browser.go_to(url)
293296
}.to raise_error(
294297
Ferrum::StatusError,
295-
%(Request to #{url} failed to reach server, check DNS and/or server status)
298+
%r{Request to http://nope:#{port}/ failed to reach server, check DNS and server status}
296299
)
297300
end
298301

@@ -303,8 +306,8 @@ module Ferrum
303306
expect {
304307
browser.go_to("/ferrum/visit_timeout")
305308
}.to raise_error(
306-
Ferrum::StatusError,
307-
%r{there are still pending connections: http://.*/ferrum/really_slow}
309+
Ferrum::PendingConnectionsError,
310+
%r{Request to http://.*/ferrum/visit_timeout reached server, but there are still pending connections: http://.*/ferrum/really_slow}
308311
)
309312
ensure
310313
browser.timeout = old_timeout
@@ -319,8 +322,8 @@ module Ferrum
319322
expect {
320323
browser.go_to("/ferrum/really_slow")
321324
}.to raise_error(
322-
Ferrum::StatusError,
323-
%r{there are still pending connections: http://.*/ferrum/really_slow}
325+
Ferrum::PendingConnectionsError,
326+
%r{Request to http://.*/ferrum/really_slow reached server, but there are still pending connections: http://.*/ferrum/really_slow}
324327
)
325328
ensure
326329
browser.timeout = prev_timeout
@@ -336,6 +339,16 @@ module Ferrum
336339
browser.timeout = old_timeout
337340
end
338341
end
342+
343+
it "does not report open resources when pending_connection_errors is set to false" do
344+
begin
345+
browser = Browser.new(base_url: base_url, pending_connection_errors: false, timeout: 0.1)
346+
347+
expect { browser.go_to("/ferrum/really_slow") }.not_to raise_error
348+
ensure
349+
browser&.quit
350+
end
351+
end
339352
end
340353

341354
it "allows the driver to have a fixed port" do

0 commit comments

Comments
 (0)