E2E testing checks if your whole app works well from start to finish. It helps catch problems users might face.
E2E testing with Playwright in NextJS
Introduction
Syntax
NextJS
import { test, expect } from '@playwright/test'; test('test name', async ({ page }) => { await page.goto('http://localhost:3000'); await page.click('selector'); await expect(page.locator('selector')).toHaveText('expected text'); });
Use page.goto(url) to open a page.
Use page.click(selector) to click elements.
Examples
NextJS
test('check homepage title', async ({ page }) => { await page.goto('http://localhost:3000'); await expect(page).toHaveTitle('Home Page'); });
NextJS
test('login form works', async ({ page }) => { await page.goto('http://localhost:3000/login'); await page.fill('input[name="email"]', 'user@example.com'); await page.fill('input[name="password"]', 'password123'); await page.click('button[type="submit"]'); await expect(page.locator('text=Welcome')).toBeVisible(); });
Sample Program
This test simulates a user going to the homepage, clicking login, entering credentials, submitting, and seeing a welcome message.
NextJS
import { test, expect } from '@playwright/test'; test('user can navigate and see welcome message', async ({ page }) => { await page.goto('http://localhost:3000'); await page.click('text=Login'); await page.fill('input[name="email"]', 'testuser@example.com'); await page.fill('input[name="password"]', 'testpass'); await page.click('button[type="submit"]'); await expect(page.locator('text=Welcome, testuser')).toBeVisible(); });
Important Notes
Use clear and stable selectors like text or data-testids for locating elements.
Run tests in headed mode to watch what happens during development.
Keep tests small and focused on one user action at a time.
Summary
E2E tests check the full user experience in your app.
Playwright lets you write simple scripts to open pages, click, fill forms, and check results.
Good selectors and clear steps make tests reliable and easy to maintain.
Practice
1. What is the main purpose of E2E testing in a Next.js application using Playwright?
easy
Solution
Step 1: Understand E2E testing scope
E2E (end-to-end) testing simulates real user actions across the whole app, not just parts.Step 2: Identify Playwright's role
Playwright automates browser actions to test full user flows, like clicking and filling forms.Final Answer:
To test the entire user experience from start to finish -> Option DQuick Check:
E2E testing = full user experience [OK]
Hint: E2E tests cover full user flows, not just parts [OK]
Common Mistakes:
- Confusing E2E with unit or integration tests
- Thinking E2E tests only check backend APIs
- Assuming E2E tests verify only UI styles
2. Which of the following is the correct syntax to launch a browser and open a new page in Playwright for Next.js E2E testing?
easy
Solution
Step 1: Recall Playwright import and usage
Playwright's browser types like chromium are imported directly, so you use chromium.launch().Step 2: Check async usage and method names
launch() and newPage() are async and must be awaited; syntax must match Playwright docs.Final Answer:
const browser = await chromium.launch(); const page = await browser.newPage(); -> Option AQuick Check:
Use 'await chromium.launch()' and 'await browser.newPage()' [OK]
Hint: Use 'await chromium.launch()' and 'await browser.newPage()' [OK]
Common Mistakes:
- Using 'playwright.chromium.launch()' instead of 'chromium.launch()'
- Missing 'await' keyword for async calls
- Calling 'newPage()' directly on Playwright instead of browser
3. Given this Playwright test snippet for a Next.js app:
What will be logged if the button triggers showing 'Success' inside
await page.goto('http://localhost:3000');
await page.click('button#submit');
const text = await page.textContent('div.result');
console.log(text);What will be logged if the button triggers showing 'Success' inside
div.result?medium
Solution
Step 1: Understand the test flow
The test navigates to the page, clicks the submit button, then reads text from 'div.result'.Step 2: Analyze expected page behavior
Clicking the button triggers showing 'Success' inside 'div.result', so textContent returns 'Success'.Final Answer:
'Success' -> Option CQuick Check:
textContent('div.result') = 'Success' after click [OK]
Hint: Click triggers text update; textContent reads updated text [OK]
Common Mistakes:
- Assuming textContent returns undefined if element exists
- Expecting error without checking if selector exists
- Thinking textContent returns empty string if no text
4. You wrote this Playwright test for a Next.js app:
But the test fails with timeout error. What is the most likely cause?
await page.goto('http://localhost:3000');
await page.click('button.submit');
await expect(page.locator('div.result')).toHaveText('Success');But the test fails with timeout error. What is the most likely cause?
medium
Solution
Step 1: Analyze failure reason
Timeout usually means the expected text never appeared in 'div.result'.Step 2: Check selector correctness
If 'button.submit' selector is wrong (e.g., class is 'submit-btn'), click never happens, so text never changes.Final Answer:
The selector 'button.submit' does not match any element -> Option BQuick Check:
Wrong selector = no click = assertion timeout [OK]
Hint: Check selectors carefully; wrong selector blocks actions [OK]
Common Mistakes:
- Assuming assertion syntax is wrong without checking selectors
- Ignoring that click may fail silently if selector is wrong
- Not verifying page URL correctness
5. You want to write a Playwright test for a Next.js app that fills a login form and verifies the welcome message. Which sequence of steps is correct?
hard
Solution
Step 1: Understand logical user flow
User must first visit login page, then fill form fields, then submit.Step 2: Verify post-submit behavior
After submitting, wait for welcome message to appear, then assert its text.Final Answer:
Go to login page, fill username and password, click submit, wait for welcome message, assert text -> Option AQuick Check:
Follow user steps in order: visit, fill, submit, check [OK]
Hint: Follow real user steps: open page, fill, submit, then check [OK]
Common Mistakes:
- Trying to assert before submitting the form
- Clicking submit before filling fields
- Checking messages before navigating to page
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
