Playwright Inspector
Playwright Inspector
The Playwright Inspector is a window where you run a test one step at a time.
You see the browser, the current line of code and the element each locator finds.
The examples use the login test from the Debugging chapter.
Start the Inspector with --debug
Run a test with --debug to step through it in the Playwright Inspector:
Example
npx playwright test tests/login.spec.js --debug
Playwright opens a visible browser and the Inspector window, and pauses before the first action.
In the Inspector you can:
- Click Step over (F10) to run one action at a time
- Click Resume (F8) to run until the end, or until the next pause
- See the locator of the next action highlighted in the browser
- Use Pick locator to click an element and get its locator
- Read the Log tab to see what Playwright waits for
--debug also turns off the test timeout and runs the tests one at a time (with one worker).
So the test does not fail while you look around.
To debug one test, add its line number to the file name:
Example
npx playwright test tests/login.spec.js:4 --debug
Note: --debug is a shortcut for the PWDEBUG=1 environment variable (a setting you give a program from the terminal).
In a bash terminal you can also write PWDEBUG=1 npx playwright test.
In PowerShell, run $env:PWDEBUG=1 first, and then npx playwright test.
Pause a Test with page.pause()
Put await page.pause() in a test to stop at that line:
Example
tests/login.spec.js
test('student can log in', async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/login.html');
await page.getByLabel('Username').fill('student');
await page.pause();
await page.getByLabel('Password').fill('w3schools');
});
Example
npx playwright test tests/login.spec.js --headed
The test runs normally until it reaches page.pause().
Then the Inspector opens.
You can look at the page, and then run the rest of the test step by step.
page.pause() only works in a visible browser, so run with --headed or --debug.
Remember to remove it when you are done.
Watch the Browser with slowMo
Run with --headed to see the browser while the test runs:
Example
npx playwright test --headed
Tests are often too fast to follow.
The slowMo launch option (a setting for starting the browser) waits the given number of milliseconds after every action:
Example
playwright.config.js
export default defineConfig({
use: {
headless: false,
launchOptions: {
slowMo: 500,
},
},
});
Now every click and fill takes half a second, and you can see each step happen.
Do not keep slowMo in your normal config: it makes every test slower.
Debug in VS Code
The Playwright Test for VS Code extension adds a Testing panel to Visual Studio Code.
With the extension you can:
- Run or debug a single test with the play button next to it
- Set a breakpoint (a line where the test stops) by clicking left of a line number, then right-click the play button and choose Debug Test
- Check Show browser to watch the test in a visible browser
- Hover a locator in your code to highlight it in the open browser
When the test stops at a breakpoint, you can read the values of all variables in the VS Code debugger.