In this article, we will discuss in detail how we can debug playwright scripts and locators using the Inspector.

1) To launch the test script in debug mode, we will have to use the command:

1
PWDEBUG=1 npx playwright test tests/2-checkBox.spec.ts

tests/2-checkBox.spec.ts is the path+file name of the test script from the project root.
 
2) After executing the above command, two windows will be opened – The Browser and the Inspector app with test steps.

test script opened with playwright inspector
 
3) To execute the test line by line, we can click the Step over button. This will only execute the current test step and stop.

step over in playwright inspector
 
4) If you don’t want to go through the test line by line, in that case, you can use the Resume and Pause functionalities. Clicking the resume button will start the test case execution until the pause button is clicked.

Resume button in playwright inspector

pause button in playwright inspector
 
5) When stopped on an input action such as check, the exact point Playwright is about to check is highlighted with the large red dot on the inspected page:

red highlight for playwright action
 
6) You can further drill down on how each action is performed, by looking into the logs. For example in the below image, for the test step await expect(page.locator(‘#checkboxes’)).toBeVisible(), three actionability checks are performed by playwright.

playwright inspector logs
 
7) You can also find selectors using the inspector app, by using the Explore functionality. Click the Explore button to hover over elements in the screen and click them to automatically generate selectors for those elements.

explore in playwright inspector for locating selectors
 
8) You can also use the browser dev tools to locate the selectors using the following playwright methods.

a) playwright.$(selector) Query Playwright selector, using the actual Playwright query engine.

selector using playwright query engine

b) playwright.$$(selector) Same as playwright.$, but returns all matching elements.

selector using playwright.$$

c) playwright.inspect(selector) Reveal element in the Elements panel (if DevTools of the respective browser supports it). It automatically redirects you to the exact line of code for that element.

selector using playwright.inspect

d) playwright.locator(selector) Query Playwright element using the actual Playwright query engine.

selector using playwright.locator

e) playwright.selector(element) Generates selector for the given element. $0 returns the most recently selected element or JavaScript object, $1 returns the second most recently selected one, and so on.

selector using playwright.selector

Do check out 🙂

Github: https://github.com/alapanme/Playwright-Automation