ferrum icon indicating copy to clipboard operation
ferrum copied to clipboard

Add support for `browser.on(:response)`.

Open postmodern opened this issue 3 years ago • 10 comments

I wanted to be able to have a callback similar to on(:request) but triggered once a response was received. This adds an additional callback for the Network.responseReceived event when a response is received. It will look up the Ferrum::Network::Exchange object with the matching requestId and passes it back to the block, once a response has been received for a request.

I didn't know where to add specs for this, since there does not appear to be specs for Ferrum::Page or a spec/page_spec.rb file? I also didn't know where I should mention browser.on(:response) in the README, since browser.on(...) is only mentioned in conjunction to using browser.network.intercept.

postmodern avatar Oct 01 '22 22:10 postmodern

@route would appreciate some guidance on this new feature and where I should add the specs and documentation?

postmodern avatar Oct 01 '22 22:10 postmodern

@postmodern I was reorganizing specs so currently page specs are in the browser_spec. I think we can create page_spec and put it there. Though it overlaps with network_spec a bit, because page.on(:request) only works with interception, but page.on(:response) works without it. I'm now thinking that maybe it's better to use something like page.network.on(:request | :response | :auth). Need to experiment with that...

route avatar Oct 05 '22 08:10 route

I can't say for certain, but I believe the act of enabling network intercept requires you to authorize the requests to proceed. For example, some of the specs show:

https://github.com/rubycdp/ferrum/blob/main/spec/network/exchange_spec.rb#L28

network.intercept
page.on(:request) { |r, _, _| r.continue }

ttilberg avatar Feb 07 '23 20:02 ttilberg

@ttilberg hmm that solved the exception, except now it does not appear that my on(:response) callback is being called back.

postmodern avatar Feb 07 '23 20:02 postmodern

@postmodern I took a look but didn’t find a CDP event translation helper for on(:response). However, in an earlier issue I noted and used network.responseReceived, might this get you on your way?

https://github.com/rubycdp/ferrum/issues/197#issue-1009903139

Edit: haha… I just looked at the rest of the context of this issue… lol. I’ll see myself out!

ttilberg avatar Feb 07 '23 21:02 ttilberg

@ttilberg yeah it's added in this PR. Feel free to pull my branch and try to mess with it. I swear it was working last year.

postmodern avatar Feb 08 '23 02:02 postmodern

OK I added some echo debugging and the @client.on('Network.responseReceived') callback is being called, but it's not finding the exchange object for the given params['requestId'], so the passed in block is never called.

postmodern avatar Feb 08 '23 03:02 postmodern

Got it, I needed to do network.select from the context of Ferrum::Page.

postmodern avatar Feb 08 '23 03:02 postmodern