"Mastering Smart Locators and Multi-Tab Navigation"
Duration: 1.5 Hours | Theme: Advanced Locators & Multi-Page
π "As testers, sometimes clicking a link opens a new tab, sometimes we need to find fields without IDs, and sometimes we select buttons based on text. Playwright gives us smart locators (`getBy*`) to handle this."
Verify that clicking the footer PrestaShop link opens a new page.
Race condition occurs! The test might wait indefinitely for a new page event that never comes, or the click might happen before the listener is set up. `Promise.all` ensures both operations (waiting for event + clicking) happen simultaneously.
Fill Login form where fields have visible labels.
Accessibility & Maintainability:
On PrestaShop homepage β search bar placeholder.
Common failure scenarios:
Use role β button, link, checkbox, radio.
Multiple advantages:
Navigate by visible text, e.g., "Create an account".
Multiple strategies:
Add a specific product to cart (instead of using nth selectors).
Key differences:
| `.nth()` | `.filter({ hasText })` |
|---|---|
| Position-based (1st, 2nd, 3rd) | Content-based (specific text) |
| Brittle - breaks if order changes | Resilient - works regardless of position |
| Hard to maintain | Self-documenting and maintainable |
| Fast execution | Slightly slower but more reliable |
Validate success message after adding to cart.
Key differences:
`page.locator()` is the base method that accepts any CSS selector, XPath, or text. `page.getBy*()` methods are semantic helpers that use best practices for accessibility and maintainability.
Example:
Multiple strategies:
Always use Promise.all pattern:
Why? Prevents race conditions and ensures the event listener is set up before the action that triggers it.
Frame handling:
Key differences:
File upload methods:
Authentication strategies:
Test data strategies: