Testleaf

Selenium 4: Capture & Analyze Network Logs with Chrome DevTools Protocol

https://www.testleaf.com/blog/wp-content/uploads/2025/08/Selenium-4-Capture-Analyze-Network-Logs-with-Chrome-DevTools-Protocol.mp3?_=1

Introduction

Modern Selenium automation testing goes far beyond just clicking buttons and verifying UI changes. Today’s web applications interact with numerous backend APIs, load third-party scripts, trigger analytics beacons, and manage asynchronous requests — all in the background.

While your test might pass visually, a failed API call, a slow-loading resource, or a security vulnerability could be silently impacting the user experience. This is where network log capturing becomes a game-changer.

With Chrome DevTools Protocol (CDP) support in Selenium 4, you can capture and analyze network requests directly — without third-party tools — to detect issues early, improve performance, and boost product quality.

Why Capture Network Logs in Selenium Tests? 

Network logs provide insights that the UI alone cannot reveal. They are invaluable in identifying problems early, debugging tricky scenarios, and ensuring a seamless user experience.
Here’s what network logs can reveal: 

  • Validate API calls triggered by UI actions. 
  • Check request and response payloads for correctness. 
  • Spot performance bottlenecks like slow resources or large images. 
  • Catch failed HTTP requests (404, 500, 403) early. 
  • Debug AJAX-heavy applications. 

Example: 
Your UI test clicks ‘Place Order’, and the page shows ‘Order Successful’. But under the hood, the API call to process payment returned a 401 Unauthorized. Without network logging, your test would pass — and customers would be stuck. 

Recommended for You: automation testing interview questions

Approach: Using Chrome DevTools Protocol in Selenium 4 

The Chrome DevTools Protocol is an interface that allows direct communication with the browser. Through WebSocket commands, you can monitor network requests, console logs, performance metrics, and DOM changes.

In Selenium 4, CDP integration is built-in — no plugins, no extra binaries. 

How It Works for Network Logging 

When you enable network logging with CDP, Selenium directly communicates with Chrome’s debugging interface. Here’s the sequence:
1. Network.enable() → Starts network activity tracking.
2. requestWillBeSent → Captures outgoing HTTP requests.
3. responseReceived → Captures server responses.
4. Apply custom logic → Log, filter, or fail tests based on the captured data. 

 

Java Example: Capturing Requests & Responses 

 import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v125.network.Network;
import org.openqa.selenium.devtools.v125.network.model.Request;
import org.openqa.selenium.devtools.v125.network.model.Response;
import java.util.Optional;

public class NetworkLogCapture {

    public static void main(String[] args) {

        ChromeDriver driver = new ChromeDriver();

        // Create DevTools session
        DevTools devTools = driver.getDevTools();
        devTools.createSession();

        // Enable network logging
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        // Capture requests
        devTools.addListener(Network.requestWillBeSent(), request -> {
            Request req = request.getRequest();
            System.out.println(“REQUEST: ” + req.getUrl());
        });

        // Capture responses
        devTools.addListener(Network.responseReceived(), response -> {
            Response res = response.getResponse();
            System.out.println(“RESPONSE: ” + res.getUrl() + ” | Status: ” + res.getStatus());
        });

        driver.get(“https://example.com”);
        driver.quit();
    }
}
 

Advantages of CDP Method 

  1. Fast & Lightweight — Direct connection to Chrome. 
  2. No Third-Party Libraries — Pure Selenium. 
  3. Perfect for Chrome-Specific Tests. 

Limitations 

  • Chrome-only (or Chromium-based browsers). 
  • Event syntax may differ slightly between Chrome versions. 
  • Requires knowledge of DevTools event names. 

Best Practices for Network Log Testing 

  • Filter logs by domain or request type to avoid noise.
  • Add assertions to fail tests when a key API call fails.
  • Validate payloads by parsing JSON/XML.
  • Run in headed mode first to ensure logging works before switching to headless.
  • Combine network logging with reporting tools like Extent Reports.

Real-World Testing Scenarios with CDP 

  1. E-commerce Checkout Flow → Validate that the payment gateway API returns 200 OK and a correct payload.
  2. Login Validation → Capture token generation API calls and confirm the presence of the token.
  3. Performance Benchmarking → Measure load time of critical resources like CSS, JS, and images.
  4. Security Testing → Detect unexpected calls to unauthorized domains.
  5. Analytics Validation → Confirm that correct analytics events are sent to tracking endpoints. 

 

Final Thoughts 

By integrating Chrome DevTools Protocol with Selenium automation testing, you move beyond surface-level checks and into full-stack quality assurance. You’ll catch critical backend issues, validate data accuracy, and optimize performance before your users even notice a problem.

For Chrome-based automation, CDP is faster, cleaner, and more reliable than traditional proxy tools. If cross-browser coverage is a must, pair CDP testing with tools like BrowserMob Proxy for a complete solution.

 

FAQs

Q1: Can I capture network logs in browsers other than Chrome?

CDP works only for Chrome and Chromium-based browsers. For Firefox or Safari, use proxy-based solutions.

Q2: Does network logging slow down test execution?

Minimal impact — CDP logging is lightweight compared to proxy-based tools.

Q3: Can I capture request payloads and headers?

Yes, CDP provides full access to request/response payloads, headers, and metadata.

Q4: Is CDP logging supported in Selenium Grid?

Yes, but requires Chrome nodes with DevTools enabled.

Q5: How is CDP better than BrowserMob Proxy?

CDP is faster, doesn’t require extra tools, and integrates directly with Chrome.

We Also Provide Training In:
Author’s Bio:

As a Senior SDET with 8+ years in testing and development, I build scalable automation platforms ensuring quality at speed. Passionate about mentoring and innovation, I equip teams with real-time solutions and high-impact frameworks, driving excellence through continuous learning. Let’s shape the future of quality engineering together.

Dilipkumar Rajendran

Senior SDET | Playwright & Selenium Expert

                                                                         

Accelerate Your Salary with Expert-Level Selenium Training

X
Exit mobile version