Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 395
Suresh Sharma Avatar asked Mar 21 '18 19:03

Suresh Sharma


People also ask

How do I find the XHR response in Selenium?

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.

How can I capture network traffic of a specific page using Selenium?

Selenium can execute JavaScript commands with the help of the execute_script method. JavaScript command to be executed is passed as a parameter to this method. To capture the network traffic, we have to pass the command: return window. performance.


1 Answers

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

like image 173
Manmohan_singh Avatar answered Oct 05 '22 23:10

Manmohan_singh