Skip to main content

Quality Assurance

Simulating Network Offline Mode in Selenium 4

Istock 1394630019

Selenium 4 introduces powerful features that allow you to simulate various network scenarios, providing a robust foundation for testing your web applications. In this blog, we’ll dive into the fascinating realm of simulating offline network conditions using Selenium 4 and Chrome DevTools.

Setting Up Selenium WebDriver with Chrome DevTools

To embark on the journey of network emulation, you need to set up your Selenium WebDriver to leverage Chrome DevTools. Here’s a step-by-step guide:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.devtools.DevTools;


public class OfflineModeExample {

    public static void main(String[] args) {

        ChromeOptions options = new ChromeOptions();

        options.setExperimentalOption("w3c", false);

        WebDriver driver = new ChromeDriver(options);

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

        devTools.createSession();

        // Enable network emulation for offline mode

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

        devTools.send(Network.emulateNetworkConditions(

            true,   // Enable offline mode

            0,      // Latency in milliseconds

            0,      // Download throughput in megabits per second

            0,      // Upload throughput in megabits per second

            Optional.empty(), Optional.empty(), Optional.empty()

        ));

        // Your test logic goes here

        // Disable network emulation (bring back online)

        devTools.send(Network.emulateNetworkConditions(

            false,  // Disable offline mode (bring back online)

            0,

            0,

            0,

            Optional.empty(), Optional.empty(), Optional.empty()

        ));

        // Continue with the rest of your test or cleanup

        driver.quit();

    }

}

 

This code snippet initializes a Chrome WebDriver with DevTools, enables network emulation for offline mode, executes your test logic, and then brings the network back online. Ensure that you adapt the test logic according to your specific application requirements.

 

Simulating Offline Mode: A Practical Example

Let’s explore a practical scenario where simulating offline mode can be invaluable. Consider testing an e-commerce website where users need to access their shopping cart even in low or no connectivity situations.

public class ShoppingCartTest {

    public static void main(String[] args) {

        ChromeOptions options = new ChromeOptions();

        options.setExperimentalOption("w3c", false);

        WebDriver driver = new ChromeDriver(options);

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

        devTools.createSession();

        // Enable network emulation for offline mode

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

        devTools.send(Network.emulateNetworkConditions(

            true,   // Enable offline mode

            0,      // Latency in milliseconds

            0,      // Download throughput in megabits per second

            0,      // Upload throughput in megabits per second

            Optional.empty(), Optional.empty(), Optional.empty()

        ));

        // Navigate to the shopping cart page

        driver.get("https://www.example-ecommerce.com/cart");

        // Perform actions on the shopping cart (e.g., view items, remove items)

        // Disable network emulation (bring back online)

        devTools.send(Network.emulateNetworkConditions(

            false,  // Disable offline mode (bring back online)

            0,

            0,

            0,

            Optional.empty(), Optional.empty(), Optional.empty()

        ));

        // Continue with the rest of your test or cleanup

        driver.quit();

    }

}

 

In this example, the shopping cart page is accessed while the network is offline. The test logic could involve attempting to add items to the cart, viewing the cart contents, or removing items. This allows you to observe how well your application handles such scenarios.

Advantages of Simulating Network Offline Mode

  1. Robustness Testing:

Evaluate how gracefully your application handles scenarios where users lose network connectivity.

Identify and fix potential issues in accessing critical functionalities under offline conditions.

  1. User Experience Testing:

Ensure a seamless user experience even in low or no connectivity situations.

Verify that users can interact with essential features, such as viewing a shopping cart or accessing cached content.

  1. Error Handling Assessment:

Test error messages and notifications displayed to users when the application encounters network issues.

Validate that error messages are informative and guide users on how to proceed.

 

Real-Time Usage Scenarios

Scenario 1: Handling Offline Transactions in a Banking App

Simulate offline mode to test if a banking application allows users to view their transaction history, even when the device is temporarily offline.

 

Scenario 2: Offline Document Editing in a Productivity App

For an online document editing application, verify that users can continue editing documents even without an internet connection, with changes syncing once back online.

 

Scenario 3: Offline Access to Maps in a Travel App

In a travel app with map features, test if users can access offline maps and plan routes when connectivity is limited.

 

When to Use Network Offline Simulation

Critical Features Dependency:

When your application has features crucial for user interaction that heavily rely on network communication.

Error Handling Scenarios:

When assessing how your application responds to network-related errors and communicates them to users.

User Experience Evaluation:

When conducting user experience testing to guarantee a seamless and informative experience under various network conditions.

Custom Conditions: Simulating Specific Offline Scenarios

Selenium 4 allows you to customize network conditions based on your testing requirements. For instance, you can specify values for download/upload speeds, latency, and simulate completely offline conditions. Here’s how you can achieve this:

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

devTools.send(Network.emulateNetworkConditions(

    true,    // offline

    100,     // latency in milliseconds

    5000,    // download throughput in megabits per second

    2000,    // upload throughput in megabits per second

    Optional.empty(), Optional.empty(), Optional.empty()

));

 

In this code snippet, you can customize the latency, download throughput, and upload throughput according to your specific testing needs.

 

Conclusion

Simulating network offline conditions using Selenium 4 and Chrome DevTools is a game-changer in web application testing. It empowers testers and developers to ensure that their applications are not only feature-rich but also resilient in adverse network scenarios.

Thoughts on “Simulating Network Offline Mode in Selenium 4”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Jeet Palan

Jeet Palan is an Technical Consultant at Perficient. He has experience in Manual and Automation testing. In addition to this, he is willing to learn different types of testing and likes to know and learn about new trending technologies.

More from this Author

Categories
Follow Us