Tuesday 26 June 2012

Selenium WebDriver - Browsermob Proxy Integration

Hello Everyone,

In this post, I would like to share my learning related with 'Selenium Webdriver' - 'Browsermob Proxy' integration. Browsermob proxy is one of the proxy that has been closely integrated with Selenium.

When we use browsermob proxy with Selenium, we can intercept the http requests and responses that pass through the established selenium browser session. Now, I would like to explain how to integrate Selenium Webdriver with browsermob proxy using java.

For any queries related to browsermob-proxy, you can refer the below website: http://opensource.webmetrics.com/browsermob-proxy/

Softwares required:

1. Latest Selenium java files from Selenium website (Version that I used here is Selenium 2.23).
2. Download the Browermob proxy zip file from the above mentioned website (Version that I used is Browsermob-proxy v6).
3. Mozilla Firefox version supported by Selenium (I used Firefox 8.0.1).

Steps:

1. Unzip the Browsermob-proxy zip file.
2. Create new project in Eclipse ide, lets say Browsermob.
3. In eclipse, right click on the project and choose 'Properties'.
4. Navigate to 'Java Build Path' and select the 'Libraries' tab.
5. Click on the 'Add External Jar' option.
6. Add all the jar dependencies of Selenium jar.
7. Then add the jar dependencies of Browsermob-proxy under 'lib' folder of the unzipped Browsermob-proxy zip file.
8. Now, create a new class and copy and paste the below code:


public class Test_One {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ProxyServer server = new ProxyServer(8105);
         server.start();
         server.setCaptureHeaders(true);
       
         server.setCaptureContent(true);
         server.newHar("test");
         DesiredCapabilities capabilities = new DesiredCapabilities();
         Proxy proxy = server.seleniumProxy();
         FirefoxProfile profile = new FirefoxProfile();
         profile.setAcceptUntrustedCertificates(true);
         profile.setAssumeUntrustedCertificateIssuer(true);
         profile.setPreference("network.proxy.http", "localhost");
         profile.setPreference("network.proxy.http_port", 8105);
         profile.setPreference("network.proxy.ssl", "localhost");
         profile.setPreference("network.proxy.ssl_port", 8105);
         profile.setPreference("network.proxy.type", 1);
         profile.setPreference("network.proxy.no_proxies_on", "");
         profile.setProxyPreferences(proxy);
         capabilities.setCapability(FirefoxDriver.PROFILE,profile);
         capabilities.setCapability(CapabilityType.PROXY, proxy);
         WebDriver driver = new FirefoxDriver(capabilities);
         driver.get("http://www.google.com");
         Har har1 = server.getHar();
         }
}

}


Execute the above code. You can observe that proxy would have been created in the port 8105 and all the Http Requests and Responses would be passed via the proxy.

You can further refer about HAR files in order to obtain the required information about how to access http requests & responses from the HAR files.

Please let me know if you have any queries on this.