⚡️ Network Nightmare: Is Your App Secretly Slow?
Your Playwright tests pass, but users complain about sluggishness or broken API calls. How do you pinpoint those hidden network bottlenecks and validate every backend interaction?
📌 Problem Statement
Modern web applications heavily rely on efficient network communication. Slow requests, failed API calls, or large asset downloads can cripple user experience. Standard E2E tests often miss these subtle network-level issues.
• Challenge: Diagnosing network performance, validating specific API payloads, or debugging intermittent network failures during automated test runs.
• Solution: Leveraging HAR (HTTP Archive) files, a standardized JSON format recording all browser network activity, including requests, responses, timings, and headers.
💡 Solution & Code Walkthrough
Playwright offers built-in tracing capabilities to capture HAR files effortlessly. By enabling HAR capture during a test run, you get a detailed log of all network events for deep analysis.
import { test, expect } from '@playwright/test';
test.describe('HAR Capture for Network Diagnostics', () => {
let harPath: string; // Path to store HAR file
test.beforeEach(async ({ page, browser }) => {
harPath = `network-trace-${Date.now()}.har`; // Unique HAR filename
// ✅ Start tracing with HAR capture enabled
await browser.startTracing(page, {
har: true, // Crucial: Enables HAR file generation
path: harPath, // Defines where the HAR file will be saved
// snapshots: true, // Optional: captures DOM snapshots
// screenshots: true, // Optional: captures screenshots
});
});
test('validate API calls and page performance', async ({ page }) => {
// ✅ Perform actions that generate network traffic
await page.goto('https://playwright.dev');
await page.locator('text=Docs').click();
await page.waitForLoadState('networkidle'); // Wait for network to settle
// Add assertions for page content or UI elements here
});
test.afterEach(async ({ browser }) => {
// ✅ Stop tracing, saving the HAR file to 'harPath'
await browser.stopTracing();
// ℹ️ In a real scenario, you'd load and analyze 'harPath' here
// e.g., using a Node.js HAR parser to assert specific requests/responses.
});
});
• Execution: Run your Playwright tests. A .har file will be generated in your project root for each beforeEach block.
• Analysis: Use HAR viewers (online tools, browser dev tools) or programmatic parsers (e.g., har-parser npm package) to inspect the captured data.
🔑 Key Takeaways
• ✅ Deep Insights: HAR files provide granular detail for every network request/response, invaluable for debugging complex issues.
• ✅ Performance Metrics: Analyze request timings, resource loading, and cascade to identify performance bottlenecks.
• ✅ API Validation: Verify correct API endpoint calls, request headers, body content, and response statuses/payloads.
• ❌ Overhead: Capturing HAR files adds some overhead; use it selectively for network-critical tests.
• ❌ Cleanup: Remember to manage generated HAR files; clean them up or archive them as needed.
❓ Quick Summary Q&A
• Q: What is a HAR file? A: A JSON-formatted log of all network activity during a browser session.
• Q: How to enable HAR capture in Playwright? A: Use browser.startTracing(page, { har: true, path: 'your-file.har' }).
TAGS: playwright, testing, har, performance testing, api testing, network, automation, javascript, typescript, sdet
────────────────────────────────────────
Enhance your developer skills on the go!
────────────────────────────────────────
📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:
🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261124
🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261124&mt=8
────────────────────────────────────────
────────────────────────────────────────
Top comments (0)