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

Playwright Multi-Project Configuration (Projects)

Playwright Multi-Project Configuration (Projects)

This chapter explains how to run tests on multiple browsers and devices through Projects configuration, as well as the use of project dependencies.


What is a Project

In Playwright, a Project represents a group of tests that run under a specific browser and device configuration.

By configuring multiple Projects, you can run the same set of test code on Chromium, Firefox, WebKit, and mobile devices.


Basic multi-browser configuration

Example

// File path: playwright.config.ts

import

{

defineConfig

,

devices

}

from

'@playwright/test'

;

export

default

defineConfig

(

{

  projects

:

[

   

{

      name

:

'chromium'

,

      use

:

{

...

devices

[

'Desktop Chrome'

]

}

,

   

}

,

   

{

      name

:

'firefox'

,

      use

:

{

...

devices

[

'Desktop Firefox'

]

}

,

   

}

,

   

{

      name

:

'webkit'

,

      use

:

{

...

devices

[

'Desktop Safari'

]

}

,

   

}

,

 

]

,

}

)

;

Run tests for a specific browser:



# Only runs on Chromium

npx playwright test --project=chromium



# Runs on Chromium and Firefox

npx playwright test --project=chromium --project=firefox


devices device presets

Playwright has many built-in device presets (from @playwright/test 's devices), simulating real devices' viewport, User-Agent, pixel ratio, and more.

Example

import

{

defineConfig

,

devices

}

from

'@playwright/test'

;

export

default

defineConfig

(

{

  projects

:

[

   

// Desktop

   

{

      name

:

'desktop-chrome'

,

      use

:

{

...

devices

[

'Desktop Chrome'

]

}

,

   

}

,

   

// Mobile

   

{

      name

:

'mobile-iphone'

,

      use

:

{

...

devices

[

'iPhone 15'

]

}

,

   

}

,

   

{

      name

:

'mobile-pixel'

,

      use

:

{

...

devices

[

'Pixel 7'

]

}

,

   

}

,

   

// Tablet

   

{

      name

:

'tablet-ipad'

,

      use

:

{

...

devices

[

'iPad Pro'

]

}

,

   

}

,

 

]

,

}

)

;

Common device presets:

Preset name Device type Viewport size
Desktop Chrome Desktop 1280×720
Desktop Firefox Desktop 1280×720
Desktop Safari Desktop 1280×720
iPhone 15 Phone 393×852
Pixel 7 Phone 412×915
iPad Pro Tablet 1024×1366

Custom project configuration

You can, in a Project's use override the global configuration:

Example

export

default

defineConfig

(

{

  use

:

{

    baseURL

:

'http://localhost:3000'

,

    locale

:

'zh-CN'

,

    timezone

:

'Asia/Shanghai'

,

 

}

,

  projects

:

[

   

{

      name

:

'chromium-desktop'

,

      use

:

{

        ...

devices

[

'Desktop Chrome'

]

,

       

// Inherit the global locale and timezone

     

}

,

   

}

,

   

{

      name

:

'chromium-mobile'

,

      use

:

{

        ...

devices

[

'Pixel 7'

]

,

       

// Override to an English environment

        locale

:

'en-US'

,

        timezone

:

'America/New_York'

,

     

}

,

   

}

,

   

// Test dark mode

   

{

      name

:

'chromium-dark'

,

      use

:

{

        ...

devices

[

'Desktop Chrome'

]

,

        colorScheme

:

'dark'

,

     

}

,

   

}

,

   

// Test light mode

   

{

      name

:

'chromium-light'

,

      use

:

{

        ...

devices

[

'Desktop Chrome'

]

,

        colorScheme

:

'light'

,

     

}

,

   

}

,

 

]

,

}

)

;


Project dependencies (dependencies)

One project can depend on another, ensuring the depended-on project runs first.

The most common use is for a Setup project (authentication) to run first, with other projects depending on it.

Example

export

default

defineConfig

(

{

  projects

:

[

   

// Setup project -- generate authentication state

   

{

      name

:

'setup'

,

      testMatch

:

/auth.setup.ts/

,

   

}

,

   

// Test projects depend on setup, ensuring authentication state is generated first

   

{

      name

:

'chromium'

,

      use

:

{

        ...

devices

[

'Desktop Chrome'

]

,

        storageState

:

'playwright/.auth/user.json'

,

     

}

,

      dependencies

:

[

'setup'

]

,

   

}

,

   

{

      name

:

'firefox'

,

      use

:

{

        ...

devices

[

'Desktop Firefox'

]

,

        storageState

:

'playwright/.auth/user.json'

,

     

}

,

      dependencies

:

[

'setup'

]

,

   

}

,

 

]

,

}

)

;

setup The project runs only once, and all projects that depend on it wait for it to finish before starting.


Sharing configuration via testProject

If you want multiple projects to share configuration without duplication, you can extract a base project:

Example

// File path: playwright.config.ts

import

{

defineConfig

,

devices

}

from

'@playwright/test'

;

const

desktopProject

=

{

  use

:

{

    ...

devices

[

'Desktop Chrome'

]

,

    viewport

:

{

width

:

1920

,

height

:

1080

}

,

    locale

:

'zh-CN'

,

 

}

,

}

as

const

;

export

default

defineConfig

(

{

  projects

:

[

   

{

      name

:

'chromium'

,

      ...

desktopProject

,

 

// Inherit the configuration of desktopProject

      use

:

{

        ...

desktopProject

.

use

,

 

// Inherit first

        browserName

:

'chromium'

,

// Then override

     

}

,

   

}

,

   

{

      name

:

'firefox'

,

      use

:

{

        ...

devices

[

'Desktop Firefox'

]

,

       

// Override viewport and locale

        viewport

:

{

width

:

1920

,

height

:

1080

}

,

        locale

:

'zh-CN'

,

     

}

,

   

}

,

 

]

,

}

)

;