Localization Testing & Capturing Network in Selenium 4 using CDP

Introduction

This blog explores the essentials of Localization Testing and the efficient use of Chrome DevTools Protocol (CDP) for network data capture in Selenium 4. We'll uncover practical insights to enhance the quality and performance of web applications.

What is Localization Testing?

Localization Testing ensures that a software application meets cultural and language-specific requirements for a region. It focuses on functionality, language, date formats, currency symbols, and other cultural elements to ensure a seamless user experience globally.

Performing Localization Testing

Emulating Geographical Location

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import java.util.HashMap;
import java.util.Map;

public class SetGeolocation {

    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // Initialize ChromeDriver
        ChromeDriver driver = new ChromeDriver();

        // Obtain DevTools instance
        DevTools devTools = driver.getDevTools();
        devTools.createSession();

        // Set geolocation
        Map<String, Object> coordinates = new HashMap<>();
        coordinates.put("latitude", 40);
        coordinates.put("longitude", 3);
        coordinates.put("accuracy", 1);

        // Execute CDP command to set geolocation
        driver.executeCdpCommand("Emulation.setGeolocationOverride", coordinates);

        // Navigate to Google and perform a search
        driver.get("http://google.com");
        driver.findElement(By.name("q")).sendKeys("netflix", Keys.ENTER);

        // Click on the first search result
        driver.findElements(By.cssSelector(".LC20lb")).get(0).click();

        // Get and print the title of the page
        String title = driver.findElement(By.cssSelector(".our-story-card-title")).getText();

        // Output - Películas ilimitadas, programas de TV y más
        System.out.println("Page Title: " + title);

        // Close the driver
        driver.quit();
    }
}

Exploring Network Interactions with Selenium 4 and CDP

The Network domain in Selenium provides the capability to monitor and analyze various network activities of a web page. It reveals details about HTTP, file, data, and other types of requests and responses. This includes information about headers, bodies, timing, and more, offering insights into the communication between the browser and the server during the loading and rendering of a webpage.

1.Capturing Network Requests

import java.util.Optional;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v94.network.Network;

public class CaptureRequestSelenium {

    // Create a method to capture network requests
    public void captureRequestSelenium() {
        // Set the path for the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path to chromedriver");

        // Initialize a ChromeDriver instance
        ChromeDriver driver = new ChromeDriver();

        // Obtain DevTools instance from the ChromeDriver
        DevTools devTools = driver.getDevTools();

        // Create a session using DevTools
        devTools.createSession();

        // Enable network monitoring using Chrome DevTools Protocol (CDP)
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        // Add a listener for the 'requestWillBeSent' event
        devTools.addListener(Network.requestWillBeSent(), requestSent -> {
            // Print the captured request details
            System.out.println("Request URL => " + requestSent.getRequest().getUrl());
            System.out.println("Request Method => " + requestSent.getRequest().getMethod());
            System.out.println("Request Headers => " + requestSent.getRequest().getHeaders().toString());
            System.out.println("------------------------------------------------------");
        });

        // Open a URL in the browser to trigger network requests
        driver.get("https://example.com");
    }
}

2.Capturing Network Responses

import java.util.Optional;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v94.network.Network;

public class CaptureResponseSelenium {

    // Create a method to capture network responses
    public void captureResponseSelenium() {
        // Set the path for the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path to chromedriver");

        // Initialize a ChromeDriver instance
        ChromeDriver driver = new ChromeDriver();

        // Obtain DevTools instance from the ChromeDriver
        DevTools devTools = driver.getDevTools();

        // Create a session using DevTools
        devTools.createSession();

        // Enable network monitoring using Chrome DevTools Protocol (CDP)
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        // Add a listener for the 'responseReceived' event
        devTools.addListener(Network.responseReceived(), responseReceived -> {
            // Print the captured response details
            System.out.println("Response URL => " + responseReceived.getResponse().getUrl());
            System.out.println("Response Status => " + responseReceived.getResponse().getStatus());
            System.out.println("Response Headers => " + responseReceived.getResponse().getHeaders().toString());
            System.out.println("Response MIME Type => " + responseReceived.getResponse().getMimeType().toString());
            System.out.println("------------------------------------------------------");
        });

        // Open a URL in the browser to trigger network responses
        driver.get("https://example.com");
    }
}

Conclusion

In conclusion, Selenium 4, integrated with Chrome DevTools Protocols, proves to be a powerful tool for effective localization testing. By harnessing the capabilities of CDP, testers can seamlessly emulate diverse geographical locations, languages, and cultural settings, ensuring a robust web application across the globe. This feature not only facilitates localization testing but also enables efficient capturing of network requests and responses. Congratulations on mastering these valuable techniques in Selenium automation! 🌐🚀

0
Subscribe to my newsletter

Read articles from Pankaj Suryavanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pankaj Suryavanshi
Pankaj Suryavanshi