playwright-cli eval command to inspect element attributes
The playwright-cli eval command evaluates JavaScript against an element and returns the result. Use it to inspect element properties and attributes that don't appear in the snapshot output. The syntax is: playwright-cli eval "<javascript expression>" <element-id>. The element parameter in the expression is named 'el'.
Get element id with playwright-cli eval
To get an element's id attribute, use: playwright-cli eval "el => el.id" <element-id>
Get element CSS classes with playwright-cli eval
To get all CSS classes on an element, use: playwright-cli eval "el => el.className" <element-id>
Get specific attribute with playwright-cli eval
To get a specific attribute value, use: playwright-cli eval "el => el.getAttribute('attribute-name')" <element-id>. For example, to get a data-testid attribute: playwright-cli eval "el => el.getAttribute('data-testid')" <element-id>
Get computed CSS style with playwright-cli eval
To get a computed CSS property value, use: playwright-cli eval "el => getComputedStyle(el).propertyName" <element-id>. For example, to get the computed display property: playwright-cli eval "el => getComputedStyle(el).display" <element-id>
playwright-cli run-code syntax and function expression
The run-code command executes arbitrary Playwright code. The code must be a single function expression, which is wrapped in parentheses and evaluated. The function receives a page object as a parameter. Code can be passed as a string argument or loaded from a file using --filename=./path. Import/export/require syntax is not supported.
Example: playwright-cli run-code geolocation with latitude and longitude
playwright-cli run-code "async page => {\n await page.context().grantPermissions(['geolocation']);\n await page.context().setGeolocation({ latitude: 37.7749, longitude: -122.4194 });\n}"
Example: playwright-cli run-code multiple permissions
playwright-cli run-code "async page => {\n await page.context().grantPermissions([\n 'geolocation',\n 'notifications',\n 'camera',\n 'microphone'\n ]);\n}"
Example: playwright-cli run-code emulate dark color scheme
playwright-cli run-code "async page => {\n await page.emulateMedia({ colorScheme: 'dark' });\n}"
Example: playwright-cli run-code login and save state
playwright-cli run-code "async page => {\n await page.goto('https://example.com/login');\n await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');\n await page.getByRole('textbox', { name: 'Password' }).fill('secret');\n await page.getByRole('button', { name: 'Sign in' }).click();\n await page.waitForURL('**/dashboard');\n await page.context().storageState({ path: 'auth.json' });\n return 'Login successful';\n}"
android-timeout for JavaScript
The timeout parameter is type float. Maximum time in milliseconds. Defaults to 30 seconds. Pass 0 to disable. Default changeable via AndroidDevice.setDefaultTimeout.
Running Playwright tests in JS/TS
To run Playwright tests in JavaScript or TypeScript, use the command 'PLAYWRIGHT_HTML_OPEN=never npx playwright test'. The environment variable PLAYWRIGHT_HTML_OPEN=never prevents the interactive HTML report from opening automatically.
Running Playwright tests with npm script in JS/TS
To run a custom test command defined in package.json in JavaScript or TypeScript, use 'PLAYWRIGHT_HTML_OPEN=never npm run special-test-command'. The environment variable PLAYWRIGHT_HTML_OPEN=never prevents the interactive HTML report from opening.
Running Playwright tests in Python
To run Playwright tests in Python, use the command 'pytest'.
Debugging Playwright tests in JS/TS
To debug a failing Playwright test in JavaScript or TypeScript, run the command 'PLAYWRIGHT_HTML_OPEN=never npx playwright test --debug=cli'. This pauses the test at the start and prints debugging instructions. Run the command in the background and check the output until 'Debugging Instructions' is printed.
Attaching to a Playwright debug session
Once debugging instructions containing a session name are printed, use 'playwright-cli attach <session-name>' to attach to the session and explore the page. For example, 'playwright-cli attach tw-abcdef'.
Debugging Playwright tests in Python
To debug a failing Playwright test in Python, run 'pytest --playwright-debug=cli -s'. This pauses the test at the start and prints debugging instructions. Once instructions containing a session name are printed, use 'playwright-cli attach <session-name>' to attach and explore the page.
Debugging workflow with playwright-cli
When debugging with playwright-cli, keep the test running in the background while you explore and look for a fix. The test is paused at the start, so you should step over or pause at a particular location where the problem is most likely to be. Every action you perform with playwright-cli generates corresponding Playwright code that appears in the output and can be copied directly into the test.