Skip to content

Commit 6e0a845

Browse files
committed
Fix #136 error for log entry and failed request
1 parent e046958 commit 6e0a845

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

lib/ferrum/network.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,25 @@ def subscribe
159159

160160
@page.on("Network.loadingFailed") do |params|
161161
exchange = select(params["requestId"]).last
162+
exchange.error ||= Network::Error.new
162163

163-
if exchange && params['canceled']
164-
exchange.error = :canceled
165-
end
164+
exchange.error.id = params["requestId"]
165+
exchange.error.type = params["type"]
166+
exchange.error.error_text = params["errorText"]
167+
exchange.error.monotonic_time = params["timestamp"]
168+
exchange.error.canceled = params["canceled"]
166169
end
167170

168171
@page.on("Log.entryAdded") do |params|
169172
entry = params["entry"] || {}
170173
if entry["source"] == "network" && entry["level"] == "error"
171174
exchange = select(entry["networkRequestId"]).last
172-
error = Network::Error.new(entry)
173-
exchange.error = error
175+
exchange.error ||= Network::Error.new
176+
177+
exchange.error.id = entry["networkRequestId"]
178+
exchange.error.url = entry["url"]
179+
exchange.error.description = entry["text"]
180+
exchange.error.timestamp = entry["timestamp"]
174181
end
175182
end
176183
end

lib/ferrum/network/error.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
module Ferrum
44
class Network
55
class Error
6-
def initialize(data)
7-
@data = data
8-
end
9-
10-
def id
11-
@data["networkRequestId"]
12-
end
13-
14-
def url
15-
@data["url"]
16-
end
6+
attr_writer :canceled
7+
attr_reader :time, :timestamp
8+
attr_accessor :id, :url, :type, :error_text, :monotonic_time, :description
179

18-
def description
19-
@data["text"]
10+
def canceled?
11+
@canceled
2012
end
2113

22-
def time
23-
@time ||= Time.strptime(@data["timestamp"].to_s, "%s")
14+
def timestamp=(value)
15+
@timestamp = value
16+
@time = Time.strptime((value / 1000).to_s, "%s")
2417
end
2518
end
2619
end

spec/network_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ module Ferrum
6969

7070
it "captures canceled requests" do
7171
browser.go_to("/ferrum/with_ajax_connection_canceled")
72-
expect(browser.at_xpath("//h1[text() = 'Canceled']")).to be
72+
73+
# FIXME: Hack to wait for content in the browser
74+
Ferrum.with_attempts(errors: RuntimeError, max: 10, wait: 0.1) do
75+
browser.at_xpath("//h1[text() = 'Canceled']") || raise("Node not found")
76+
end
77+
7378
expect(browser.network.idle?).to be true
7479
end
7580

spec/support/views/with_ajax_connection_canceled.erb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
<body>
99
<h1>Here</h1>
1010
<script>
11-
$.ajax("http://localhost:12345/slow_response").abort();
12-
$("h1").text("Canceled");
11+
let req = $.ajax("/ferrum/really_slow");
12+
setTimeout(() => {
13+
req.abort();
14+
$("h1").text("Canceled");
15+
}, 500);
1316
</script>
1417
</body>
1518
</html>

0 commit comments

Comments
 (0)