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

Playwright Project Structure

Playwright Project Structure

After successfully initializing a Playwright project, a series of files and folders will be generated in your project directory. This chapter explains the purpose of each part in detail.


Overview of the initialized project structure

Run npm init playwright@latest , the following structure will be generated in the project directory:



your-project/

├── playwright.config.ts      # Playwright 配置文件

├── package.json              # 项目依赖与脚本

├── package-lock.json         # 依赖锁定文件

├── tests/                    # 测试文件目录

│   └── example.spec.ts       # 示例测试文件

├── tests-examples/           # 更多示例测试(可选)

│   └── demo-todo-app.spec.ts

├── .github/                  # GitHub Actions 配置(可选)

│   └── workflows/

│       └── playwright.yml

└── node_modules/             # npm 依赖包


playwright.config.ts -- configuration file

This is the core configuration file of Playwright, controlling all aspects of test execution.

Example

// File path: playwright.config.ts

import

{

defineConfig

,

devices

}

from

'@playwright/test'

;

export

default

defineConfig

(

{

 

// Test file directory, path relative to this configuration file

  testDir

:

'./tests'

,

 

// Run all tests fully in parallel

  fullyParallel

:

true

,

 

// If test.only is left in the source code, the build fails on CI

  forbidOnly

:

!!

process.

env

.

CI

,

 

// Retry 2 times on failure in CI, no retry locally

  retries

:

process.

env

.

CI

?

2

:

0

,

 

// Use a single thread in CI, use the default multi-threading locally

  workers

:

process.

env

.

CI

?

1

:

undefined

,

 

// Use the HTML reporter

  reporter

:

'html'

,

 

// Configuration shared by all tests

  use

:

{

   

// Base URL; just use relative paths in tests

    baseURL

:

'http://localhost:3000'

,

   

// Collect Trace on failure retry

    trace

:

'on-first-retry'

,

 

}

,

 

// Multi-browser/multi-device project configuration

  projects

:

[

   

{

      name

:

'chromium'

,

      use

:

{

...

devices

[

'Desktop Chrome'

]

}

,

   

}

,

   

{

      name

:

'firefox'

,

      use

:

{

...

devices

[

'Desktop Firefox'

]

}

,

   

}

,

   

{

      name

:

'webkit'

,

      use

:

{

...

devices

[

'Desktop Safari'

]

}

,

   

}

,

 

]

,

 

// Start the local development server before tests begin (optional)

 

// webServer: {

 

//   command: 'npm run start',

 

//   url: 'http://localhost:3000',

 

//   reuseExistingServer: !process.env.CI,

 

// },

}

)

;

playwright.config.ts is the core entry point for controlling Playwright behavior; article 15 explains each configuration item in detail.


package.json -- dependencies and scripts

After initialization,package.json Playwright-related content will be added to it:

Example

{

 

"devDependencies"

:

{

   

"@playwright/test"

:

"^1.52.0"

   

// Playwright Test as a development dependency

 

}

,

 

"scripts"

:

{

   

"test"

:

"playwright test"

       

// You can run npm test directly

 

}

}

You can also add more custom scripts:

Example

{

 

"scripts"

:

{

   

"test"

:

"playwright test"

,

   

"test:ui"

:

"playwright test --ui"

,

   

"test:headed"

:

"playwright test --headed"

,

   

"test:chromium"

:

"playwright test --project=chromium"

,

   

"test:debug"

:

"playwright test --debug"

,

   

"codegen"

:

"playwright codegen"

 

}

}


tests/ directory -- test files

tests/ The directory is the default location for test files (specified by the testDir configuration).

The content of the sample test file generated during initialization is as follows:

Example

// File path: tests/example.spec.ts

import

{

test

,

expect

}

from

'@playwright/test'

;

test

(

'has title'

,

async

(

{

page

}

)

=>

{

 

// Navigate to the official Playwright website

  await page.

goto

(

'https://playwright.dev/'

)

;

 

// Assert that the page title contains "Playwright"

  await expect

(

page

)

.

toHaveTitle

(

/Playwright/

)

;

}

)

;

test

(

'get started link'

,

async

(

{

page

}

)

=>

{

 

// Navigate to the official Playwright website

  await page.

goto

(

'https://playwright.dev/'

)

;

 

// Click the "Get started" link

  await page.

getByRole

(

'link'

,

{

name

:

'Get started'

}

)

.

click

(

)

;

 

// Assert that an "Installation" heading appears on the page

  await expect

(

page.

getByRole

(

'heading'

,

{

name

:

'Installation'

}

)

)

.

toBeVisible

(

)

;

}

)

;

Test file naming recommendation: use .spec.ts(TypeScript) or .spec.js(JavaScript) extension.


tests-examples/ directory -- more examples

If you chose to include sample tests during initialization,tests-examples/ the directory will contain a more complete test example:

demo-todo-app.spec.ts It demonstrates a complete test scenario for a real Todo app, including adding tasks, toggling completion status, filtering, and more.

This is a good learning reference; you can run it to see the result:



npx playwright test tests-examples/


.github/workflows/ -- CI configuration

If you chose to add GitHub Actions during initialization,.github/workflows/playwright.yml the file will be generated automatically.

This workflow automatically runs Playwright tests on every code push or PR creation.


Browser storage location

The browser binaries installed by Playwright are not in the project directory; they are stored in the operating system cache directory:

Operating system Browser storage path
macOS
~/Library/Caches/ms-playwright/
Windows
%USERPROFILE%\AppData\Local\ms-playwright
Linux
~/.cache/ms-playwright/

You can also set a custom storage path:



# Set browser installation path

export PLAYWRIGHT_BROWSERS_PATH=/your/custom/path

npx playwright install


node_modules/ -- dependency packages

Playwright's core dependencies @playwright/test installed in node_modules/ it.

The dependency contains Playwright's complete runtime internally, including the protocol layer for communicating with the browser.

In addition to the Node.js packages installed via npm, Playwright also relies on browser binaries (stored in the system cache); both are indispensable.