To add end-to-end tests to Astro with Playwright, run npm init playwright@latest, then point Playwright’s webServer at npm run build && npm run preview so tests run against the real production build on http://localhost:4321. Playwright starts the server, waits for it, runs your browser tests and shuts it down again.
Key Takeaways
- Test the built site with
astro preview, not the dev server, so you catch production-only problems.webServerinplaywright.config.tsbuilds and serves the site automatically.- Start with three tests: key pages load, navigation works, and forms or islands behave.
- Run the same config in CI with
npx playwright install --with-deps.
Install
# Terminal
npm init playwright@latest
The installer asks a few questions: choose TypeScript, a tests folder and whether to add a GitHub Actions workflow. It installs @playwright/test and the browsers.
Configure the web server
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './tests',
use: {
baseURL: 'http://localhost:4321',
},
webServer: {
command: 'npm run build && npm run preview',
url: 'http://localhost:4321',
reuseExistingServer: !process.env.CI,
timeout: 120_000,
},
projects: [
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
{ name: 'mobile', use: { ...devices['Pixel 7'] } },
],
});
reuseExistingServer lets you keep astro preview running locally between test runs. If you set a base in astro.config.mjs, add it to baseURL and url. A mobile project matters for Astro sites, because menus and islands often behave differently on small screens.
Test that pages load
// tests/pages.spec.ts
import { expect, test } from '@playwright/test';
const pages = ['/', '/about/', '/blog/', '/contact/'];
for (const path of pages) {
test(`${path} loads with a heading`, async ({ page }) => {
const response = await page.goto(path);
expect(response?.status()).toBe(200);
await expect(page.locator('h1')).toBeVisible();
});
}
test('unknown URLs show the 404 page', async ({ page }) => {
const response = await page.goto('/this-does-not-exist/');
expect(response?.status()).toBe(404);
});
The 404 test confirms your custom 404.astro page is wired up; see our custom 404 tip.
Test navigation and an island
// tests/navigation.spec.ts
import { expect, test } from '@playwright/test';
test('mobile menu opens and navigates', async ({ page, isMobile }) => {
test.skip(!isMobile, 'mobile menu only');
await page.goto('/');
await page.getByRole('button', { name: /menu/i }).click();
await page.getByRole('link', { name: 'Blog' }).click();
await expect(page).toHaveURL(/\/blog\/$/);
});
test('newsletter form shows a confirmation', async ({ page }) => {
await page.goto('/');
await page.getByLabel('Email').fill('[email protected]');
await page.getByRole('button', { name: /subscribe/i }).click();
await expect(page.getByText(/thanks/i)).toBeVisible();
});
Role and label locators double as a light accessibility check: if a button has no accessible name, the test cannot find it. For a fuller check, add @axe-core/playwright and run it on key pages. In tests that submit to a real form backend, use page.route() to intercept the request instead of sending live submissions.
Run in CI
# .github/workflows/e2e.yml
name: e2e
on: [pull_request]
jobs:
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
- run: npm ci
- run: npx playwright install --with-deps
- run: npx playwright test
- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-report
path: playwright-report/
Current Astro releases need Node.js 22.23.4 or newer. Uploading the report on failure gives you screenshots and traces for each failed test. You can also run the suite against a preview deployment URL instead of a local server; see the preview deployments tip.
For unit tests, pair this with Vitest, and see the testing tools overview for how they fit together. Themes in our Astro theme catalog build with npm run build and preview with npm run preview, so this config works as-is. The Playwright web server docs list every webServer option.
FAQ
How do I run Playwright tests against an Astro site?
Set webServer.command to npm run build && npm run preview and url to http://localhost:4321 in playwright.config.ts. Playwright builds, serves and tests the site in one command.
Should Playwright test astro dev or astro preview?
Use astro preview. It serves the production build, so tests catch problems with bundling, image optimisation and routing that the dev server hides.
How do I test Astro islands with Playwright?
Interact with them as a user would: click, type and assert on what appears. Playwright waits for elements automatically, which covers islands that hydrate with client:visible or client:idle.
Can Playwright check for broken links on an Astro site?
Yes. Collect every a[href] on key pages, request each internal URL with page.request.get(), and assert a 200 status. For large sites a dedicated link checker on the dist folder is faster.