Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
`go_to` instead

### Fixed
- `page.on(:request, &:continue)` never continued the request, it called `continue(index, total)` and the error was
dropped. Symbol procs and lambdas taking one argument get only the object now, for `:request`, `:response`, `:auth`
and `:dialog`
- `AuthRequest#abort` raised `BrowserError` and left the page hanging, it cancels the auth challenge now
- `InterceptedRequest#navigation_request?` and `AuthRequest#navigation_request?` always returned `nil`, they are true
for document requests now
Expand Down
14 changes: 11 additions & 3 deletions lib/ferrum/interceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ def on(name, &block)
request = Network::InterceptedRequest.new(client, params)
exchange = network.find_or_build_exchange(request.network_id)
exchange.intercepted_request = request
block.call(request, index, total)
yield_event(block, request, index, total)
end
when :response
client.on("Network.loadingFinished") do |params, index, total|
exchange = network.select(params["requestId"]).last
block.call(exchange, index, total) if exchange
yield_event(block, exchange, index, total) if exchange
end
when :auth
client.on("Fetch.authRequired") do |params, index, total|
request = Network::AuthRequest.new(self, params)
block.call(request, index, total)
yield_event(block, request, index, total)
end
else
client.on(name, &block)
Expand Down Expand Up @@ -89,5 +89,13 @@ def off(name, id)
def subscribed?(event)
client.subscribed?(event)
end

private

def yield_event(block, object, index, total)
return block.call(object) if block.lambda? && (block.arity == 1 || block.parameters == [%i[req], %i[rest]])

block.call(object, index, total)
end
end
end
2 changes: 1 addition & 1 deletion lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def on(name, &block)

client.on("Page.javascriptDialogOpening") do |params, index, total|
dialog = Dialog.new(self, params)
block.call(dialog, index, total)
yield_event(block, dialog, index, total)
end
end

Expand Down
4 changes: 4 additions & 0 deletions sig/ferrum/interceptable.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ module Ferrum
def off: (Symbol | String name, Integer id) -> void

def subscribed?: (String event) -> bool

private

def yield_event: (Proc block, untyped object, Integer index, Integer total) -> untyped
end
end
24 changes: 24 additions & 0 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,30 @@ def is_mobile?
end
end

context "with :request" do
it "continues the request when given the continue method as a block" do
network.intercept
page.on(:request, &:continue)

page.go_to("/foo")

expect(page.body).to include("Another World")
end

it "yields the request with its index and total to a regular block" do
network.intercept
args = nil
page.on(:request) do |request, index, total|
args ||= [index, total]
request.continue
end

page.go_to("/foo")

expect(args).to eq([0, 1])
end
end

context "with :response" do
it "yields the exchange once its response is fully loaded" do
exchanges = []
Expand Down
Loading