--- name: playwright-testing description: Playwright testing. Use this skill to write and run automated tests for web applications using Playwright. --- # Test authoring guidelines For more details on Playwright best practices see ## Test user-visible behavior Automated tests should verify that the application code works for the end users, and avoid relying on implementation details such as things which users will not typically use, see, or even know about such as the name of a function, whether something is an array, or the CSS class of some element. The end user will see or interact with what is rendered on the page, so your test should typically only see/interact with the same rendered output. ## Make tests as isolated as possible Each test should be completely isolated from another test and should run independently with its own local storage, session storage, data, cookies etc. ## Avoid testing third-party dependencies Only test what you control. Don't try to test links to external sites or third party servers that you do not control, unless it is specifically for testing purposes. ## Use locators Locators come with auto waiting and retry-ability. To make tests resilient, we recommend prioritizing user-facing attributes and explicit contracts. ```javascript // 👍 Use role selectors page.getByRole("button", { name: "submit" }); // Using filters to locate elements with text page.getByRole("listitem").filter({ hasText: "Product 2" }); // Use chaining and filtering page .getByRole("listitem") .filter({ hasText: "Product 2" }) .getByRole("button", { name: "Add to cart" }); // 👎 Avoid CSS selectors page.locator("button.buttonIcon.episode-actions-later"); ``` ### Locator preference 1. getByRole to locate by explicit and implicit accessibility attributes ⭐⭐⭐⭐⭐ 2. getByText to locate by text content 3. getByLabel to locate a form control by associated label's text 4. getByPlaceholder to locate an input by placeholder 5. getByAltText to locate an element, usually image, by its text alternative 6. getByTitle to locate an element by its title attribute 7. getByTestId to locate an element based on its `data-testid` ### Filtering ```javascript // Filter elements having text page.getByRole("listitem").filter({ hasText: "Product 2" }); // Filter elements not having text page.getByRole("listitem").filter({ hasNotText: "Out of stock" }); // Filter elements having another locator inside page .getByRole("listitem") .filter({ has: page.getByRole("heading", { name: "Product 2" }) }); // Filter only visible elements // Note: Hidden elements do not have a role, so this filter is not // needed when using getByRole // 👎 CSS selector is not recommended, use a better locator if possible page.locator(".something").filter({ visible: true }); ``` The filtering locator **must be relative** to the original locator and is queried starting with the original locator match, not the document root. Therefore, the following will not work, because the filtering locator starts matching from the `