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 offline and add a listener to verify loading failed message from browser
//Set the network to offline
devTools.send(Network.emulateNetworkConditions(true, 100, 1000, 2000, Optional.of(ConnectionType.CELLULAR4G)));
//Here the emulateNetworkConditions() method actually sets the expected network conditions. The first parameter is offline which is set as true. Other parameters are latency, downloadThroughput, uploadThroughput and ConnectionType.
try {
driver.get("https://www.google.com");
}
catch(WebDriverException e){
System.out.println("Webdriver exception due to no internet? -> " + e.getMessage().contains("ERR_INTERNET_DISCONNECTED"));
}
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.WebDriverException;
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 ChangeNetworkToOfflineUsingSelenium4 {
public static void main(String[] args){
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 offline
devTools.send(Network.emulateNetworkConditions(true, 100, 1000, 2000, Optional.of(ConnectionType.CELLULAR4G)));
try {
driver.get("https://www.google.com");
}
catch(WebDriverException e){
System.out.println("Webdriver exception due to no internet? -> " + e.getMessage().contains("ERR_INTERNET_DISCONNECTED"));
}
driver.quit();
}
}