๐ฏ Playwright Locators in JavaScript (Complete Guide)
This guide explains each Playwright locator with:
- ✅ What it is
- ๐ When to use
- ⚙️ How to use it
- ๐ฏ Benefits
- ๐งช Code Examples
๐น 1. Locator by ID
- ✅ What: Selects an element with a unique
id. - ๐ When: Element has a unique
id. - ⚙️ How:
page.locator('#username') - ๐ฏ Benefit: Fast and reliable.
<input id="username" />
await page.locator('#username').fill('John');
๐น 2. Locator by Class
- ✅ What: Selects by
class. - ๐ When: Repeated or styled elements.
- ⚙️ How:
page.locator('.password') - ๐ฏ Benefit: Useful for shared styling.
<input class="password" />
await page.locator('.password').fill('12345');
๐น 3. Locator by Text
- ✅ What: Matches visible element text.
- ๐ When: For buttons, links, etc.
- ⚙️ How:
page.getByText('Login') - ๐ฏ Benefit: Human-readable.
<button>Login</button>
await page.getByText('Login').click();
๐น 4. Locator by Placeholder
- ✅ What: Uses
placeholdertext. - ๐ When: For form hints.
- ⚙️ How:
getByPlaceholder() - ๐ฏ Benefit: Clear and targeted.
<input placeholder="Enter username" />
await page.getByPlaceholder('Enter username').fill('admin');
๐น 5. Locator by Role
- ✅ What: Uses ARIA roles (e.g., button, heading).
- ๐ When: For accessible apps.
- ⚙️ How:
getByRole() - ๐ฏ Benefit: Accessibility-friendly.
<button>Login</button>
await page.getByRole('button', { name: 'Login' }).click();
๐น 6. Locator by Label
- ✅ What: Uses label text for inputs.
- ๐ When: For labeled form fields.
- ⚙️ How:
getByLabel() - ๐ฏ Benefit: Strong for forms.
<label for="user">Username</label>
<input id="user" />
await page.getByLabel('Username').fill('john');
๐น 7. Locator by Name Attribute
- ✅ What: Targets elements with
name. - ๐ When: Useful for forms.
- ⚙️ How:
locator('[name="email"]') - ๐ฏ Benefit: Common in backend forms.
<input name="email" />
await page.locator('[name="email"]').fill('abc@example.com');
๐น 8. Locator by Data Test ID
- ✅ What: Uses
data-testid. - ๐ When: For stable test targeting.
- ⚙️ How:
locator('[data-testid="xyz"]') - ๐ฏ Benefit: Doesn’t break on UI changes.
<button data-testid="login-button">Login</button>
await page.locator('[data-testid="login-button"]').click();
๐น 9. Locator by Tag + Class
- ✅ What: Combines element tag and class.
- ๐ When: For reusable component patterns.
- ⚙️ How:
locator('li.item') - ๐ฏ Benefit: Precise targeting in groups.
<li class="item">Item 1</li>
await page.locator('li.item').click();
๐น 10. Locator with nth(), first(), last()
- ✅ What: Picks from multiple matches.
- ๐ When: Lists, repeated buttons, etc.
- ⚙️ How:
nth(index),first(),last() - ๐ฏ Benefit: Fine control on lists.
<ul>
<li class="item">One</li>
<li class="item">Two</li>
</ul>
await page.locator('li.item').nth(1).click();
✅ Best Practices
| Practice | Why |
|---|---|
Use data-testid |
Most stable and intention-specific |
Prefer getByRole or getByLabel |
Ensures accessibility |
Avoid excessive use of nth() |
Breaks if DOM order changes |
| Use text-based locators for user-facing elements | Reflects real user interaction |
Need help converting this to a GitHub test project or video tutorial? Let me know!
Comments
Post a Comment