Network Traffic Capture
Selenium offers a feature, captureNetworkTraffic, which allows you to intercept the network traffic as seen by the browser running your test. The response includes headers, status codes, timings and ajax requests. It’s especially useful if you’re debugging requests in your browser.
To be able to capture network traffic one should start instance as –
selenium.start(“captureNetworkTraffic=true”);
and then launch the application usual way. Once you reach a point after which n/w traffic is to be captured then fire following method –
selenium.captureNetworkTraffic(“xml”);
We can pass – “xml”, “plain” or “json” as parameter to this method. Since its return type is String, we can assert presence of any specific request on the response received.
For capturing network traffic, you can use Browser Mob proxy in below mentioned way:
BrowserMob Proxy is widely used in automation testing company which further makes it easy to use a proxy in tests:
// 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 “Google”
proxy.newHar(“Google”);
// open Yahoo
driver.get(“http://google.com“);
// get the HAR data
Har har = proxy.getHar();