Frontend Development 6 min read

Advanced Network Interception, Response Analysis, Throttling, and Proxy Configuration with Playwright

This article explains how to use Playwright to intercept and mock network requests, analyze responses, simulate various network conditions including throttling and offline mode, and configure proxy servers, providing Python code examples for comprehensive web‑application testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Advanced Network Interception, Response Analysis, Throttling, and Proxy Configuration with Playwright

Introduction In UI automation testing, controlling and monitoring backend HTTP communication is as important as simulating user interactions. Playwright offers powerful features for intercepting network requests, mocking responses, analyzing response data, and simulating various network conditions.

1. Request Interception and Mocking Demonstrates how to intercept specific API calls and fulfill them with custom JSON data using Playwright's route handling. Includes a complete Python example.

from playwright.sync_api import sync_playwright

def handle_route(route, request):
    # Check if the request URL matches the target API
    if "api.example.com/data" in request.url:
        # Mock response with custom data
        route.fulfill(
            status=200,
            content_type="application/json",
            body='{"key": "mocked_value"}'
        )
    else:
        # Continue the original request
        route.continue_()

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    page = browser.new_page()
    # Register route handler
    page.route("**/*", handle_route)
    # Navigate to page and trigger request
    page.goto("https://example.com")
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

2. Response Analysis and Data Extraction Shows how to listen to all network responses, print status and URL, and directly fetch a specific response's text for further processing.

# Listen to all network responses
page.on("response", lambda response: print(response.status, response.url))

# Get content of a specific response
response = page.request.get("https://api.example.com/data")
print(response.text())

3. Simulating Different Network Conditions Explains how to create a browser context with throttling, proxy, device emulation, and offline mode to mimic real‑world network environments.

browser = playwright.chromium.launch(headless=False)
context = browser.new_context(
    # Simulate 3G network conditions
    viewport={"width": 1920, "height": 1080},
    http_credentials={"username": "user", "password": "pass"},
    offline=False,
    proxy={"server": "http://proxy.example.com:8080"},
    **playwright.devices['iPhone 12']
)
# Apply offline mode
context.set_offline(True)  # offline mode
context.add_init_script(path="path/to/script.js")  # add init script
page = context.new_page()
page.goto("https://example.com")

4. Using a Proxy Server Provides a simple example of launching a Chromium browser with proxy settings so that all requests go through the specified proxy.

browser = playwright.chromium.launch(
    headless=False,
    proxy={
        "server": "http://myproxy.com:8080",
        "username": "user",
        "password": "pass"
    }
)
page = browser.new_page()
page.goto("https://example.com")

Conclusion By mastering request interception, response analysis, network throttling, and proxy configuration with Playwright, testers can gain deeper insight into web application communication mechanisms and ensure robust performance across diverse network scenarios.

proxyAutomationPlaywrightthrottlingweb testingnetwork interception
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.