I tried to capture Network XHR logs (chrome browser) that generally shows Request(MethodType, Headers, parameters) and Response with Selenium webdriver but i was only able to get api's request that client sent to server(without parameter), while searching i found below code and it only provides me apis request:-
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage())
}
But i want to get also all the parameters that sent by client(browser) to server and also response. *how the same feature will work for firefox.
Thanks in advance!!
I tried to capture Network XHR logs (chrome browser) that generally shows Request(MethodType, Headers, parameters) and Response with Selenium webdriver but i was only able to get api's request that client sent to server(without parameter), while searching i found below code and it only provides me apis request:-
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage())
}
But i want to get also all the parameters that sent by client(browser) to server and also response. *how the same feature will work for firefox.
Thanks in advance!!
Share Improve this question asked Mar 21, 2018 at 19:42 Suresh SharmaSuresh Sharma 1861 gold badge6 silver badges25 bronze badges 1- I think Selenium only has access to the DOM, so you'd need to do something sort of odd using javascript which would dirty your test case a bit. The DOM should have everything that is being sent... HTML5 storage-cache included... but to get the headers, you'd need to execute javascript. One thing that seems possible here is to get into the framework the site is using and modify it to route/duplicate requests... so if they were using jQuery for instance, put hooks into the AJAX calls that return or write to the DOM the information you need. – browsermator Commented Sep 30, 2019 at 22:16
2 Answers
Reset to default 2You can use browsermobproxy.
Following code snippet captures all request and response logs.
// start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);
// enable more detailed HAR capture, if desired (see CaptureType for the plete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
// create a new HAR with the label "yahoo."
proxy.newHar("yahoo.");
// open yahoo.
driver.get("http://yahoo.");
// get the HAR data
Har har = proxy.getHar();
The captured response can be viewed by any har viewer.
If you are using a library like Axios to make XHR calls, you can take advantage of request interceptors and response interceptors as middlewares to intercept and eventually log every XHR call with its response without relying on headless browsers interfaces.
Request example
client.interceptors.request.use(
req => {
// req contains your request data
},
err => Promise.reject(err),
);
Response example
client.interceptors.response.use(
response => response, // XHR Response
error => {
const originalRequest = error.config; // Error.config contains too the original request
// ...code
})