Browsermob Sample Program

//Import dependency classes from Selenium standlone jar file
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

//Import dependency classes from BrowserMob-Proxy jar file
import org.browsermob.proxy.ProxyServer;
import org.browsermob.core.har.Har;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;

import java.io.FileOutputStream;

public class SampleClass {

    public static void main(String[] args) throws Exception {

        String strFilePath = "<Define Your Local System Path here>";

        // start the proxy
        ProxyServer server = new ProxyServer(4444);
        server.start();
        //captures the moouse movements and navigations
        server.setCaptureHeaders(true);
        server.setCaptureContent(true);

        // get the Selenium proxy object
        Proxy proxy = server.seleniumProxy();

        // configure it as a desired capability
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, proxy);

        // start the browser up
        WebDriver driver = new FirefoxDriver(capabilities);

        // create a new HAR with the label "SampleSite"
        server.newHar("SampleSite");

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

        // get the HAR data
        Har har = server.getHar();
        FileOutputStream fos = new FileOutputStream(strFilePath);
        har.writeTo(fos);
        server.stop();
        driver.quit();

    }
}

Popular posts from this blog

Selenium Webdriver Interview Questions with Answer

Selenium IDE Interview Questions with Answer

DLL to Jar file conversion