Download BrowserMob Proxy
Add below Dependency
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.2</version>
<scope>test</scope>
</dependency>
After Running Script we will get har (HTTP Archive) file which will contain all http requests
We need to parse them, for that we will use HAR Parser
https://github.com/sdstoehr/har-reader
Example
<dependency>
<groupId>de.sstoehr</groupId>
<artifactId>har-reader</artifactId>
<version>2.0.1</version>
</dependency>
To View HAR File you can use online tools
https://ericduran.github.io/chromeHAR/
OR
chrome://apps/
HAR Viewer Extension in Chrome
Program
package mypackage;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.filters.RequestFilter;
import net.lightbody.bmp.proxy.CaptureType;
import net.lightbody.bmp.util.HttpMessageContents;
import net.lightbody.bmp.util.HttpMessageInfo;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import de.sstoehr.harreader.HarReader;
import de.sstoehr.harreader.HarReaderException;
import de.sstoehr.harreader.model.HarEntry;
import de.sstoehr.harreader.model.HarPostDataParam;
import de.sstoehr.harreader.model.HarQueryParam;
public class captureNetworkTraffic {
String driverPath = "C:/Users/NEERAJ/Downloads/";
String sFileName = "E:/NeerajNetworkTraffic.har";
public WebDriver driver;
public BrowserMobProxy proxy;
@BeforeTest
public void setUp() {
// start the proxy
proxy = new BrowserMobProxyServer();
proxy.start(0);
//get the Selenium proxy object - org.openqa.selenium.Proxy;
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
//set chromedriver system property
System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
driver = new ChromeDriver(capabilities);
System.out.println("Hearders-->"+CaptureType.REQUEST_HEADERS);
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
proxy.addRequestFilter(new RequestFilter() {
@Override
public HttpResponse filterRequest(HttpRequest request,
HttpMessageContents contents, HttpMessageInfo messageInfo) {
if(request.headers().contains("X-Requested-With")){
System.out.println("Here XHR"+request.uri());
}
// TODO Auto-generated method stub
return null;
}
});
// create a new HAR with the label "facebook.com"
proxy.newHar("facebook.com");
// open facebook.com
driver.get("https://www.quikr.com/");
}
@Test
public void testCaseOne() {
System.out.println("Navigate to selenium tutorials page");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@AfterTest
public void tearDown() throws HarReaderException {
// get the HAR data
Har har = proxy.getHar();
// Write HAR Data in a File
File harFile = new File(sFileName);
try {
har.writeTo(harFile);
} catch (IOException ex) {
System.out.println (ex.toString());
System.out.println("Could not find file " + sFileName);
}
if (driver != null) {
proxy.stop();
// driver.quit();
}
HarReader harReader = new HarReader();
de.sstoehr.harreader.model.Har har1 = harReader.readFromFile(new File("E:/NeerajNetworkTraffic.har"));
List<HarEntry> entries = har1.getLog().getEntries();
for (HarEntry entry: entries){
String method = entry.getRequest().getMethod().name();
if ( method.equals("GET"))
{ //For get request, we need to know QueryString,
List<HarQueryParam> params = entry.getRequest().getQueryString();
if(Integer.valueOf((entry.getResponse().getStatus())/100) ==4 ||Integer.valueOf((entry.getResponse().getStatus())/100) ==5 ){
System.out.println("This is not working correctly "+entry.getRequest().getUrl());
}
/*for (HarQueryParam param: params){
System.out.println("see query string:" + param.getName() + "=" + param.getValue() );
}
*/}else if ( method.equals("POST")){
List<HarPostDataParam> params = entry.getRequest().getPostData().getParams();
//System.out.println("Here POST-->"+entry.getResponse().getStatus());
if(Integer.valueOf((entry.getResponse().getStatus())/100) ==4 ||Integer.valueOf((entry.getResponse().getStatus())/100) ==5 ){
System.out.println("This is not working correctly "+entry.getRequest().getUrl());
}
/*for (HarPostDataParam param: params){
System.out.println("see param:" + param.getName() + "=" + param.getValue() );
}//FOR
*/}
}
}
}
Add below Dependency
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.2</version>
<scope>test</scope>
</dependency>
After Running Script we will get har (HTTP Archive) file which will contain all http requests
We need to parse them, for that we will use HAR Parser
https://github.com/sdstoehr/har-reader
Example
<dependency>
<groupId>de.sstoehr</groupId>
<artifactId>har-reader</artifactId>
<version>2.0.1</version>
</dependency>
To View HAR File you can use online tools
https://ericduran.github.io/chromeHAR/
OR
chrome://apps/
HAR Viewer Extension in Chrome
Program
package mypackage;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.filters.RequestFilter;
import net.lightbody.bmp.proxy.CaptureType;
import net.lightbody.bmp.util.HttpMessageContents;
import net.lightbody.bmp.util.HttpMessageInfo;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import de.sstoehr.harreader.HarReader;
import de.sstoehr.harreader.HarReaderException;
import de.sstoehr.harreader.model.HarEntry;
import de.sstoehr.harreader.model.HarPostDataParam;
import de.sstoehr.harreader.model.HarQueryParam;
public class captureNetworkTraffic {
String driverPath = "C:/Users/NEERAJ/Downloads/";
String sFileName = "E:/NeerajNetworkTraffic.har";
public WebDriver driver;
public BrowserMobProxy proxy;
@BeforeTest
public void setUp() {
// start the proxy
proxy = new BrowserMobProxyServer();
proxy.start(0);
//get the Selenium proxy object - org.openqa.selenium.Proxy;
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
//set chromedriver system property
System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
driver = new ChromeDriver(capabilities);
System.out.println("Hearders-->"+CaptureType.REQUEST_HEADERS);
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
proxy.addRequestFilter(new RequestFilter() {
@Override
public HttpResponse filterRequest(HttpRequest request,
HttpMessageContents contents, HttpMessageInfo messageInfo) {
if(request.headers().contains("X-Requested-With")){
System.out.println("Here XHR"+request.uri());
}
// TODO Auto-generated method stub
return null;
}
});
// create a new HAR with the label "facebook.com"
proxy.newHar("facebook.com");
// open facebook.com
driver.get("https://www.quikr.com/");
}
@Test
public void testCaseOne() {
System.out.println("Navigate to selenium tutorials page");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@AfterTest
public void tearDown() throws HarReaderException {
// get the HAR data
Har har = proxy.getHar();
// Write HAR Data in a File
File harFile = new File(sFileName);
try {
har.writeTo(harFile);
} catch (IOException ex) {
System.out.println (ex.toString());
System.out.println("Could not find file " + sFileName);
}
if (driver != null) {
proxy.stop();
// driver.quit();
}
HarReader harReader = new HarReader();
de.sstoehr.harreader.model.Har har1 = harReader.readFromFile(new File("E:/NeerajNetworkTraffic.har"));
List<HarEntry> entries = har1.getLog().getEntries();
for (HarEntry entry: entries){
String method = entry.getRequest().getMethod().name();
if ( method.equals("GET"))
{ //For get request, we need to know QueryString,
List<HarQueryParam> params = entry.getRequest().getQueryString();
if(Integer.valueOf((entry.getResponse().getStatus())/100) ==4 ||Integer.valueOf((entry.getResponse().getStatus())/100) ==5 ){
System.out.println("This is not working correctly "+entry.getRequest().getUrl());
}
/*for (HarQueryParam param: params){
System.out.println("see query string:" + param.getName() + "=" + param.getValue() );
}
*/}else if ( method.equals("POST")){
List<HarPostDataParam> params = entry.getRequest().getPostData().getParams();
//System.out.println("Here POST-->"+entry.getResponse().getStatus());
if(Integer.valueOf((entry.getResponse().getStatus())/100) ==4 ||Integer.valueOf((entry.getResponse().getStatus())/100) ==5 ){
System.out.println("This is not working correctly "+entry.getRequest().getUrl());
}
/*for (HarPostDataParam param: params){
System.out.println("see param:" + param.getName() + "=" + param.getValue() );
}//FOR
*/}
}
}
}
Amazing-Possible to share screen shot of the actual value?
ReplyDeleteThanks Nimesh,
ReplyDeleteYou can try to execute the above program and steps,
Could't upload the screenshots during this post.
Will share screenshots soon, to make it more understandable :)