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

Playwright Installation

Playwright Installation

Playwright is a Node.js library, so Node.js must be installed locally; if you already have it installed, you can skip this.

1. Install Node.js

The official Playwright recommendation is to use Node.js.

npm -v

Seeing a version number is sufficient.

For more installation details, refer to:https://www.runoob.com/nodejs/nodejs-install-setup.html

2. Create a project directory

If Node.js is installed successfully, we can begin installing and using Playwright.

Run in the command line:

mkdir runoob-playwright-demo

cd runoob-playwright-demo

3. Initialize and install Playwright

Run:

npm init playwright@latest

During installation, you will be prompted:

After installation, your project directory will look roughly like this:

playwright-demo/

├─ tests/                # 示例测试用例

├─ playwright.config.js  # 配置文件

├─ package.json

└─ node_modules/

4. Initialize with yarn



yarn create playwright

5. Initialize with pnpm



pnpm create playwright

The three package managers produce identical initialization results; just choose the one you are familiar with.


Manual Installation

If you already have a project and want to add Playwright manually, you can do it in two steps.

Step 1: Install the npm package

# 安装 Playwright Test 作为开发依赖

npm i -D @playwright/test



# If you only need a browser automation library (no test runner)

npm i playwright

Step 2: Install browsers



# Install all browsers (Chromium, Firefox, WebKit)

npx playwright install



# Or install specific browsers only

npx playwright install chromium

npx playwright install firefox

npx playwright install webkit

Browser binaries are downloaded by default to the operating system cache directory (such as macOS's ~/Library/Caches/ms-playwright/), which does not take up space in your project.


Installed browser descriptions

Playwright installs test versions of browsers, not the browsers you use daily:

Name in Playwright Corresponding browser Description
Chromium The open-source basis of Google Chrome The core engine of Chrome, with nearly identical behavior
Firefox Mozilla Firefox The official test build of Firefox
WebKit The engine of Apple Safari The underlying rendering engine of Safari

These test versions have minor differences from the browsers users use daily, but behave consistently in the vast majority of test scenarios.


Verify the installation

After installation, run the following command to verify success:



# View installed Playwright versions and browsers

npx playwright --version

The expected output is roughly as follows:



Version 1.52.0

Run a sample test to verify everything works:



npx playwright test

If you see test run output (including passing and failing tests), the installation was successful.


Common installation issues

Slow browser downloads

Browser binaries are large (about 400-500 MB), and download speed depends on your network environment.

You can use a domestic mirror by setting environment variables:



# Set download source to domestic mirror

export PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright/

npx playwright install

Missing system dependencies on Linux

Running Playwright on Linux requires installing additional system libraries.



# Install system dependencies required by Playwright

npx playwright install-deps



# If only specific browser dependencies are needed

npx playwright install-deps chromium

When using Playwright in a Docker container, please use the official Microsoft Docker image (mcr.microsoft.com/playwright), which comes with all dependencies pre-installed.


First script: Hello World

Create a new test.js file and write the following content:

Example

// test.js

const

{

chromium

}

=

require

(

'playwright'

)

;

(

async

(

)

=>

{

 

// 1. Launch the browser

 

const

browser

=

await chromium.

launch

(

{

headless

:

false

}

)

;

// Set to false to see the browser

 

const

page

=

await browser.

newPage

(

)

;

 

// 2. Open a web page

  await page.

goto

(

'https://www.baidu.com'

)

;

 

// 3. Take and save a screenshot

  await page.

screenshot

(

{

path

:

'baidu.png'

}

)

;

 

// 4. Close the browser

  await browser.

close

(

)

;

  console.

log

(

"Screenshot saved to example.png"

)

;

}

)

(

)

;

Run:



node test.js

The above is a simple Playwright example that can take a screenshot of Baidu's homepage.