Thursday, October 3, 2019

Using browsermob proxy with Selenium WebDriver

Some time we need to extract har(http archive) file from browser to measure client site performance or extracting analytics data. Incorporating browsermob proxy with selenium we need to follow the below steps:

Step 1:
Create a maven project and add the following dependencies in pom.xml file.

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
       
        <!-- https://mvnrepository.com/artifact/net.lightbody.bmp/browsermob-core -->
        <dependency>
            <groupId>net.lightbody.bmp</groupId>
            <artifactId>browsermob-core</artifactId>
            <version>2.1.5</version>
        </dependency>


        <!-- Dependency For Driver Management -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.2</version>
        </dependency>


Step 2:

Instantiate and start the browsermob proxy server .

        BrowserMobProxyServer proxy = new BrowserMobProxyServer();
        proxy.setTrustAllServers(true);
        proxy.start();


Step 3:
  
Create the Selenium proxy to store all browser network communication.

        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);


Step 4:

Set the capturing capability of Selenium Proxy.

        Set<CaptureType> captureTypes = new HashSet<CaptureType>();
        captureTypes.add(CaptureType.REQUEST_BINARY_CONTENT);
        captureTypes.add(CaptureType.REQUEST_CONTENT);
        captureTypes.add(CaptureType.REQUEST_COOKIES);
        captureTypes.add(CaptureType.REQUEST_HEADERS);
        captureTypes.add(CaptureType.RESPONSE_BINARY_CONTENT);
        captureTypes.add(CaptureType.RESPONSE_CONTENT);
        captureTypes.add(CaptureType.RESPONSE_COOKIES);
        captureTypes.add(CaptureType.RESPONSE_HEADERS);


Step 5:

Set the Capture Type in Selenium Proxy
 
        proxy.enableHarCaptureTypes(captureTypes);


Step 6:

Set browser capability for Chrome
 
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY,seleniumProxy);
        ChromeOptions option = new ChromeOptions().merge(capabilities); 


Step 7:

 Instantiate Chrome browser with Capability and maximize the browser.
 
        ChromeDriverManager.chromedriver().setup();
        driver = new ChromeDriver(option);
        driver.manage().window().maximize();


Step 8:

Giving a har name, this name is not related with physical file name. This name will be available within your data it is basically used to group the data of different page.

        proxy.newHar("Google");
Step 9:
 
Open the browser with actual URL
 
        driver.get("https://www.google.com");
        driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);


Step 10:
 
Get the all network traffic.

        Har har = proxy.getHar();


Step 11:

Store it in a .har file.
 
        File harFile = new File("./Google.har");
        har.writeTo(harFile);    


Step 12: 

Close the  browser and stop the browsermob proxy server
 

        if (driver != null) {
            driver.quit();
        } else if (proxy.isStarted()) {
            proxy.stop();
        }


Complete Code:

package com.ibm.atnt.automation.test;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
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 io.github.bonigarcia.wdm.ChromeDriverManager;
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.proxy.CaptureType;

public class ValidateAnalyticsData {
    WebDriver driver;
    BrowserMobProxy proxy;

    @BeforeTest
    private void beforeTestAnalyticsData(){

        /*
         * Instantiate and start the browsermob proxy server.
         */
        proxy = new BrowserMobProxyServer();
        proxy.setTrustAllServers(true);
        proxy.start();

        /*
         * Create the Selenium proxy to store all browser network communication.
         */
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

        /*
         * Set the capturing capability of Selenium Proxy
         */
        Set<CaptureType> captureTypes = new HashSet<CaptureType>();
        captureTypes.add(CaptureType.REQUEST_BINARY_CONTENT);
        captureTypes.add(CaptureType.REQUEST_CONTENT);
        captureTypes.add(CaptureType.REQUEST_COOKIES);
        captureTypes.add(CaptureType.REQUEST_HEADERS);
        captureTypes.add(CaptureType.RESPONSE_BINARY_CONTENT);
        captureTypes.add(CaptureType.RESPONSE_CONTENT);
        captureTypes.add(CaptureType.RESPONSE_COOKIES);
        captureTypes.add(CaptureType.RESPONSE_HEADERS);

        /*
         * Set the Capture Type in Selenium Proxy
         */
        proxy.enableHarCaptureTypes(captureTypes);
       
        /*
         * Set browser capability for Chrome
         */
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY,seleniumProxy);
        ChromeOptions option = new ChromeOptions().merge(capabilities);
       
        /*
         * Instantiate Chrome browser with Capability and maximize.
         */
        ChromeDriverManager.chromedriver().setup();
        driver = new ChromeDriver(option);
        driver.manage().window().maximize();
    }

    @Test
    public void testAnalyticsData() throws IOException {

        /*
         * Giving a har name
         */
        proxy.newHar("Google");

        /*
         * Open the browser with actual URL
         */
        driver.get("https://www.google.com");
        driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);

        /*
         * Get the all network traffic.
         */
        Har har = proxy.getHar();

        /*
         * Store it in a .har file.
         */
        File harFile = new File("./Google.har");
        har.writeTo(harFile);
    }

    @AfterTest
    private void afterTestAnalyticsData() {
        if (driver != null) {
            driver.quit();
        } else if (proxy.isStarted()) {
            proxy.stop();
        }
    }
}

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ibm.atnt.automation</groupId>
    <artifactId>Demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
       

       <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
       
        <!-- https://mvnrepository.com/artifact/net.lightbody.bmp/browsermob-core -->
        <dependency>
            <groupId>net.lightbody.bmp</groupId>
            <artifactId>browsermob-core</artifactId>
            <version>2.1.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.2</version>
        </dependency>


    </dependencies>
</project> 

2 comments:

  1. Thanks, Experience with various technologies and businesses this is generally helpful.
    Still, I followed step-by-step your method in this selenium online training
    selenium certification
    selenium online training Hyderabad
    selenium online courses

    ReplyDelete
  2. Hey , if the site is secured by cloudflare and i have the secret keys for it . how can i implement that to perform automation using any driver.

    ReplyDelete