Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
yaoyao0821 opened this issue Nov 1, 2021 · 9 comments
Closed

Comments

@yaoyao0821
Copy link

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

@github-actions
Copy link

github-actions bot commented Nov 1, 2021

@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!

@yaoyao0821
Copy link
Author

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

Thank you and best,
Yao

@harsha509
Copy link
Member

Hi @yaoyao0821 ,

Thanks for raising the issue.

I'll look into this!

@knnmran
Copy link

knnmran commented Mar 2, 2022

Any update on this fix?

Thank you.

@zzhengjian
Copy link

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

@diemol
Copy link
Member

diemol commented Aug 12, 2022

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

@harsha509
Copy link
Member

Hi @yaoyao0821 ,

This is fixed with commit 85a7856 and the fix is available in the latest selenium-webdriver@4.3.0

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

Thanks,
Sri Harsha

@harsha509
Copy link
Member

Closing as it is resolved in 4.3.0!

@github-actions
Copy link

github-actions bot commented Oct 1, 2022

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 github-actions bot locked and limited conversation to collaborators Oct 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants