0
0
Selenium Javatesting~10 mins

Performance metrics via DevTools in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page using Selenium with Chrome DevTools Protocol (CDP) integration. It collects performance metrics like First Contentful Paint and DOMContentLoaded event time, then verifies these metrics are present and valid numbers.

Test Code - JUnit
Selenium Java
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v114.performance.Performance;
import org.openqa.selenium.devtools.v114.performance.model.Metric;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

public class PerformanceMetricsTest {
    private ChromeDriver driver;
    private DevTools devTools;

    @BeforeEach
    public void setUp() {
        ChromeOptions options = new ChromeOptions();
        driver = new ChromeDriver(options);
        devTools = driver.getDevTools();
        devTools.createSession();
        devTools.send(Performance.enable(java.util.Optional.empty()));
    }

    @Test
    public void testPerformanceMetrics() {
        driver.get("https://www.example.com");

        // Wait briefly to allow metrics collection
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        List<Metric> metrics = devTools.send(Performance.getMetrics()).getMetrics();

        // Check that key metrics exist and have valid values
        boolean hasFCP = metrics.stream().anyMatch(m -> m.getName().equals("FirstContentfulPaint") && m.getValue() > 0);
        boolean hasDOMLoaded = metrics.stream().anyMatch(m -> m.getName().equals("DomContentLoaded") && m.getValue() > 0);

        assertTrue(hasFCP, "FirstContentfulPaint metric should be present and greater than 0");
        assertTrue(hasDOMLoaded, "DomContentLoaded metric should be present and greater than 0");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            devTools.send(Performance.disable());
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and ChromeDriver with DevTools session is createdChrome browser is launched with DevTools session enabled for performance monitoring-PASS
2Navigates to https://www.example.comBrowser loads the example.com homepage-PASS
3Waits 2 seconds to allow performance metrics to be collectedPage is fully loaded and metrics are being gathered by DevTools-PASS
4Retrieves performance metrics via DevTools Performance.getMetrics()Performance metrics data is available in the testCheck that FirstContentfulPaint metric exists and value > 0PASS
5Checks that DomContentLoaded metric exists and value > 0Metrics list contains DomContentLoaded with valid valueDomContentLoaded metric is present and greater than 0PASS
6Test ends and browser is closed, DevTools session disabledBrowser closed, resources cleaned up-PASS
Failure Scenario
Failing Condition: Performance metrics are missing or have zero/invalid values
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of enabling Performance domain in DevTools before navigation?
ATo disable browser caching
BTo take screenshots of the page
CTo start collecting performance metrics during page load
DTo block network requests
Key Result
Always enable the DevTools Performance domain before navigation to capture accurate performance metrics, and verify key metrics have valid values to ensure meaningful test results.