selenium icon indicating copy to clipboard operation
selenium copied to clipboard

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

Open yaoyao0821 opened this issue 4 years ago • 5 comments

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

yaoyao0821 avatar Nov 01 '21 17:11 yaoyao0821

@yaoyao0821, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] avatar Nov 01 '21 17:11 github-actions[bot]

Hi there, is there any updates on this issue? Is is a bug or did I miss anything?

Thank you and best, Yao

yaoyao0821 avatar Dec 07 '21 16:12 yaoyao0821

Hi @yaoyao0821 ,

Thanks for raising the issue.

I'll look into this!

harsha509 avatar Dec 07 '21 17:12 harsha509

Any update on this fix?

Thank you.

knnmran avatar Mar 02 '22 04:03 knnmran

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

zzhengjian avatar Apr 01 '22 12:04 zzhengjian

@zzhengjian would you like to send us a PR to improve this?

diemol avatar Aug 12 '22 07:08 diemol

Hi @yaoyao0821 ,

This is fixed with commit https://github.com/SeleniumHQ/selenium/pull/10764/commits/85a785684d8ce37f99cb64c22597c44021d6fb34 and the fix is available in the latest [email protected]

Could you please check again and let us know about this?

Thanks, Sri Harsha

harsha509 avatar Aug 21 '22 07:08 harsha509

Closing as it is resolved in 4.3.0!

harsha509 avatar Aug 31 '22 08:08 harsha509

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Oct 01 '22 00:10 github-actions[bot]