question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[🐛 Bug]: Network Interception doesn't work in JavaScript example.

See original GitHub issue

What happened?

Hi there, I try the Network interception JavaScript example but it fails to intercept the response.

The first Collect Performance Metrics example works good, but the second onIntercept function example doesn’t work.

After sending the request (await driver.url()), the page still returns the original response and nothing is changed.

Note: The first line of the example maybe wrong. It causes an error because of the lack of argument in createCDPConnection function in the example code. I check the source code and add an argument ‘page’ to fix it. Now there is no errors but still interception doesn’t work and always returns a 404 error.

Is it a bug in JavaScript or do I miss anything? Thanks!

How can we reproduce the issue?

const webdriver = require('selenium-webdriver');
const { HttpResponse } = require('selenium-webdriver/devtools/networkinterceptor');

(async function selenium4Test () {
  let driver = new webdriver.Builder().forBrowser('chrome').build();
    const connection = await driver.createCDPConnection('page');
    let url = 'http://localhost:1000/test.html';
    let httpResponse = new HttpResponse(url);
    httpResponse.addHeaders("Content-Type", "UTF-8");
    httpResponse.body = "sausages";
    await driver.onIntercept(connection, httpResponse, async function () {
      let body = await driver.getPageSource();
      console.log(body, httpResponse);
    })
    await driver.get(url);
})();

Relevant log output

The output of httpResponse:
HttpResponse {
  returnBody: 'sausages',
  returnHeaders: [ { name: 'Content-Type', value: 'UTF-8' } ],
  returnMethod: 'GET',
  returnStatus: 200,
  urlToIntercept: 'http://localhost:1000/test.html'
}
The actual page body is the same as the original one. E.g. <html><head>...</head><body>
</body></html>

Operating System

macOS BigSur 11.2.3

Selenium version

JavaScript 4.0.0

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

Chrome 95.0.4638.69

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

ChromeDriver 95.0.4638.54

Are you using Selenium Grid?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
harsha509commented, Dec 7, 2021

Hi @yaoyao0821 ,

Thanks for raising the issue.

I’ll look into this!

2reactions
zzhengjiancommented, Apr 1, 2022

The current implementation couldn’t really work it intended to. If you check the sourcecode, the Fetch.continueRequest is actually manipulating the request instead of the response:

          connection.execute('Fetch.continueRequest', {
            requestId: requestPausedParams['requestId'],
            url: httpResponse.urlToIntercept,
            method: httpResponse.method,
            headers: httpResponse.headers,
            postData: httpResponse.body
          })

For manipulating the response, we may need to use Fetch.fulfillRequest

          connection.execute('Fetch.fulfillRequest', {
            requestId: requestPausedParams['requestId'],
            responseCode: 404
          })

And also in the callback, I suggest to pass callback(requestPausedParams) so that would help us to capture basic XHR info and do assertions in the tests

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve ERROR NetworkError at XMLHttpRequest ...
This is one version of a custom transfer state file but there are a few ways to do this if this one doesn't...
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
This is the browser trying to be robust and retrying a failed network request. The browser sees a network error coming back from...
Read more >
752010 - Devtools network interception does not work on mac ...
We've received reports that the Network.requestIntercepted event does not fire on mac if --user-data-dir is specified. Curiously if --headless ...
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
It's a very simple Vue.js app built with json-server - a single file json database. You can check out a branch in which...
Read more >
Node.js v19.2.0 Documentation
Native abstractions for Node.js; Node-API; Addon examples ... Running npm install -g yarn doesn't work ... Error propagation and interception.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found