The problem
When testing a web application with Playwright, I usually set two configuration options:
webServer to start the dev server automatically
baseURL so all tests can use short relative URLs for navigation
To keep these two settings in sync, I used a single port variable that takes its value from process.env.PORT and defaults to 3000 . I put that port in both baseURL and the command that started the dev server.
Example config:
playwright.config.ts
import { defineConfig } from '@playwright/test' ;
const port = process . env . PORT || '3000' ; const baseURL = `http://localhost:${port}` ;
export default defineConfig ( { use : { baseURL } , webServer : { command : `npm run dev -- --port ${port}` , url : baseURL , } , } ) ;
Enter fullscreen mode
Exit fullscreen mode
This works fine when I run one instance of the app at a time.β¦