2021-01-01 15:17:27 -08:00
---
id: debug
title: "Debugging tools"
---
2020-12-30 18:04:51 -08:00
Playwright scripts work with existing debugging tools, like Node.js debuggers
and browser developer tools. Playwright also introduces new debugging features
for browser automation.
2021-01-01 15:17:27 -08:00
<!-- TOC -->
2020-12-30 18:04:51 -08:00
2021-02-21 18:36:39 -08:00
## Playwright Inspector
[Playwright Inspector ](./inspector.md ) is a GUI tool that helps authoring and debugging Playwright scripts. That's our default recommended tool for scripts troubleshooting.
2021-02-22 16:37:16 -08:00
< img width = "712" alt = "Playwright Inspector" src = "https://user-images.githubusercontent.com/883973/108614092-8c478a80-73ac-11eb-9597-67dfce110e00.png" > < / img >
2021-02-21 18:36:39 -08:00
2021-04-01 20:13:50 +02:00
## Run in headed mode
2020-12-30 18:04:51 -08:00
Playwright runs browsers in headless mode by default. To change this behavior,
2021-01-20 16:06:26 -08:00
use `headless: false` as a launch option. You can also use the [`option: slowMo` ] option
2020-12-30 18:04:51 -08:00
to slow down execution and follow along while debugging.
```js
await chromium.launch({ headless: false, slowMo: 100 }); // or firefox, webkit
```
2021-03-01 09:18:44 -08:00
```java
chromium.launch(new BrowserType.LaunchOptions() // or firefox, webkit
2021-03-05 13:50:34 -08:00
.setHeadless(false)
.setSlowMo(100));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
await chromium.launch(headless=False, slow_mo=100) # or firefox, webkit
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
chromium.launch(headless=False, slow_mo=100) # or firefox, webkit
```
2020-12-30 18:04:51 -08:00
## Browser Developer Tools
You can use browser developer tools in Chromium, Firefox and WebKit while running
2021-04-20 15:58:34 -07:00
a Playwright script in headed mode. Developer tools help to:
2020-12-30 18:04:51 -08:00
* Inspect the DOM tree and **find element selectors**
* **See console logs** during execution (or learn how to [read logs via API ](./verification.md#console-logs ))
* Check **network activity** and other developer tools features
2021-01-03 08:47:29 -08:00
< a href = "https://user-images.githubusercontent.com/284612/77234134-5f21a500-6b69-11ea-92ec-1c146e1333ec.png" > < img src = "https://user-images.githubusercontent.com/284612/77234134-5f21a500-6b69-11ea-92ec-1c146e1333ec.png" width = "500" alt = "Chromium Developer Tools" > < / img > < / a >
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
Using a [`method: Page.pause` ] method is an easy way to pause the Playwright script execution
and inspect the page in Developer tools. It will also open [Playwright Inspector ](./inspector.md ) to help with debugging.
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
:::note
**For WebKit**: launching WebKit Inspector during the execution will
prevent the Playwright script from executing any further.
:::
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
:::note
**For Chromium**: you can also open developer tools through a launch option.
2020-12-30 18:04:51 -08:00
```js
await chromium.launch({ devtools: true });
```
2021-03-01 09:18:44 -08:00
```java
2021-03-05 13:50:34 -08:00
chromium.launch(new BrowserType.LaunchOptions().setDevtools(true));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
await chromium.launch(devtools=True)
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
chromium.launch(devtools=True)
```
2021-04-20 15:58:34 -07:00
:::
2021-01-08 17:39:33 +01:00
2020-12-30 18:04:51 -08:00
## Run in Debug Mode
2021-04-20 15:58:34 -07:00
Set the `PWDEBUG` environment variable to run your scripts in debug mode. Using `PWDEBUG=1` will open [Playwright Inspector ](./inspector.md ).
Using `PWDEBUG=console` will configure the browser for debugging in Developer tools console:
* **Runs headed**: Browsers always launch in headed mode
* **Disables timeout**: Sets default timeout to 0 (= no timeout)
* **Console helper**: Configures a `playwright` object in the browser to generate and highlight
[Playwright selectors ](./selectors.md ). This can be used to verify text or
composite selectors.
2020-12-30 18:04:51 -08:00
2021-01-14 18:19:02 -08:00
```sh js
2020-12-30 18:04:51 -08:00
# Linux/macOS
2021-04-20 15:58:34 -07:00
$ PWDEBUG=console npm run test
2020-12-30 18:04:51 -08:00
# Windows
2021-04-20 15:58:34 -07:00
$ set PWDEBUG=console
2020-12-30 18:04:51 -08:00
$ npm run test
```
2021-03-01 09:18:44 -08:00
```sh java
# Linux/macOS
2021-04-20 15:58:34 -07:00
$ PWDEBUG=console mvn test
2021-03-01 09:18:44 -08:00
# Windows
2021-04-20 15:58:34 -07:00
$ set PWDEBUG=console
2021-03-01 09:18:44 -08:00
$ mvn test
```
2021-01-14 18:19:02 -08:00
```sh python
# Linux/macOS
2021-04-20 15:58:34 -07:00
$ PWDEBUG=console pytest -s
2021-01-14 18:19:02 -08:00
# Windows
2021-04-20 15:58:34 -07:00
$ set PWDEBUG=console
2021-01-14 18:19:02 -08:00
$ pytest -s
```
2021-04-20 15:58:34 -07:00
## Selectors in Developer Tools Console
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
When running in Debug Mode with `PWDEBUG=console` , a `playwright` object is available in Developer tools console.
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
1. Run with `PWDEBUG=console`
2020-12-30 18:04:51 -08:00
1. Setup a breakpoint to pause the execution
1. Open the console panel in browser developer tools
1. Use the `playwright` API
* `playwright.$(selector)` : Highlight the first occurrence of the selector. This reflects
how `page.$` would see the page.
* `playwright.$$(selector)` : Highlight all occurrences of the selector. This reflects
how `page.$$` would see the page.
* `playwright.inspect(selector)` : Inspect the selector in the Elements panel.
* `playwright.clear()` : Clear existing highlights.
2021-04-20 15:58:34 -07:00
* `playwright.selector(element)` : Generate a selector that points to the element.
2020-12-30 18:04:51 -08:00
2021-01-03 08:47:29 -08:00
< a href = "https://user-images.githubusercontent.com/284612/86857345-299abc00-c073-11ea-9e31-02923a9f0d4b.png" > < img src = "https://user-images.githubusercontent.com/284612/86857345-299abc00-c073-11ea-9e31-02923a9f0d4b.png" width = "500" alt = "Highlight selectors" > < / img > < / a >
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
## Visual Studio Code debugger (Node.js)
The VS Code debugger can be used to pause and resume execution of Playwright
scripts with breakpoints. The debugger can be configured in two ways.
### Use launch config
Setup [`launch.json` configuration ](https://code.visualstudio.com/docs/nodejs/nodejs-debugging )
for your Node.js project. Once configured launch the scripts with F5 and use
breakpoints.
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
### Use the JavaScript Debug Terminal
2020-12-30 18:04:51 -08:00
2021-04-20 15:58:34 -07:00
1. Open [JavaScript Debug Terminal ](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_javascript-debug-terminal )
1. Set a breakpoint in VS Code
* Use the `debugger` keyword or set a breakpoint in the VS Code UI
1. Run your Node.js script from the terminal
2020-12-30 18:04:51 -08:00
## Verbose API logs
Playwright supports verbose logging with the `DEBUG` environment variable.
2021-01-14 18:19:02 -08:00
```sh js
2020-12-30 18:04:51 -08:00
# Linux/macOS
$ DEBUG=pw:api npm run test
# Windows
$ set DEBUG=pw:api
$ npm run test
```
2021-01-14 18:19:02 -08:00
2021-03-01 09:18:44 -08:00
```sh java
# Linux/macOS
$ DEBUG=pw:api mvn test
# Windows
$ set DEBUG=pw:api
$ mvn test
```
2021-01-14 18:19:02 -08:00
```sh python
# Linux/macOS
$ DEBUG=pw:api pytest -s
# Windows
$ set DEBUG=pw:api
$ pytest -s
```