Menu

Set Playwright's baseURL from a dynamic server port
πŸ“°

Set Playwright's baseURL from a dynamic server port

DEV Community: viteΒ·Vitaliy PotapovΒ·14 days ago
#dS7x7ZKR
#devtovite#devto#dev#port#playwright
Long article (795 words) β€” no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.