Skip to content

[🐛 Bug]: Intercepting requests #12040

@mihael

Description

@mihael

What happened?

I am not sure this is actually a bug or a lack of a feature, so please bear this in mind.

Problem: Using page.driver.browser.intercept to intercept a request to an URL that does not exist, results in JSON parsing error when trying to continue the response:

.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools.rb:49:in `send_cmd': -32602: Invalid parameters: Failed to deserialize params.responseCode - BINDINGS: mandatory field missing at position 95 (Selenium::WebDriver::Error::WebDriverError)

I was following this tutorial:
https://gist.github.com/arielj/4f52b6fcdb8035babcd3f6dac8c2dc77

a) I can intercept and mock the response for an URL that exists and is responding.
b) It seems there is no way to intercept a fetch to a non-existent URL, or to an URL with CORS disallowing connections.
c) It seems an intercepted request always hits the remote server?

I was under the impression I could intercept and mock requests for any fetch request originating from JS code in a Rails app. But it seems this is only partially true, since the URLs intercepted are being hit anyway?!

How can we reproduce the issue?

Please see this minimal reproduction Rails project: https://github.com/mihael/intercepted

System test examples are here:
https://github.com/mihael/intercepted/blob/main/test/system/swapi_test.rb

Run with: bin/rails test test/system/swapi_test.rb

The setup:
https://github.com/mihael/intercepted/blob/main/lib/interceptor.rb
https://github.com/mihael/intercepted/blob/main/test/application_system_test_case.rb

Relevant log output

intercepted https://fakerapizzz.it/api/v1/companies?_quantity=5 with: my mocked response
#<Thread:0x00007fde4aea9630 /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/common/websocket_connection.rb:121 run> terminated with exception (report_on_exception is true):
/Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools.rb:49:in `send_cmd': -32602: Invalid parameters: Failed to deserialize params.responseCode - BINDINGS: mandatory field missing at position 95 (Selenium::WebDriver::Error::WebDriverError)
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-devtools-0.113.0/lib/selenium/devtools/v113/fetch.rb:56:in `fulfill_request'
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools/network_interceptor.rb:145:in `mutate_response'
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools/network_interceptor.rb:123:in `intercept_response'
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools/network_interceptor.rb:84:in `block in request_paused'
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools/network_interceptor.rb:162:in `with_cancellable_request'
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools/network_interceptor.rb:81:in `request_paused'
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/devtools/network_interceptor.rb:48:in `block in intercept'
	from /Users/mihael/.rvm/gems/ruby-3.0.1/gems/selenium-webdriver-4.9.1/lib/selenium/webdriver/common/websocket_connection.rb:131:in `block in callback_thread'

Operating System

macOS

Selenium version

Ruby 4.9.1

What are the browser(s) and version(s) where you see this issue?

Chrome 110

What are the browser driver(s) and version(s) where you see this issue?

Webdrivers chromedriver 113.0.5672.63

Are you using Selenium Grid?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-rbRuby BindingsI-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions