Code Monkey home page Code Monkey logo

browsermob_proxy--java's Introduction

Java-TestNG-BrowserMobProxy

Configure LT_USERNAME and LT_ACCESS_KEY in src\main\java\SampleBM.java

Problem statement: To capture the network logs at different instances of the automation tests.

Use case: Suppose you want to run an web automation script where you are performing n number of actions. After each set of actions, you would like to capture the network logs of the actions that has been executed.

Solution: Using a proxy called BrowserMob Proxy, we can capture the network calls after each commands.

What is a Browser Mob Proxy?

BrowserMob Proxy is a tool used for capturing and manipulating network traffic in web applications. It allows you to monitor and intercept HTTP/HTTPS traffic between a web browser and a web server. BrowserMob Proxy can be used for a variety of tasks such as performance testing, security testing, and web scraping.

With BrowserMob Proxy, you can capture HTTP traffic and analyze it for various purposes. It also provides the ability to manipulate requests and responses in real-time. For example, you can modify headers, add or remove cookies, and even modify the HTML of the page being loaded.

BrowserMob Proxy can be used with various programming languages such as Java, Python, and Ruby. It also has a REST API that allows you to programmatically control the proxy and retrieve data.

Overall, BrowserMob Proxy is a useful tool for developers, testers, and security professionals who need to monitor and manipulate network traffic in web applications.

For more details, please visit : https://github.com/lightbody/browsermob-proxy and https://bmp.lightbody.net/

How to capture the network logs using BrowserMob Proxy?

Step 1: Download the browsermob proxt from below URL: https://bmp.lightbody.net/ Step 2: Paste it in the same folder where your code is present. Step 3: Add the browsermob-core dependency to your pom:

<dependency>
    <groupId>net.lightbody.bmp</groupId>
    <artifactId>browsermob-core</artifactId>
    <version>2.1.5</version>
    <scope>test</scope>
</dependency>

Step 4: Start a proxy using net.lightbody.bmp.BrowserMobProxy:

 BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
// get the JVM-assigned port and get to work!
int port = proxy.getPort();
//...

Step 5: Download Tunnel from below link and save it in the same folder where you saved the Browsermob proxy. https://www.lambdatest.com/support/docs/local-testing-macos/

Step 6: Start the tunnel. Use below code for reference.

    t = new Tunnel();
    HashMap<String, String> options = new HashMap<String, String>();
    options.put("user", "your_LT_username");
    options.put("key", "your_LT_accesskey");
    options.put("proxyHost",hostIp);
    options.put("proxyPort", portn);
    options.put("ingress-only", "--ingress-only");          //mandatory while using BM proxy
    options.put("tunnelName",portn);
    //start tunnel
    t.start(options);

Step 7: Set the desired capabilities:

   ChromeOptions option = new ChromeOptions();

    option.addArguments("--ignore-certificate-errors");
    option.setProxy(seleniumProxy);
    option.setAcceptInsecureCerts(true);
    option.addArguments("--disable-backgrounding-occluded-windows");

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, option);
    capabilities.setCapability("browserName", browser);
    capabilities.setCapability("version", version);
    capabilities.setCapability("platform", platform);
    capabilities.setCapability("tunnel",true);
    capabilities.setCapability("tunnelName",portn);

Step 8: Set the remote connection:

    driver = new RemoteWebDriver(new URL("https://username:[email protected]/wd/hub"),capabilities);

Step 9: Get the HAR data.

proxy.newHar("HomePage");

    driver.get("https://www.google.com/");

    driver.findElement(By.name("q")).sendKeys("LambdaTest");
    driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
    Thread.sleep(5000);

    driver.get("https://lambdatest.com");

    // get the HAR data
    Thread.sleep(5000);
    Har har = proxy.getHar();

Step 10: Capture the HAR logs in the path same as where Browsermob proxy and tunnel are located.

    try {
        har.writeTo(new File("C://Users//deepanshu//Downloads//Java-TestNG-BrowserMobProxy-master 2//Java-TestNG-BrowserMobProxy-master"+proxy.getPort()+"homepage.har"));

    } catch (IOException e1) {
        e1.printStackTrace();
    }

That All!

You will get the logs in the desired location you mentioned. Please refer to the code snippet for better understanding.

browsermob_proxy--java's People

Contributors

deepanshusr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.