TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: BrowserMob Proxy for Selenium Load Testing
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

BrowserMob Proxy for Selenium Load Testing

Last updated: Feb 25, 2024 12:35 pm
By Meenakshi Agarwal
Share
5 Min Read
Create a Selenium Load Testing Demo Using BrowserMob Proxy
Selenium Load Testing Demo Using BrowserMob Proxy.
SHARE

If you’ve read one of our earlier posts on exploring Selenium for load testing, then you would admit it as a load-testing solution. But being a UI testing component you can only use it to make requests to a web application. In fact, you’ll need something else that can monitor the web app and measure the performance data. Hence, in this blog post, we’ll use the BrowserMob proxy to create the Selenium load-testing demo. It’s a free tool and can run with Selenium for load testing the web apps.

Contents
1- How to Setup the BrowserMob Proxy with Selenium?2- Selenium Load Testing Demo – Full Coding Snippet.

Also, in this tutorial, we’ll create a Selenium load-testing demo project in Eclipse for learning purposes. And as always we suggest going through the best Selenium tutorial to get quick help. In fact, reading them will ensure that you are ready with a simple project before getting to the next section. So, you can quickly test the Selenium load testing code snippet given in the later part of the post.

A few words about the BrowserMob proxy, it helps in capturing the performance data for a web app using the Selenium Webdriver code sample. It records the performance readings in the HTML archive i.e. HAR format. You can even utilize it for tuning the browser and the incoming traffic. It can let you block/unblock content and allows editing the HTTP requests/responses packets.

How to Use BrowserMob Proxy and Selenium for Load Testing

Create a Selenium Load Testing Demo Using BrowserMob Proxy
BrowserMob – Selenium for Load Testing

1- How to Setup the BrowserMob Proxy with Selenium?

In this Selenium load testing demo, first of all, we’ll create a proxy server instance and run it on any port, say 8081. Then, we’ll add a proxy attribute to set the Webdriver capabilities. Please see the below code snippet.

// Start the BrowserMob Proxy.
ProxyServer svr = new ProxyServer(9090);
svr.start();

// Get the Selenium proxy object.
Proxy proxy = svr.seleniumProxy();

// Set desired capability for using proxy Server.
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);

// Start the Browser with desired features.
WebDriver driver = new FirefoxDriver(capabilities);

Consequently, we’ll load the Firefox browser and run a small Selenium Webdriver test.

After running the test, we’ll call the BrowserMob API to read the perf data and export it in HAR format.

// Collect the performance data from the BrowserMob proxy server.
// Get the HAR data.
Har har = server.getHar();

// Write the HAR Data in a File
File harFile = new File("C:\\<PATH>\\sample.har");
har.writeTo(harFile);

// Stop the BrowserMob Proxy Server
server.stop();

// Close the browser
driver.quit();

2- Selenium Load Testing Demo – Full Coding Snippet.

Below is the complete code for running a load use case. However, there is a package shortcut used in the code which you can replace with the following.

ShortcutReplace the shortcut with the following if needed.
import org.openqa.selenium.*;
import org.browsermob.*;
You may choose to import a specific class as per your use case:
For example, in the case of org.openqa.selenium, you may use:
- Keys;
- WebDriver;
- WebElement;
- By;
- firefox.FirefoxDriver;
- Proxy;
- remote.CapabilityType;
- remote.DesiredCapabilities;

Similarly, for org.browsermob, you may use these:
- proxy.ProxyServer;
- core.har.Har;
Import the specific classes based on your needs.

Let’s check out the code below.

package com.techbeamers.loadtesting;

import java.io.File;
import org.openqa.selenium.*;
import org.browsermob.*;

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

    // Start the BrowserMob Proxy.
    ProxyServer server = new ProxyServer(8081);
    server.start();

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

    // Set desired capability for using Proxy Server
    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 "StockMarketData"
    server.newHar("StockMarketData");

    // Open the Google homepage.
    driver.get("http://www.google.com");

    // Find the search edit box on the Google page.
    WebElement searchBox = driver.findElement(By.name("q"));

    // Type in Selenium.
    searchBox.sendKeys("Selenium");

    // Find the search button.
    WebElement button = driver.findElement(By.name("btnG"));

    // Click the button.
    button.click();

    Thread.sleep(5000);

    // Collect the performance data from the BrowserMob proxy server.
    // Get the HAR data.
    Har ar = server.getHar();

    // Write the HAR Data in a File
    File arFile = new File("C:\\<PATH>\\sample.har");
    ar.writeTo(arFile);

    // Stop the BrowserMob Proxy Server
    server.stop();

    // Close the browser
    driver.quit();
  }
}

Final Word – BrowserMob Proxy and Selenium for Load Testing

This tutorial on “BrowserMob and Selenium” was in continuation of our promise to make each topic of your interest as simple as you could perceive it. Hence, we tried to add all micro-level details about using BrowserMob proxy with Selenium.

We wish this post could have made you more informed than you were before reading it.

All the Best,

TechBeamers

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

TAGGED:Advanced Selenium Tutorial
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article How to Use Internet Explorer Driver to Run Webdriver Tests How to Use Internet Explorer Driver with Selenium
Next Article Python MongoDB Programming Tutorial for Beginners How to Connect MongoDB with Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x