Decorative background gradient
Back to Blog
Playwright NextjsE2e TestingCi Cd Testing

Playwright E2E Testing in Next.js — Complete Guide

Set up Playwright for end-to-end testing in a Next.js app — auth state reuse, API mocking, visual regression, and running tests in CI.

Playwright E2E Testing in Next.js — Complete Guide

Playwright has become the standard for Next.js end-to-end testing because it handles the two hardest parts of E2E testing well: authenticated flows and flaky network conditions. Here's a full setup.

Step 1: Install and Initialize Playwright

bash

This scaffolds playwright.config.ts and a tests/ directory, and installs browser binaries for Chromium, Firefox, and WebKit.

ts

Using the production build in webServer.command — not next dev — matters because dev-mode behavior (slower compilation, different error overlays) doesn't reflect what ships.

Step 2: Write a Basic Test

ts

Step 3: Reuse Authentication Across Tests

Logging in via the UI before every test is slow and brittle. Instead, authenticate once in a setup project and save the session.

ts
ts

Every test in the chromium project now starts already signed in, with no login flow executed per test.

Step 4: Mock API Responses to Test Edge Cases

ts

Route interception lets you deterministically test loading, empty, and error states that are otherwise hard to reproduce against a real backend.

Step 5: Add Visual Regression Tests

ts

The first run generates a baseline image; subsequent runs fail if the rendered page differs beyond a configurable pixel threshold, catching visual regressions that functional assertions wouldn't notice.

Step 6: Run Playwright in CI

yaml

Uploading the HTML report as an artifact even on failure (if: always()) is what makes failing CI runs debuggable — the report includes screenshots and traces for every failed step.

Key Takeaways

Playwright's storageState mechanism removes login overhead from every test, route interception makes error and edge-case states testable without a real failing backend, and running against a production build rather than the dev server in CI is what keeps test results representative of what actually ships.

Frequently Asked Questions

Should I test against next dev or next build && next start in CI?

Use the production build (next build && next start) in CI. The dev server has different caching, bundling, and error-overlay behavior than production, so tests passing against next dev can still fail against what actually ships — Playwright's webServer config can run either, but production is the more reliable signal.

How do I avoid logging in before every single Playwright test?

Run a one-time setup project that logs in and saves the authenticated session with page.context().storageState({ path: "auth.json" }), then configure your other test projects to load that file as their starting storageState. Every test then starts already authenticated.

How do I test error states without a real failing backend?

Use page.route() to intercept the specific API call and return a mocked error response (e.g. status 500) before the page loads. This tests your app's error-handling UI deterministically, without needing to actually break a real service.

Does Playwright test multiple browsers by default?

Yes — the default Playwright config defines projects for Chromium, Firefox, and WebKit, and running npx playwright test executes your suite against all three unless you filter with --project. This catches Safari/WebKit-specific bugs that a Chrome-only test suite would miss.

Working on something similar? Take a look at my services and case studies, or book a free call to talk about your idea.

Related Articles

Generative Engine Optimization (GEO) — How to Get Your Website Cited by ChatGPT, Perplexity, and AI Overviews
Generative Engine OptimizationAi Search SeoChatgpt Search+1 more

Generative Engine Optimization (GEO) — How to Get Your Website Cited by ChatGPT, Perplexity, and AI Overviews

More of your customers now ask ChatGPT or Perplexity instead of scrolling Google results. Here's what Generative Engine Optimization actually is and the practical changes that make AI search engines cite your site.

October 18, 2026Read more →
Passkeys in Next.js — Passwordless Login with WebAuthn and SimpleWebAuthn
PasskeysWebauthnNextjs Authentication+1 more

Passkeys in Next.js — Passwordless Login with WebAuthn and SimpleWebAuthn

Passkeys are replacing passwords across every major platform. Here's how to add passkey registration and login to a Next.js App Router app with SimpleWebAuthn — challenges, verification, and the mistakes that break it.

October 18, 2026Read more →
Silent Tool Failures in AI Agents — Why Your Agent Lies About Success
Ai Agent ReliabilityTool Call FailuresLlm Error Handling+1 more

Silent Tool Failures in AI Agents — Why Your Agent Lies About Success

Most AI agent bugs never throw an error — the tool call "succeeds" with empty, truncated, or stale data and the agent confidently reports the task as done. Here's how to catch failures that don't look like failures.

September 22, 2026Read more →
AI Coding Agents Compared in 2026 — Claude Code vs Cursor vs GitHub Copilot
Claude Code Vs CursorAI Coding AgentsGithub Copilot

AI Coding Agents Compared in 2026 — Claude Code vs Cursor vs GitHub Copilot

Compare Claude Code, Cursor, and GitHub Copilot for real development work — agentic workflows, codebase understanding, pricing, and which fits your team.

September 18, 2026Read more →
Best Authentication Libraries for Next.js in 2026 — Clerk vs Auth.js vs Better Auth
Nextjs AuthenticationClerk Vs NextauthBetter Auth

Best Authentication Libraries for Next.js in 2026 — Clerk vs Auth.js vs Better Auth

Compare Clerk, Auth.js (NextAuth), and Better Auth for Next.js — setup effort, hosted UI, pricing, and which fits your app.

September 18, 2026Read more →
Best Error Monitoring & Analytics Tools for Next.js in 2026 — Sentry vs PostHog vs Vercel Analytics
Nextjs Error MonitoringSentry Vs PosthogWeb Analytics

Best Error Monitoring & Analytics Tools for Next.js in 2026 — Sentry vs PostHog vs Vercel Analytics

Compare Sentry, PostHog, and Vercel Analytics for a Next.js app — error tracking, product analytics, session replay, and pricing.

September 18, 2026Read more →

Trending Topics