End-to-End Testing with Playwright in React0%

End-to-End Testing with Playwright in React

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

End-to-End Testing with Playwright in React

While unit and integration tests run inside an artificial Node.js DOM simulator (jsdom), End-to-End (E2E) testing runs your compiled React application inside real headless or headed browser engines (Chromium, Firefox, WebKit). Microsoft Playwright is the industry standard for E2E testing, offering automatic waiting, cross-browser execution, mobile emulation, and network mocking.


1. Playwright Architecture and Advantages

  • Real Browser Engines: Tests execute against real Chromium, WebKit (Safari), and Firefox engines.
  • Auto-Waiting: Playwright automatically waits for elements to become visible, enabled, and stable before clicking, eliminating flaky sleep() or setTimeout() statements.
  • Trace Viewer & Video Recording: Records video, screenshots, and DOM snapshots on failure for effortless debugging.
  • Isolated Browser Contexts: Each test runs in a sandboxed browser context, equivalent to a clean incognito window.

2. Playwright Configuration (playwright.config.ts)

TypeScript
import { defineConfig, devices } from "@playwright/test";
 
export default defineConfig({
testDir: "./e2e",
fullyParallel: true,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 2 : undefined,
reporter: [["html", { open: "never" }], ["list"]],
use: {
baseURL: "http://localhost:3000",
trace: "on-first-retry",
video: "retain-on-failure",
screenshot: "only-on-failure",
},
// Automatically start dev server before running test suite
webServer: {
command: "npm run dev",
url: "http://localhost:3000",
reuseExistingServer: !process.env.CI,
},
projects: [
{ name: "chromium", use: { ...devices["Desktop Chrome"] } },
{ name: "firefox", use: { ...devices["Desktop Firefox"] } },
{ name: "webkit", use: { ...devices["Desktop Safari"] } },
{ name: "Mobile Chrome", use: { ...devices["Pixel 5"] } },
],
});

3. Writing an E2E Authentication & Dashboard Test

TypeScript
// e2e/auth-flow.spec.ts
import { test, expect } from "@playwright/test";
 
test.describe("Enterprise Authentication Flow", () => {
test("authenticates user and loads analytics dashboard", async ({ page }) => {
// 1. Navigate to login view
await page.goto("/login");
 
// Assert page title
await expect(page).toHaveTitle(/Enterprise Portal/i);
 
// 2. Locate form elements using locator accessibility methods
const emailInput = page.getByLabel("Corporate Email");
const passwordInput = page.getByLabel("Password");
const submitBtn = page.getByRole("button", { name: /sign in/i });
 
// 3. Fill and submit credentials
await emailInput.fill("lead.architect@enterprise.com");
await passwordInput.fill("SecureSecret123!");
await submitBtn.click();
 
// 4. Assert URL redirected to dashboard
await expect(page).toHaveURL(/.*dashboard/);
 
// 5. Assert user greeting and stats card rendered
const greeting = page.getByRole("heading", { name: /welcome back/i });
await expect(greeting).toBeVisible();
 
const revenueCard = page.getByTestId("kpi-revenue");
await expect(revenueCard).toContainText("$1,240,500");
});
 
test("displays validation error on invalid credentials", async ({ page }) => {
await page.goto("/login");
 
await page.getByLabel("Corporate Email").fill("invalid@user.com");
await page.getByLabel("Password").fill("WrongPassword");
await page.getByRole("button", { name: /sign in/i }).click();
 
// Assert error alert pops up
const errorAlert = page.getByRole("alert");
await expect(errorAlert).toBeVisible();
await expect(errorAlert).toHaveText(/invalid email or password/i);
});
});

4. Mocking API Routes in Playwright

Playwright allows mocking network requests at the browser socket level without modifying React source code:

TypeScript
test("displays offline maintenance banner when API returns 503", async ({ page }) => {
// Intercept route before navigating
await page.route("**/api/v1/health", (route) => {
route.fulfill({
status: 503,
contentType: "application/json",
body: JSON.stringify({ status: "Service Under Maintenance" }),
});
});
 
await page.goto("/dashboard");
await expect(page.getByText(/scheduled maintenance in progress/i)).toBeVisible();
});

Interactive Quiz

Practice Quiz

Question 1
What is the primary difference between running tests in JSDOM (React Testing Library) vs Playwright?
Question 2
How does Playwright eliminate flaky test timeouts?
Question 3
What is the purpose of the webServer configuration block in playwright.config.ts?
Question 4
How does page.route() work in Playwright?
Question 5
What artifact does Playwright generate on failure that provides step-by-step interactive debugging with DOM inspection?
Next Lesson

Visual Regression and Snapshot Testing Strategies

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext