How to use ChromiumNetworkConditions class of org.openqa.selenium.chromium package

Best Selenium code snippet using org.openqa.selenium.chromium.ChromiumNetworkConditions

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package com.saucedemo.selenium.se4newfeatures;
2
3import com.saucelabs.saucebindings.junit5.SauceBaseTest;
4import org.junit.jupiter.api.Assertions;
5import org.junit.jupiter.api.Test;
6import org.openqa.selenium.WebDriver;
7import org.openqa.selenium.WebDriverException;
8import org.openqa.selenium.chromium.ChromiumNetworkConditions;
9import org.openqa.selenium.chromium.HasNetworkConditions;
10import org.openqa.selenium.remote.Augmenter;
11
12public class ChromeNetworkTest extends SauceBaseTest {
13
14    @Test
15    public void toggleOffline() {
16        WebDriver augmentedDriver = new Augmenter().augment(driver);
17        ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions();
18        networkConditions.setOffline(true);
19        ((HasNetworkConditions) augmentedDriver).setNetworkConditions(networkConditions);
20
21        try {
22            driver.get("https://www.saucedemo.com");
23            Assertions.fail("If Network is set to be offline, the previous line should throw an exception");
24        } catch (WebDriverException ex) {
25            ((HasNetworkConditions) augmentedDriver).setNetworkConditions(new ChromiumNetworkConditions());
26        }
27        driver.get("https://www.saucedemo.com");
28    }
29}
30
Full Screen
copy
1package chrome_devtools;
2
3import io.github.bonigarcia.wdm.WebDriverManager;
4import io.github.bonigarcia.wdm.managers.ChromiumDriverManager;
5import org.openqa.selenium.WebDriver;
6import org.openqa.selenium.chrome.ChromeDriver;
7import org.openqa.selenium.chromium.AddHasNetworkConditions;
8import org.openqa.selenium.chromium.ChromiumDriver;
9import org.openqa.selenium.chromium.ChromiumNetworkConditions;
10import org.openqa.selenium.devtools.DevTools;
11import org.openqa.selenium.devtools.v85.log.Log;
12import org.openqa.selenium.devtools.v85.network.model.ConnectionType;
13import org.openqa.selenium.devtools.v94.network.Network;
14import org.openqa.selenium.mobile.NetworkConnection;
15import org.testng.annotations.Test;
16
17import java.util.Optional;
18
19import static org.openqa.selenium.devtools.v85.network.Network.emulateNetworkConditions;
20
21public class ChromeDevToolsDemo {
22
23    WebDriver driver;
24
25    @Test
26    public void enableNetworkOffline() {
27        WebDriverManager.chromedriver().setup();
28        driver=new ChromeDriver();
29
30        DevTools devTools=((ChromiumDriver)driver).getDevTools();
31
32        devTools.createSession();
33        devTools.send(Network.enable(Optional.of(1000000),Optional.empty(),Optional.empty()));
34
35        devTools.send(emulateNetworkConditions(true,100,1000,2000,Optional.of(ConnectionType.WIFI)));
36
37        driver.get("https://formy-project.herokuapp.com/");
38    }
39
40    @Test(description = "Get console logs")
41    public void generateConsoleLogs() {
42        WebDriverManager.chromedriver().setup();
43        driver=new ChromeDriver();
44
45        DevTools devTools=((ChromiumDriver)driver).getDevTools();
46
47        devTools.createSession();
48
49        devTools.send(Log.enable());
50//        devTools.addListener(Log.entryAdded(),entry -> System.out.println(entry.asSeleniumLogEntry()));
51
52        driver.get("https://formy-project.herokuapp.com/");
53    }
54}
55
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on ChromiumNetworkConditions

    No relevant questions found for this class ūüėě

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)