113 / 139 · 13 Browser Automation with Playwright · Production Framework← prev⊞ allnext →☰ Read as one page
13.3Configuration Management
Multi-Environment Setup
// playwright.config.ts
import { defineConfig } from '@playwright/test';
const env = process.env.TEST_ENV || 'staging';
const envConfig = {
local: { baseURL: 'http://localhost:3000' },
staging: { baseURL: 'https://staging.example.com' },
production: { baseURL: 'https://example.com' },
};
export default defineConfig({
testDir: './tests',
timeout: 60_000,
retries: env === 'production' ? 0 : 2,
workers: env === 'local' ? undefined : 4,
use: {
baseURL: envConfig[env].baseURL,
screenshot: 'only-on-failure',
trace: 'on-first-retry',
video: 'on-first-retry',
},
projects: [
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
dependencies: ['setup'],
use: { browserName: 'chromium', storageState: '.auth/user.json' },
},
{
name: 'firefox',
dependencies: ['setup'],
use: { browserName: 'firefox', storageState: '.auth/user.json' },
},
],
});
Running Against Different Environments
# Default (staging)
npx playwright test
# Local development
TEST_ENV=local npx playwright test
# Production smoke tests
TEST_ENV=production npx playwright test --grep @smoke