Set up Playwright in a project. Use when user says "set up playwright", "add e2e tests", "configure playwright", "testing setup", "init playwright", or "add test infrastructure".
Set up a production-ready Playwright testing environment. Detect the framework, generate config, folder structure, example test, and CI workflow.
Use the Explore subagent to scan the project:
package.json for framework (React, Next.js, Vue, Angular, Svelte)tsconfig.json → use TypeScript; otherwise JavaScript@playwright/test in dependencies)tests/, e2e/, __tests__/).github/workflows/, .gitlab-ci.yml)If not already installed:
npm init playwright@latest -- --quiet
Or if the user prefers manual setup:
npm install -D @playwright/test
npx playwright install --with-deps chromium
playwright.config.tsAdapt to the detected framework:
Next.js:
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './e2e',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: [
['html', { open: 'never' }],
['list'],
],
use: {
baseURL: 'http://localhost:3000',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
},
projects: [
{ name: "chromium", use: { ...devices['Desktop Chrome'] } },
{ name: "firefox", use: { ...devices['Desktop Firefox'] } },
{ name: "webkit", use: { ...devices['Desktop Safari'] } },
],
webServer: {
command: 'npm run dev',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
});
React (Vite):
baseURL to http://localhost:5173webServer.command to npm run devVue/Nuxt:
baseURL to http://localhost:3000webServer.command to npm run devAngular:
baseURL to http://localhost:4200webServer.command to npm run startNo framework detected:
webServer blockbaseURL from user input or leave as placeholdere2e/
├── fixtures/
│ └── index.ts # Custom fixtures
├── pages/
│ └── .gitkeep # Page object models
├── test-data/
│ └── .gitkeep # Test data files
└── example.spec.ts # First example test
import { test, expect } from '@playwright/test';
test.describe('Homepage', () => {
test('should load successfully', async ({ page }) => {
await page.goto('/');
await expect(page).toHaveTitle(/.+/);
});
test('should have visible navigation', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('navigation')).toBeVisible();
});
});
If .github/workflows/ exists, create playwright.yml: