[🐛 Bug]: Network Interception doesn't work in JavaScript example.
See original GitHub issueWhat 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:
- Created 2 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top GitHub Comments
Hi @yaoyao0821 ,
Thanks for raising the issue.
I’ll look into this!
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:
For manipulating the response, we may need to use Fetch.fulfillRequest
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