How to simulate network conditions

It is useful to be able to run Selenium tests on different types of networks, having different download and upload throughput.

This was not possible until recently when the needed support was added to the ChromeDriver class.

See below the code that opens the Google home page on multiple download and upload speeds:

import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.Response;
import org.testng.annotations.DataProvider;
public class TestClass {
   //data provider for the connection's download and upload throughput    
   @DataProvider(name = "test1")
    public Object[][] networkConditions() {
        return new Object[][] {
            { 5000 , 5000 },
            { 10000, 7000 },
            { 15000, 9000 },
            { 20000, 10000 },
            { 23000, 11000 },
            { 30000, 15000 },
            { 40000, 20000 },
            { 50000, 20000 },
            { 75000, 20000 },
            { 100000, 20000 },
            { 0, 0 },
        };
    }
    
    @Test(dataProvider = "networkConditions")
    public void test(int downloadThroughput, int uploadThroughput)
throws IOException {
        System.setProperty("webdriver.chrome.driver",
"c:/selenium/chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        
        if (downloadThroughput > 0 && uploadThroughput > 0) {
            CommandExecutor executor = driver.getCommandExecutor();
                   
            Map map = new HashMap();
            map.put("offline", false);
            map.put("latency", 5);
                
            map.put("download_throughput", downloadThroughput);
            map.put("upload_throughput", uploadThroughput);
            Response response = executor.execute(
new Command(driver.getSessionId(), 
"setNetworkConditions", 
 ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map))));
        }
            
        driver.get("http://google.com");
        
        driver.quit();
    }
    
}

 

The code should be easy to follow.

 

A hashmap is created with the network conditions to be used for the browser.

 

The driver gets a command executor and uses it to execute a command on the current session to set the network conditions based on the hash map.

 

These are the results of executing the code:

 

download: 5000   , upload: 5000  , finished in 87221 ms
download: 10000  , upload: 7000  , finished in 46126 ms
download: 15000  , upload: 9000  , finished in 32165 ms
download: 20000  , upload: 10000 , finished in 25703 ms
download: 23000  , upload: 11000 , finished in 22966 ms
download: 30000  , upload: 15000 , finished in 19065 ms
download: 40000  , upload: 20000 , finished in 15008 ms
download: 50000  , upload: 20000 , finished in 13116 ms
download: 75000  , upload: 20000 , finished in 11031 ms
download: 100000 , upload: 20000 , finished in 9463 ms
download: 0      , upload: 0     , finished in 5874 ms

 

If the download and upload are 0, the code is executed with no network conditions specified.

 

In all other cases, a download and upload throughput is being used.

One Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s