In one of our blog, we have discussed selenium 4 new features like emulating network conditions & intercept network resources.
We will be discussing these 2 features in detail with selenium4 code implementation.
If we right click on the browser, will get a tab “Network” next to Element tab, where we can manually perform the above operations.
For more Network related operations, you can visit below site to know more
https://chromedevtools.github.io/devtools-protocol/tot/Network/
Emulating network conditions –
Selenium 4 can emulate network conditions like we can run browser applications under different network type 2g / 3g/ 4g or even offline mode
Network class exposes a method emulateNetworkConditions
to deal with this.
code implementation
package sel4; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.network.Network; import org.openqa.selenium.devtools.network.model.ConnectionType; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.Test; import java.util.Optional; import static org.openqa.selenium.devtools.network.Network.loadingFailed; public class emulateNetworkCond { WebDriver driver; WebDriverWait wait; DevTools devTools; @Test public void emulateNetworkConditionTest() throws InterruptedException { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); devTools = ((ChromeDriver)driver).getDevTools(); driver.get("https://www.qavalidation.com"); devTools.createSession(); devTools.send(Network.enable(Optional.of(1000000), Optional.empty(), Optional.empty())); //change the download, upload speed and connection type to verify how your web app reacts devTools.send( Network.emulateNetworkConditions(true, 100, 200000, 100000, Optional.of(ConnectionType.ETHERNET))); //when offline devTools.addListener(loadingFailed(), loadingFailed -> { System.out.println(loadingFailed.getErrorText()); Assert.assertEquals(loadingFailed.getErrorText(), "net::ERR_INTERNET_DISCONNECTED"); }); long startTime = System.currentTimeMillis(); driver.get("https://www.qavalidation.com"); long endTime = System.currentTimeMillis(); System.out.println("page loaded in " + (endTime - startTime)); Thread.sleep(3000); driver.quit(); } }
1st parameter of emulateNetworkConditions
method accepts boolean, true
if you want to run the browser in offline mode, else make it false
Last parameter accepts different network conditions, you will get all available conditions when you put dot after ConnectionType
When you run the offline mode, we can use the loadingFailed() to get the logs from browser i.e – net::ERR_INTERNET_DISCONNECTED
and then capturing the time to load the browser application and know how the application loads with different network conditions.
Intercept Network resources
We can block certain resources like css, images or any media in our web application, and see how the application looks like
For this we will be using setBlockedURLs
method which accepts the immutable list of resources
Code implementation
package sel4; import com.google.common.collect.ImmutableList; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.network.Network; import org.openqa.selenium.devtools.network.model.BlockedReason; import org.openqa.selenium.devtools.network.model.ResourceType; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; import java.util.Optional; import static org.openqa.selenium.devtools.network.Network.loadingFailed; import static org.testng.Assert.assertEquals; public class inetIntercept { WebDriver driver; WebDriverWait wait; DevTools devTools; @Test public void networkIntercepting() throws InterruptedException { //https://chromedevtools.github.io/devtools-protocol/tot/Network/ WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); devTools = ((ChromeDriver)driver).getDevTools(); devTools.createSession(); devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty())); driver.get("https://www.qavalidation.com"); Thread.sleep(3000); //devTools.send(Network.setBlockedURLs(ImmutableList.of("any specific resource path"))); devTools.send(Network.setBlockedURLs(ImmutableList.of("*.png","*.css"))); devTools.addListener(loadingFailed(), loadingFailed -> { if (loadingFailed.getType().equals(ResourceType.STYLESHEET)) { assertEquals(loadingFailed.getBlockedReason(), BlockedReason.INSPECTOR); } else if (loadingFailed.getType().equals(ResourceType.IMAGE)) { assertEquals(loadingFailed.getBlockedReason(), BlockedReason.INSPECTOR); } }); driver.get("https://www.qavalidation.com"); Thread.sleep(3000); driver.quit(); } }
You need to create a session to perform Network operations.
You can find all selenium 4 related code samples on github.
Hope this helps!