Skip to content
  • There are no suggestions because the search field is empty.

Improve Software Performance by Simulating Slow Internet with Selenium

Slow internet speeds can be frustrating for software users, causing applications to fail or load slowly. As software developers and QA professionals, it is important to ensure that applications deliver a seamless user experience under all network conditions. In this blog post, we will explore the importance of simulating slow internet connections with Selenium 4.

 

Untitled design (6) (1)

 

Why Simulate Slow Internet in Selenium Testing?

Before we get into the "how," let's focus on the "why." A slow internet connection can affect the performance of your application. Users on slow or unreliable networks should have the same smooth experience as those on high-speed connections. Simulating slow Internet conditions during testing helps you identify and fix performance issues and increases your commitment to delivering quality software.

It becomes important for service providers to be able to access critical services during periods of low network connectivity. This is because users will continue to use the same application during these times, and users can also use the application during a disaster or other unpleasant situations where a good network connection is not possible.

 

How to Simulate Slow Internet in Selenium? 

Now, let's get into the details of simulating slow internet in Selenium testing.

Selenium offers various WebDriver settings to replicate slow internet conditions. These settings allow you to control network speed and latency and even simulate offline states.

1. Initialization of DevTools

Casting the driver instance to ChromeDriver and then obtaining the DevTools instance yields the devTools object.

DevTools devTools = ((ChromeDriver) driver). getDevTools();

2. Creating a DevTools Session

To create a session for interacting with the DevTools protocol, devTools calls createSession().

devTools.createSession();

3. Enable Network

To activate the network domain and get control over aspects linked to the network, call the network.enable() function. 

devTools.send(Network.enable(Optional.of(1000000), Optional.empty(),
       Optional.empty()));

4. Simulating Lag in the Network

To simulate particular network conditions—in this example, 2G conditions—use the emulateNetworkConditions() function. The network connection type (CELLULAR2G), download and upload speeds (10000 kbps and 10,000 kbps, respectively), and latency (100 ms) are among the parameters.

devTools.send(
Network.emulateNetworkConditions(false, 100, 10000, 10000,                            Optional.of(ConnectionType.CELLULAR2G)));

You can see that we have used the emulateNetworkConditions function, which accepts five parameters:

  • offline: It accepts a boolean value.

  • latency: the minimum latency from the request sent to the response headers received. The measurement used here is in milliseconds (ms).

  • downloadThroughput: It is the maximal aggregated download throughput (bytes/sec). If we send -1 in this parameter, it will disable the download throttling.

  • uploadThroughput: It is the maximal aggregated upload throughput (bytes/sec). If we send -1 in this parameter, it will disable the upload throttling.

  • connectionType: An enum allows multiple connection types, like CELLULAR2G, CELLULAR3G, CELLULAR4G, etc.

5. WebDriver Guidance

The WebDriver navigates to the target website (https://www.opencart.com) after configuring the network circumstances.

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

As necessary, change the settings for varying network circumstances.

You can find the full code snippet here. The below code works in Chrome and Firefox but not in the Safari browser, as Safari doesn't support the dev tools object. Once we execute the below code, it emulates the network as per the parameters and loads the website.

package mytests;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.devtools.DevTools;

import org.openqa.selenium.devtools.v115.network.Network;

import org.openqa.selenium.devtools.v115.network.model.ConnectionType;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.safari.SafariDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

import java.time.Duration;

import java.util.Optional;



public class NetworkTest {



    WebDriver driver;



@BeforeMethod

public synchronized void setup() {




        String browserName = "chrome";

System.out.println("running test on: " + browserName);



if (browserName.equalsIgnoreCase("chrome")) {


driver = new ChromeDriver();

} else if (browserName.equalsIgnoreCase("firefox")) {

driver = new FirefoxDriver();

} else if (browserName.equalsIgnoreCase("safari")) {

driver = new SafariDriver();

} else {

System.out.println("Please pass the right browser..." + browserName);

        }




        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

        driver.manage().deleteAllCookies();

        driver.manage().window().maximize();

    }




@AfterMethod


public void tearDown() {

        driver.quit();

    }



@Test

public void normalTest() {

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

    }



@Test

public void slowTest() {

DevTools devTools = ((ChromeDriver) driver). getDevTools();

        devTools.createSession();

        devTools.send(Network.enable(Optional.of(1000000),
Optional.empty(),

                Optional.empty()));

        devTools.send(

Network.emulateNetworkConditions(false, 100, 10000, 10000,

                        Optional.of(ConnectionType.CELLULAR2G)));

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

    }

}


 

Benefits of Simulating Slow Internet:

  • Identifying Performance Bottlenecks: Simulating slow internet conditions helps you identify performance bottlenecks early in the development cycle.

  • Ensuring a Seamless User Experience: Testing under various network conditions ensures a consistent user experience. This commitment to user satisfaction remains constant, regardless of internet speed.
  • Enhancing User Retention: A well-performing application, even on slow networks, is more likely to retain users.

 

Challenges of Simulating Slow Internet: 

  • Browser Compatibility: Different browsers may handle network throttling differently. It's important to understand and adapt your testing strategy for various browsers.

  • Overhead: Throttling the network can introduce additional overhead into your test execution, affecting test speed and efficiency.

  • Complex Scenarios: Replicating real-world network conditions accurately can be challenging, as the internet is dynamic and ever-changing.

 

Conclusion 

In conclusion, this article has demonstrated how to simulate slow internet connections using Selenium 4. By understanding why and how to simulate slow internet in Selenium, you are now equipped to enhance your software's performance and ensure a seamless user experience.

Don't compromise on the quality of your software; embrace the power of simulating slow internet with Selenium testing and ensure your application shines, regardless of the internet speed. It is important to note that network throttling in Selenium is not a substitute for real-world testing. The results of network throttling tests may not always accurately reflect the performance of a website or web application in a real-world environment.

Happy Testing!



MagicPod is a no-code AI-driven test automation platform for testing mobile and web applications designed to speed up release cycles. Unlike traditional "record & playback" tools, MagicPod uses an AI self-healing mechanism. This means your test scripts are automatically updated when the application's UI changes, significantly reducing maintenance overhead and helping teams focus on development.


Naman Garg

Written by Naman Garg

Manual and Automation Tester | Quality Promoter | Technology Leader | Lifelong Learner | Software QA Engineer | Product Manager | Scalable Product Builder | Robust Solution Creator | Business Goal Achiever | Social Volunteer