New feature in Selenium 4 –> Mock 2g/3g/4g/Wifi network using ChromeDevTools

Mock​​ 2g/3g/4g/Wifi network​​ using​​ ChromeDevTools (new feature​​ in Selenium 4)

We can now emulate or mock a 2g/3g/4g/wifi/ethernet network​​ using ChromeDevTools​​ (CDT)​​ option. This is a new feature introduced in Selenium 4.

We start with creating an object of DevTools and then create a devTools session

 ​​​​ 

We then enable few network parameters (maxTotalBufferSize etc).​​ 

Below​​ is​​ a​​ standard snippet that we can use​​ to set the parameters

We​​ are now set to​​ emulate​​ an​​ online network.

Notice below that we have various connection types available: Bluetooth, Cellular2G, 3G​​ etc​​ 

Let us select Cellular2G

The reason we have made first parameter ‘false’ is because it’s a boolean value. The default value (true) is offline as shown below. Since we are emulating an online network, we change the value to ‘false’.​​ 

Rest of the parameters are standard​​ 

Let us​​ test this 2g network by launching a site.​​ 

Let us capture the time before and after launching a site

The time difference would give us the measure of time that was used to launch the page​​ (in milliseconds)

Below is our complete script

Run the script

Notice the console o/p. The 2G network took ~14 seconds to load the page

Let us change to 3G now

It took ~6 sec now

Let us change to 4G

Wifi

This is how we can emulate an online network

Code snippet

package sel4scripts;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.devtools.DevTools;

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

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

import java.util.Optional;

import io.github.bonigarcia.wdm.WebDriverManager;

 

public class Mock2g3gWifiNetwork {

public static void main(String[] args) {

  WebDriverManager.chromedriver().setup();  

  WebDriver driver = new ChromeDriver();

  

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

  devTools.createSession();

 ​​ ​​ ​​​​ 

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

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ //Mock Online 2G Network

 ​​ ​​ ​​​​ //devTools.send(Network.emulateNetworkConditions(false, 200, 100000, 200000, Optional.of(ConnectionType.CELLULAR2G)));

 ​​ ​​ ​​​​ 

 ​​​​ //Mock Online 3G Network

 ​​ ​​ ​​​​ devTools.send(Network.emulateNetworkConditions(false, 200, 100000, 200000, Optional.of(ConnectionType.WIFI)));

 

 ​​ ​​ ​​​​ long startTimeMilliSec = System.currentTimeMillis();

 ​​ ​​ ​​​​ 

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

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ long endTimeMilliSec = System.currentTimeMillis();

 

 ​​ ​​ ​​​​ System.out.println("Home page landing time: " + (endTimeMilliSec - startTimeMilliSec));

 ​​ ​​ ​​​​ 

}

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024