New feature Selenium 4: Mock Offline Network and Capture Network Request URL/Method

New feature in Selenium 4 -​​ 
Mock​​ offline​​ network​​ using​​ ChromeDevTools​​ and Capture Network Request URL and Request Method​​ 

We can now emulate or mock an offline​​ network​​ using ChromeDevTools​​ (CDT). 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​​ offline network.

Select any​​ connection type: Cellular2G, 3G​​ etc​​ 

Let us make​​ our​​ first parameter​​ as​​ ‘true’. The default​​ boolean​​ value​​ is offline as shown below. Since we are emulating an offline network, let this value be ‘true’.​​ 

Rest of the parameters are standard​​ 

Let us now add a listener that will keep​​ listening for any exceptions:

devTools.addListener(loadingFailed(),​​ loadingFailed​​ -> System.out.println("Reason for failure --> "​​ +​​ loadingFailed.getErrorText()));

We have to import the below static method. Make sure that the version of devtools (here v85) matches the version of other import statements as seen below:

import​​ static​​ org.openqa.selenium.devtools.v85.network.Network.loadingFailed;​​ 

 

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

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

 

 

We can now launch any website

Below is our complete script

Run the script.

Notice below that the browser opens but the site is not loaded (since our script is emulating offline behaviour) ​​ 

Notice below, the console prints the error text message by listener

This is how we can emulate an offline network.

Let us now see how to capture network request URL and request method.

First of all let us launch a website​​ on a chrome browser, right click and inspect the page, Click Network tab and further click ‘All’

Refresh the page. You can see below that various API calls are being made

Let us select any​​ and go to the ‘Headers’ section on the right. We can see Request URL and Request Method

Same is true for other calls

Let us see how to​​ capture this header information​​ using chrome devTools in Selenium 4.

The​​ following piece of code will do the needful. We have already discussed lines 17-21.​​ 

In line#23, we are adding a listener to send a request and get the URLs and Methods. ​​ Line#32 stops the network traffic that we started at line#21

Run the script, notice below all the Request URLs and methods are captured (for the website listed on line#29 above). These are the same api calls that we saw earlier in the Network section​​ 


​​ 

Code snippet​​ (to mock offline network)

package​​ sel4scripts;

 

import​​ java.util.Optional;

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​​ static​​ org.openqa.selenium.devtools.v85.network.Network.loadingFailed;

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ MockOfflineNetwork {

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()));

 ​​ ​​ ​​​​ 

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

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ devTools.addListener(loadingFailed(),​​ loadingFailed​​ -> System.out.println("Reason for failure --> "​​ +​​ loadingFailed.getErrorText()));

 ​​ ​​ ​​​​ 

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

 ​​ ​​ ​​​​ 

}

}

Code snippet (to capture header info)

package​​ sel4scripts;

 

import​​ java.util.Optional;

 

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​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ CaptuteNwRequests {

public​​ static​​ void​​ main(String[]​​ args) {

  WebDriverManager.chromedriver().setup();  

  WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

  

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

  devTools.createSession();

 ​​ ​​ ​​​​ 

//Start capturing network traffic

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

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ devTools.addListener(Network.requestWillBeSent(),

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ entry​​ -> {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ System.out.println("Request URL--> "​​ +​​ entry.getRequest().getUrl());

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ System.out.println("Request Method--> "​​ +​​ entry.getRequest().getMethod());

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ });

 

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

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ //Stop capturing network traffic

 ​​ ​​ ​​​​ devTools.send(Network.disable());  ​​ ​​ ​​​​ 

}

}

 

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 31st DEC 2024