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

Playwright VS Code Development Environment Setup

Playwright VS Code Development Environment Setup

For Playwright development, it is recommended to use VS Code, because it has strong support for JavaScript/TypeScript, debuggers, and extensions.

Install VS Code

VS Code related content reference:https://www.runoob.com/vscode/vscode-tutorial.html


Essential extensions

In the Extensions panel of VS Code, search for and install the following extensions:

Playwright Test for VSCode

JavaScript / TypeScript support(built into VS Code; usually no need to install separately)

Recommended (optional):


Initialize a Playwright project

If you have not initialized a project yet, run:

npm init playwright@lates

The VS Code extension automatically detects the playwright.config.ts/jsin the project and enables the Test view.


VS Code settings

Open VS Code settings (shortcut Ctrl+, or Cmd+,), and confirm the following settings:


Search format on save → check Editor: Format On Save

-

Default terminal shell(choose according to your own system)
Recommended for Windows PowerShell or Git Bash
macOS/Linux defaults are fine


Debug configuration (launch.json)

Click on the left side of VS Code Run and Debug → Create a configuration, select Node.js, generating .vscode/launch.json, add a Playwright debug configuration, for example:

{

  "version": "0.2.0",

  "configurations": [

    {

      "name": "Debug Playwright Test",

      "type": "node",

      "request": "launch",

      "program": "${workspaceFolder}/node_modules/.bin/playwright",

      "args": ["test", "--project=chromium", "--headed"],

      "console": "integratedTerminal",

      "internalConsoleOptions": "neverOpen"

    }

  ]

}

This way, in VS Code you can press F5 to start debugging; Playwright will run in headed mode for easy observation.


Using the Playwright extension panel

After installing the Playwright Test for VSCode extension, a "Playwright" icon will appear on the left side:


Common Commands

Run in the VS Code terminal:

npx playwright test           # 运行全部测试

npx playwright test login.spec.js # 运行指定文件

npx playwright codegen        # 启动录制工具(自动生成脚本)

npx playwright show-report    # 打开测试报告


Testing Sidebar

After installing the extension, a Testing icon (laboratory beaker icon) appears on the left side of VS Code.

Click to enter the Testing Sidebar, where you can see the list of all test files.

Here you can:


Debug Mode

The VS Code extension lets you debug Playwright tests just like ordinary code:

Set breakpoints

Click to the left of a code line number to add a red breakpoint.

Right-click beside the breakpoint and select Debug Test; the test will pause at the breakpoint.

Debugging features


Live Debugging

Live Debugging is a unique feature of the VS Code extension.

When running tests with the Show Browser option enabled, clicking a Locator in the code highlights the corresponding page element in the browser window.

If a Locator matches multiple elements, Playwright highlights all matches at the same time.

You can edit a Locator directly in the code, and the browser shows the updated match results in real time.


Pick Locator

Click the Pick Locator button at the bottom of the Testing Sidebar.

Click the target element in the browser that pops up, and the best locator will appear in VS Code's Pick Locator box.

Press Enter to copy to the clipboard, then paste it into your test code.

Playwright automatically chooses the most stable, most semantic locator strategy (priority: role > text > testid).


Record New Test

Click the Record new button in the Testing Sidebar.

Playwright opens a browser window, and all your actions in it (navigation, clicks, typing) are recorded.

You can also generate assertions through the toolbar:

After recording, close the browser, and the code is automatically generated in test-1.spec.ts file.


Record at Cursor

In existing test code, place the cursor where you want to insert the action, then click Record at cursor.

Playwright records subsequent actions in the browser and inserts the generated code at the cursor position.

This feature is ideal for adding steps to existing tests.