Skip to contentPlayForge

What is baseURL in playwright.config and why use it?

Written by Jatin Thacker, Automation Architect · Updated

baseURL is a setting under use in playwright.config.js that gives every page in the test a starting address, so page.goto('/catalog.html') becomes a full URL. You use it so tests name only the path, and the same suite can run against a different environment by changing one line.

This answer is one piece of Chapter 16: The Test Runner.

  • Test Structure & Lifecycle
  • playwright.config.ts Complete Reference
  • Test Organization at Scale
  • Tags, Grep, and the Suite You Actually Run
  • What Runs Where, and What Serial Costs
  • Practice and interview questions: Assertions & Test Runner

Without baseURL, every page.goto() carries the full address of the site. When the team moves from a staging server to production, every test file has to change. With baseURL set once in the config, tests carry only the path, and switching environments is one edit or one environment variable.

baseURL is a context option, so it belongs inside the use block. Placed at the top level of defineConfig, it is not applied, and every relative goto fails.

Playwright builds the full address with the URL() constructor. That applies to page.goto(), page.route(), page.waitForURL(), page.waitForRequest() and page.waitForResponse(). A leading slash matters: with a base of http://localhost:3000/foo/, ./bar.html keeps the /foo/ part, while /bar.html replaces it.

In the example, the config sets baseURL to the practice app. The test calls page.goto('/catalog.html?mock=static&seed=42&autologin=1'). toHaveURL() then proves Playwright resolved it against the base, and the heading check proves the right page loaded.

playwright.config.js(https://practice.learnplayforge.com)
import { defineConfig, devices } from '@playwright/test'; export default defineConfig({ testDir: '.', reporter: 'list', use: { baseURL: 'https://practice.learnplayforge.com', }, projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], }); // base-url.spec.js import { test, expect } from '@playwright/test'; test('a relative path is resolved against baseURL', async ({ page }) => { await page.goto('/catalog.html?mock=static&seed=42&autologin=1'); await expect(page).toHaveURL('https://practice.learnplayforge.com/catalog.html?mock=static&seed=42&autologin=1'); await expect(page.locator('#grid-heading')).toHaveText('All products'); });
Output
1 passed (1.6s)
Test yourself
Your config has use: { baseURL: 'https://practice.learnplayforge.com' }. A test calls page.goto('/login.html'). Which address does the browser open?

Practised in the full course: the drill Config at the wrong scope.

See the chapter

AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.

163 lessons · 226 drills · start free

← All questions

Training a team? Talk to us.