Skip to content

Commit 2338302

Browse files
committed
feat: Classify exchanges when loader changes
1 parent a610938 commit 2338302

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
- `Ferrum::Network::Request#headers` are enhanced and supplemented with `Network.requestWillBeSentExtraInfo` [#506]
77
- `Ferrum::Page#off` to unsubscribe from CDP events [#455]
88
- `Ferrum::Mouse#scroll_by` to be able to scroll by, as alternative to `scroll_to` [#514]
9+
- `Ferrum::Network::Exchange#unknown` determines if the exchange is in an unknown state, meaning that browser might not return info about it [#426]
10+
- `Ferrum::Network::Exchange#loader_id` returns loader id [#426]
911

1012
### Changed
1113

14+
- `Ferrum::Network::Exchange#finished?` takes into account that exchange might be in an unknown state after loader changed(next `go_to` call) and considers
15+
such as finished [#426]
16+
1217
### Fixed
1318

1419
- Correctly set mouse events buttons property [#509]

lib/ferrum/network.rb

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ def idle?(connections = 0)
8686
end
8787

8888
def total_connections
89-
exchange_connections.count
89+
@traffic.size
9090
end
9191

9292
def finished_connections
93-
exchange_connections.count(&:finished?)
93+
@traffic.count(&:finished?)
9494
end
9595

9696
def pending_connections
@@ -100,7 +100,7 @@ def pending_connections
100100
#
101101
# Page request of the main frame.
102102
#
103-
# @return [Request]
103+
# @return [Request, nil]
104104
#
105105
# @example
106106
# browser.go_to("https://github.com/")
@@ -391,7 +391,7 @@ def subscribe_request_will_be_sent
391391

392392
if exchange.navigation_request?(@page.main_frame.id)
393393
@exchange = exchange
394-
mark_pending_exchanges_as_unknown(exchange)
394+
classify_pending_exchanges(exchange.loader_id)
395395
end
396396
end
397397

@@ -403,17 +403,6 @@ def subscribe_request_will_be_sent
403403
end
404404
end
405405

406-
# When the main frame navigates Chrome doesn't send `Network.loadingFailed`
407-
# for pending async requests. Therefore, we mark pending connections as unknown since
408-
# they are not relevant to the current navigation.
409-
def mark_pending_exchanges_as_unknown(navigation_exchange)
410-
@traffic.each do |exchange|
411-
break if exchange.id == navigation_exchange.id
412-
413-
exchange.unknown = true if exchange.pending?
414-
end
415-
end
416-
417406
def subscribe_response_received
418407
@page.on("Network.responseReceived") do |params|
419408
exchange = select(params["requestId"]).last
@@ -514,15 +503,16 @@ def whitelist?
514503
Array(@whitelist).any?
515504
end
516505

517-
def exchange_connections
518-
@traffic.select { |e| exchange_connection?(e) }
519-
end
520-
521-
def exchange_connection?(exchange)
522-
return false if !@page.frames.any? { |f| f.id == exchange.request&.frame_id }
523-
return false if exchange.request&.frame_id != @exchange.request&.frame_id
506+
# When the main frame navigates Chrome doesn't send `Network.loadingFailed`
507+
# for pending async requests. Therefore, we mark pending connections as unknown since
508+
# they are not relevant to the current navigation.
509+
def classify_pending_exchanges(new_loader_id)
510+
@traffic.each do |exchange|
511+
break if exchange.loader_id == new_loader_id
512+
next unless exchange.pending?
524513

525-
exchange.request&.loader_id == @exchange.request&.loader_id
514+
exchange.unknown = true
515+
end
526516
end
527517
end
528518
end

lib/ferrum/network/exchange.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ def navigation_request?(frame_id)
6565
request&.type?(:document) && request&.frame_id == frame_id
6666
end
6767

68+
#
69+
# The loader ID of the request.
70+
#
71+
# @return [String, nil]
72+
#
73+
def loader_id
74+
request&.loader_id
75+
end
76+
6877
#
6978
# Determines if the network exchange has a request.
7079
#

lib/ferrum/page/frames.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def subscribe_frame_stopped_loading
124124
on("Page.frameStoppedLoading") do |params|
125125
# `DOM.performSearch` doesn't work without getting #document node first.
126126
# It returns node with nodeId 1 and nodeType 9 from which descend the
127-
# tree and we save it in a variable because if we call that again root
127+
# tree, and we save it in a variable because if we call that again root
128128
# node will change the id and all subsequent nodes have to change id too.
129129
if @main_frame.id == params["frameId"]
130130
@event.set if idling?

spec/network/exchange_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@
185185
)
186186
expect(last_exchange.pending?).to be true
187187
end
188+
189+
it "classifies request if loader changes" do
190+
page.go_to("/ferrum/with_slow_ajax_connection")
191+
xhr = page.network.traffic.find(&:pending?)
192+
193+
expect(page.body).to include("Slow AJAX")
194+
expect(xhr.url).to include("really_slow")
195+
expect(xhr.unknown).to be_falsey
196+
expect(xhr).to be_pending
197+
198+
page.go_to("/")
199+
200+
expect(page.body).to include("Hello world!")
201+
expect(xhr.unknown).to be_truthy
202+
expect(xhr).not_to be_pending
203+
end
188204
end
189205

190206
describe "#intercepted?" do

0 commit comments

Comments
 (0)