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.





11 comments:

  1. WebDriver driver = new FirefoxDriver(capabilities);

    getting error on this line

    ReplyDelete
  2. This wont work in MAC. Why ?- I dont know.. but in MAC https sites are not getting opened.(google does not open if you run this in MAC).
    Just FI for MAC users.

    ReplyDelete
  3. how to check google analytics using webdriver
    can u give example please

    ReplyDelete
  4. how to modify the http request header or add a new request header and then send the request using browser mob proxy ?

    ReplyDelete
  5. I am impressed your blog,thanks a lot for sharing this Information.
    nice explained, thanks

    Mozilla Firefox Technical Support

    ReplyDelete
  6. Any idea, how to make browsermob proxy work in robotframework

    ReplyDelete
  7. http://thungracinox.com.vn

    http://ruoungoaihn.com

    http://dieuhoacuhn.com

    ReplyDelete
  8. Hi thanks for sharing it.
    however, ProxyServer doesn't respond, "The proxy server is refusing connections", is there any setting required to resolve this?

    I'm not behind any specific firewall.

    I have started browserMob2.0 server + using these jars:

    browserMob2.0 ( along with jackson_core_1.7.1,jackson_asl_1.7.1 )
    selenium 2.53

    ReplyDelete
  9. Hey how to use upstream proxy with this ? i tried with the method setChaninedProxy() of browserMob but it did not work ! any help !

    ReplyDelete
  10. how can we pass two proxies.. ?

    ReplyDelete