-
Book Overview & Buying
-
Table Of Contents
Hands-On Automated Testing with Playwright
By :
AI-generated scripts can be a great starting point, but they often need a bit of polishing to meet best practices and suit your project’s specific needs. They usually include the basics like navigation, clicking buttons, or simple assertions but may fall short on things like context-aware logic, error handling, or element selection.Take a few moments to check for:Fragile selectors like div:nth-child(3). These can easily break if the page structure changes.Missing waits, which can cause flaky tests when dealing with dynamic content.For example, a basic AI-generated script might test a button like this:
await page.click('button');
This works, but the selector isn’t very specific. Instead, aim for role- or text-based selectors (like getByRole, getByText) and custom data attributes, like (data-testid), if available:
await page.getByRole('button', { name: 'Submit' }).click();
Critical actions can...