Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Configuration Explained

Playwright Configuration Explained

This chapter introduces in detail the playwright.config.ts various configurations in it, allowing you to flexibly adjust test behavior based on project needs.


Configuration file overview

playwright.config.ts is Playwright's central configuration file; all test run parameters are defined here.

Example

// File path: playwright.config.ts

import

{

defineConfig

,

devices

}

from

'@playwright/test'

;

export

default

defineConfig

(

{

 

// ===== Basic configuration =====

  testDir

:

'./tests'

,

  fullyParallel

:

true

,

  forbidOnly

:

!!

process.

env

.

CI

,

  retries

:

process.

env

.

CI

?

2

:

0

,

  workers

:

process.

env

.

CI

?

1

:

undefined

,

  reporter

:

'html'

,

  timeout

:

30000

,

 

// ===== Global shared configuration =====

  use

:

{

    baseURL

:

'http://localhost:3000'

,

    trace

:

'on-first-retry'

,

    screenshot

:

'only-on-failure'

,

    video

:

'retain-on-failure'

,

 

}

,

 

// ===== Multi-browser projects =====

  projects

:

[

   

{

name

:

'chromium'

,

use

:

{

...

devices

[

'Desktop Chrome'

]

}

}

,

   

{

name

:

'firefox'

,

 use

:

{

...

devices

[

'Desktop Firefox'

]

}

}

,

   

{

name

:

'webkit'

,

  use

:

{

...

devices

[

'Desktop Safari'

]

}

}

,

 

]

,

 

// ===== Web server =====

  webServer

:

{

    command

:

'npm run start'

,

    url

:

'http://localhost:3000'

,

    reuseExistingServer

:

!

process.

env

.

CI

,

 

}

,

}

)

;


Basic configuration items

Configuration Item Type Default value Description
testDir string tests Directory where test files are located
fullyParallel boolean false Whether to run all tests fully in parallel (including tests within the same file)
workers number CPU core count / 2 Number of workers executing in parallel
retries number 0 Number of retries for failed tests
timeout number 30000 Total timeout for each test (in milliseconds)
reporter string/array list Test reporter type
forbidOnly boolean false Fail if test.only() is detected on CI
testMatch string/RegExp *.spec.(ts\ js)
testIgnore string/RegExp - Ignored test file patterns

Configuration example

Example

export

default

defineConfig

(

{

 

// Find tests in the e2e directory

  testDir

:

'./e2e'

,

 

// All tests run fully in parallel

  fullyParallel

:

true

,

 

// Automatically determine the number of workers

  workers

:

undefined

,

 

// No retries on failure (local) / 2 retries (CI)

  retries

:

process.

env

.

CI

?

2

:

0

,

 

// Use both the command-line and HTML reporters

  reporter

:

[

   

[

'list'

]

,

   

[

'html'

,

{

open

:

'never'

}

]

,

 

]

,

}

)

;


Common configuration in use

use configurations apply to all tests and projects:

Configuration Item Type Description
baseURL string Base URL; relative paths can be used in tests (e.g. await page.goto('/login'))
viewport {width, height} Browser viewport size, default { width: 1280, height: 720 }
locale string Browser language, e.g. 'zh-CN'、'en-US'
timezone string Timezone, e.g. 'Asia/Shanghai'
colorScheme string Color scheme:'light'|'dark'|'no-preference'
geolocation {latitude, longitude} Simulate geolocation
permissions string[] Granted browser permissions (e.g. ['geolocation'])
userAgent string Custom User-Agent string
trace string Trace collection policy:'on'|'off'|'on-first-retry'
screenshot string Screenshot policy:'on'|'off'|'only-on-failure'
video string Video recording policy:'on'|'off'|'retain-on-failure'
storageState string Authentication state file path
testIdAttribute string Custom testId attribute name, default 'data-testid'
actionTimeout number Timeout for each operation (click, fill)
navigationTimeout number Timeout for each navigation

webServer configuration

webServer configuration makes Playwright automatically start your development server before running tests:

Example

export

default

defineConfig

(

{

  webServer

:

{

   

// Start command

    command

:

'npm run dev'

,

   

// Wait until this URL is accessible before running tests

    url

:

'http://localhost:5173'

,

   

// Timeout waiting time (in milliseconds)

    timeout

:

120000

,

   

// Whether to reuse an already-running server (reuse locally, restart on CI)

    reuseExistingServer

:

!

process.

env

.

CI

,

 

}

,

}

)

;


globalSetup / globalTeardown global hooks

Executed once before/after all tests run; suitable for operations such as database initialization.

Example

export

default

defineConfig

(

{

  globalSetup

:

'./global-setup.ts'

,

  globalTeardown

:

'./global-teardown.ts'

,

}

)

;

Example

// File path: global-setup.ts

import

{

FullConfig

}

from

'@playwright/test'

;

async

function

globalSetup

(

config

:

FullConfig

)

{

 

// Execute before all tests run

  console.

log

(

'Starting global setup...'

)

;

 

// For example: initialize the test database, create test users, etc.

}

export

default

globalSetup

;