New feature in Selenium 4: Intercept (Block) network traffic using CDT and Ignore Certificate Errors

New feature in Selenium 4 -​​ 
Intercept (Block)​​ network​​ traffic​​ using​​ CDT​​ and​​ Ignore​​ Certificate Errors​​ 

When​​ we inspect any ecommerce site, we can see that there are lot of jpg, gif, png images are loaded. We can ignore these images when we are running our automation test


The line#25 below will serve the purpose. We have to just mention the list of images extensions that we want to block/disable/intercept when we are running our test

Run this test, notice below that the images did not download

Let us now see how we can ignore SSL certificates error. Let us go to the site​​ https://expired.badssl.com/​​ 

Notice below that​​ we get SSL certificate error

Now, how can we ignore this error?

Before we see how to ignore this error, let us first run our below test

Notice below that we get the same error

Next, let us add below 2 lines. In line#23, we are simply setting the ‘setIgnoreCertificateErrors’ to be true ​​ 

After this setting, let us again launch the same site

Run script, notice below that the certificate error is no more

Let us try out with another site​​ https://self-signed.badssl.com/​​ 

Change below lines

Run


Code snippet​​ (to​​ block/intercept network traffic)

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​​ com.google.common.collect.ImmutableList;

 

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

 

public​​ class​​ Network_Intercept {

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.send(Network.setBlockedURLs(ImmutableList.of(".png",​​ ".jpg",​​ ".gif")));

 

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

 ​​ ​​ ​​​​ 

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

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

}

}

Code snippet (to​​ ignore SSL cert)

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.security.Security;

 

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

 

public​​ class​​ Ignore_SSLCert {

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

  WebDriverManager.chromedriver().setup();  

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

  

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

  devTools.createSession();

 ​​ ​​ ​​​​ 

  //Before ignoring SSL​​ cert

  driver.get("https://expired.badssl.com/");

  

  devTools.send(Security.enable());

  devTools.send(Security.setIgnoreCertificateErrors(true));

   

  ​​ ​​ ​​​​ //After​​ ignoring SSL​​ cert​​ 

  driver.get("https://expired.badssl.com/");

}

}

 

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