Catalog / Playwright Testing & Debugging Cheatsheet
Playwright Testing & Debugging Cheatsheet
A comprehensive cheat sheet covering Playwright's testing and debugging features, including selectors, assertions, debugging techniques, and advanced configurations.
Selectors & Locators
Basic Selectors
|
Selects an element containing the text ‘Submit’. |
|
Selects all |
|
Selects an element with the ID ‘id’. |
|
Selects all elements with the class ‘class’. |
|
Selects an |
Combining Selectors
|
Selects |
|
Selects |
|
Selects the second |
Locator Methods
|
Selects the first matching element. |
|
Selects the last matching element. |
|
Selects the element at the specified index. |
|
Filters elements to only include those containing the specified text. |
|
Find only immediate children |
Assertions
Core Assertions
|
Asserts that the page has the specified URL. |
|
Asserts that the element is visible. |
|
Asserts that the element is enabled. |
|
Asserts that the element has the specified text. |
|
Asserts that the element has the specified attribute and value. |
|
Asserts that the locator resolves to the specified number of elements. |
Advanced Assertions
|
Asserts that the page has the specified title. |
|
Asserts that the element has the specified CSS property and value. |
|
Asserts that the element has the specified value. |
|
Asserts that the element contains the specified text. |
|
Asserts that the element is not visible (negative assertion). |
Soft Assertions
Playwright does not have built-in soft assertions. You can implement your own using |
Example:
|
Debugging Techniques
Debugging in VS Code
|
Playwright Inspector
The Playwright Inspector is a GUI tool to help you debug your tests. It allows stepping through test execution, inspecting the DOM, and generating selectors. Run tests in debug mode:
This opens the inspector, pauses execution at the first action, and allows stepping through the test. |
Browser DevTools
You can use the browser’s DevTools for debugging:
|
Tracing
Playwright’s tracing feature records detailed information about test executions, including network requests, console logs, and screenshots.
|
Console Logging
Use Example:
|
Advanced Configuration
Test Configuration File
The Example:
|
Environment Variables
|
Sets the base URL for the tests. |
|
Runs tests in headed mode (shows the browser). |
|
Enables debug logging for Playwright API calls. |
Test Retries
Configure test retries in
|
Timeouts
|
Sets the global timeout for tests in |
|
Sets a custom timeout for a specific assertion. |
|
Sets a custom timeout for page navigation. |