Open Chrome browser with network set to CELLULAR4G using Selenium 4, Chrome Devtools and JAVA

Versions used while doing this blog post, (These are the prerequisites to automate chrome browser. Browser and driver versions should match)
selenium-java version – 4.4.0
chrome browser version – 105
chrome driver version – 105 [used WebDriverManager library to auto download needed chrome driver version]

Chrome browser can be opened using selenium by following below steps:
1. Instantiate (creation of object) driver object with ChromeDriver() class using WebDriverManager library (This step opens the chrome browser)

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

2. Instantiate (creation of object) driver object with ChromeDriver() class (This step opens the chrome browser)

ChromeDriver driver = new ChromeDriver(); //--> We use ChromeDriver here, as it extends ChromiumDriver which as devtools methods

3. Now, next steps of maximizing the browser and setting implicit timeout for driver object

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.manage().window().maximize();

4. Now, create DevTools session using getDevTools() and createSession() methods

DevTools devTools = driver.getDevTools();
//Create chrome dev tools session
devTools.createSession();

5. Now, enable the network

//Enable the Network
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//Here, the Network.enable function gets the control of browser network of current session (The 3 parameters are maxTotalBufferSize, maxResourceBufferSize and maxPostDataSize) 

6. Set the network to online and add connection type as cellular 4G

//Set the network to online and add connection type to cellular 4G
devTools.send(Network.emulateNetworkConditions(false,100,300000,150000, Optional.of(ConnectionType.CELLULAR4G)));

//Here the emulateNetworkConditions() method actually sets the expected network conditions. The first parameter is offline which is set as false. Other parameters are latency, downloadThroughput, uploadThroughput and ConnectionType. ConnectionType is cellular 4G here. These parameters values can be tweaked according to your emulation need

6. Launch the expected website URL and observe the offline scenario

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

Sample code for reference

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v104.network.Network;
import org.openqa.selenium.devtools.v104.network.model.ConnectionType;
import java.time.Duration;
import java.util.Optional;

public class ChangeNetworkTo4GOnlineUsingSelenium4 {

    public static void main(String[] args){
        //System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\main\\resources\\chromedriver.exe");
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));

        DevTools devTools = driver.getDevTools();
        //Create chrome dev tools session
        devTools.createSession();

        //Enable the Network
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        //Set the network to online and add connection type to cellular 4G
        devTools.send(Network.emulateNetworkConditions(false,100,300000,150000, Optional.of(ConnectionType.CELLULAR4G)));

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

        driver.quit();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *