Skip to content

Commit 81ab27a

Browse files
authored
fix: finished? should return true only when response is fully loaded (#309)
fixes #197
1 parent 9f5f884 commit 81ab27a

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
## [Unreleased](https://github.com/rubycdp/ferrum/compare/v0.13...main) ##
22

33
### Added
4+
- `Ferrum::Network::Exchange#xhr?` determines if the exchange is XHR
5+
- `Ferrum::Network::Request#xhr?` determines if the request is XHR
6+
- `Ferrum::Network::Response#loaded?` returns true if the response is fully loaded
47

58
### Changed
69

710
### Fixed
11+
- `Ferrum::Network::Exchange#finished?` returns `true` only fully loaded responses
812

913
### Removed
1014

lib/ferrum/network.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,12 @@ def subscribe_response_received
374374

375375
def subscribe_loading_finished
376376
@page.on("Network.loadingFinished") do |params|
377-
exchange = select(params["requestId"]).last
378-
exchange.response.body_size = params["encodedDataLength"] if exchange&.response
377+
response = select(params["requestId"]).last&.response
378+
379+
if response
380+
response.loaded = true
381+
response.body_size = params["encodedDataLength"]
382+
end
379383
end
380384
end
381385

lib/ferrum/network/exchange.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def blocked?
7979
# @return [Boolean]
8080
#
8181
def finished?
82-
blocked? || !response.nil? || !error.nil?
82+
blocked? || response&.loaded? || !error.nil?
8383
end
8484

8585
#
@@ -100,6 +100,15 @@ def intercepted?
100100
!intercepted_request.nil?
101101
end
102102

103+
#
104+
# Determines if the exchange is XHR.
105+
#
106+
# @return [Boolean]
107+
#
108+
def xhr?
109+
!!request&.xhr?
110+
end
111+
103112
#
104113
# Returns request's URL.
105114
#

lib/ferrum/network/request.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ def type?(value)
5050
type.downcase == value.to_s.downcase
5151
end
5252

53+
#
54+
# Determines if the request is XHR.
55+
#
56+
# @return [Boolean]
57+
#
58+
def xhr?
59+
type?("xhr")
60+
end
61+
5362
#
5463
# The frame ID of the request.
5564
#

lib/ferrum/network/response.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ class Response
1818
# @return [Hash{String => Object}]
1919
attr_reader :params
2020

21+
# The response is fully loaded by the browser.
2122
#
22-
# Initializes the respones object.
23+
# @return [Boolean]
24+
attr_writer :loaded
25+
26+
#
27+
# Initializes the responses object.
2328
#
2429
# @param [Page] page
2530
# The page associated with the network response.
@@ -121,9 +126,8 @@ def body_size=(size)
121126
#
122127
def body
123128
@body ||= begin
124-
body, encoded = @page
125-
.command("Network.getResponseBody", requestId: id)
126-
.values_at("body", "base64Encoded")
129+
body, encoded = @page.command("Network.getResponseBody", requestId: id)
130+
.values_at("body", "base64Encoded")
127131
encoded ? Base64.decode64(body) : body
128132
end
129133
end
@@ -135,6 +139,13 @@ def main?
135139
@page.network.response == self
136140
end
137141

142+
# The response is fully loaded by the browser or not.
143+
#
144+
# @return [Boolean]
145+
def loaded?
146+
@loaded
147+
end
148+
138149
#
139150
# Comapres the respones ID to another response's ID.
140151
#

0 commit comments

Comments
 (0)