Capture Network XHR logs(request/response with parameters) with Selenium
Asked Answered
G

2

6

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

Graven answered 21/3, 2018 at 19:42 Comment(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.Mafala
C
2

You 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 complete list)
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

    // create a new HAR with the label "yahoo.com"
    proxy.newHar("yahoo.com");

    // open yahoo.com
    driver.get("http://yahoo.com");

    // get the HAR data
    Har har = proxy.getHar();

The captured response can be viewed by any har viewer.

HARReport

Crispation answered 6/10, 2019 at 11:12 Comment(0)
S
0

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
  })
Sedda answered 3/10, 2019 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.