End-to-End Testing with Playwright (Overview)
What Is E2E?
End-to-end tests open your real app in a browser and act like a user: click, type and check the screen. They find problems that unit tests cannot, such as broken routing or a missing button.
| Test type | Speed | Covers |
|---|---|---|
| Unit | Very fast | One component or function |
| Integration | Fast | A few pieces together |
| E2E | Slower | The whole app and server |
Tool choice: Newer Angular CLI versions no longer ship a default e2e tool, so you choose one. Playwright and Cypress are the most popular. This lesson uses Playwright.
Install
bash
pm init playwright@latest
# choose TypeScript, tests folder e2e, install browsers
Configuration
typescript
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './e2e',
retries: process.env['CI'] ? 2 : 0,
use: { baseURL: 'http://localhost:4200', trace: 'on-first-retry' },
webServer: {
command: 'npm start', // starts ng serve
url: 'http://localhost:4200',
reuseExistingServer: !process.env['CI'],
},
projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
});
Your First Tests
typescript
import { test, expect } from '@playwright/test';
test('home page shows the title', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('heading', { name: 'Welcome' })).toBeVisible();
});
test('user can log in and see the dashboard', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('ann@example.com');
await page.getByLabel('Password').fill('secret123');
await page.getByRole('button', { name: 'Log in' }).click();
await expect(page).toHaveURL(/dashboard/);
await expect(page.getByText('Hello, Ann')).toBeVisible();
});
test('shows an error for a wrong password', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('ann@example.com');
await page.getByLabel('Password').fill('wrong');
await page.getByRole('button', { name: 'Log in' }).click();
await expect(page.getByRole('alert')).toContainText('Wrong email or password');
});
Mock the API
typescript
test('shows products from the API', async ({ page }) => {
await page.route('**/api/products', route =>
route.fulfill({ json: [{ id: 1, name: 'Laptop', price: 999 }] }));
await page.goto('/products');
await expect(page.getByText('Laptop')).toBeVisible();
});
Locator Tips
| Prefer | Because |
|---|---|
getByRole | Matches how users and screen readers see the page |
getByLabel | Great for form fields |
getByText | Visible text |
getByTestId | Stable fallback with data-testid |
| CSS selectors | Last choice, they break easily |
Run
bash
px playwright test # all tests, headless
npx playwright test --ui # interactive UI mode
npx playwright show-report # HTML report
npx playwright codegen localhost:4200 # record actions into code
Page Objects (Keep Tests Tidy)
typescript
export class LoginPage {
constructor(private page: Page) {}
goto() { return this.page.goto('/login'); }
async login(email: string, password: string) {
await this.page.getByLabel('Email').fill(email);
await this.page.getByLabel('Password').fill(password);
await this.page.getByRole('button', { name: 'Log in' }).click();
}
}
How many E2E tests? Keep only a few for the most important journeys (login, checkout, create item). Cover the details with fast unit tests.
Conclusion
E2E tests drive a real browser. Playwright starts your dev server, finds elements by role and label, can mock APIs and runs well in CI. Keep them few and focused on key user journeys.
Quick Summary: npm init playwright + webServer config + role/label locators.

