How to Actually Use This Page
- Read the scenario only. Don’t peek at the answer.
- Say the code out loud, or actually type it in a real test file against a real page.
- Then click to reveal and check yourself.
- Missed one? Mark it, come back to just the marked ones tomorrow. That’s spaced repetition —
it’s why this page exists instead of just pointing you at the reference doc.
[!TIP]
10-15 a day beats all 50 in one sitting. Cramming gets you recognition (“oh yeah, I’ve seen
that”). Spaced active recall gets you the real thing — typing it correctly without thinking,
mid-interview or mid-incident.
Locators
Find a button by its accessible name — the best-practice first choice.
```typescript
page.getByRole('button', { name: 'Submit' })
```
Find a form field by its visible label.
```typescript
page.getByLabel('Email address')
```
Find an element by a data-testid attribute.
```typescript
page.locator('[data-testid="login-btn"]')
```
Scope a locator to inside a specific card that contains certain text.
```typescript
page.locator('.card').filter({ hasText: 'Premium' })
```
Get the 3rd row of a table (0-indexed).
```typescript
page.locator('table tbody tr').nth(2)
```
Write a resilient locator that tries a label, then a placeholder, then an input type — in that order.
```typescript
page.getByLabel('Email').or(page.getByPlaceholder(/email/i)).or(page.locator('input[type="email"]'))
```
Actions
Fill a text field (clearing it first).
```typescript
await locator.fill('text');
```
Check a checkbox — the version that works whether or not it's already checked.
```typescript
await locator.check();
```
Upload a file to a file input.
```typescript
await locator.setInputFiles('path/to/file.pdf');
```
Drag one element onto another.
```typescript
await locator.dragTo(otherLocator);
```
Assertions
Assert an element is visible.
```typescript
await expect(locator).toBeVisible();
```
Assert an element's text contains a substring (not exact match).
```typescript
await expect(locator).toContainText('partial');
```
Assert exactly 5 matching elements exist.
```typescript
await expect(locator).toHaveCount(5);
```
Assert the page URL matches a pattern.
```typescript
await expect(page).toHaveURL(/dashboard/);
```
What's the single most common beginner bug with assertions, and how do you avoid it?
Forgetting `await` on `expect()` — the assertion silently no-ops and the test "passes" without
actually checking anything. Always `await expect(...)`.
Waiting
Wait for the network to go idle before continuing.
```typescript
await page.waitForLoadState('networkidle');
```
Wait for a specific API response before asserting on what it caused.
```typescript
await page.waitForResponse(resp => resp.url().includes('/api/') && resp.status() === 200);
```
What should you almost never use instead of a condition-based wait?
```typescript
// ❌ Avoid — defeats the point of Playwright's auto-waiting
await page.waitForTimeout(3000);
```
API Testing (request fixture)
Send a GET request and assert it succeeded.
```typescript
const res = await request.get('/api/posts');
expect(res.ok()).toBeTruthy();
```
Send a POST request with a JSON body.
```typescript
const res = await request.post('/api/posts', { data: { title: 'x' } });
```
Get the parsed JSON body from a response.
```typescript
const body = await res.json();
```
CLI Commands
Run the full suite, watching it happen in a real browser.
```bash
npx playwright test --headed
```
Open the time-travel UI debugger (the one you should reach for daily).
```bash
npx playwright test --ui
```
Run only tests tagged @smoke.
```bash
npx playwright test --grep @smoke
```
Shard a large suite across 3 parallel CI machines — this is shard 1.
```bash
npx playwright test --shard=1/3
```
Open a saved trace file from a failed CI run.
```bash
npx playwright show-trace trace.zip
```
Record actions in a browser and generate starter test code.
```bash
npx playwright codegen https://example.com
```
Git & CI/CD (Module 08)
Undo a commit that's already been pushed and pulled by teammates.
```bash
git revert
```
</details>
Push, but safely refuse if the remote has changes you haven't seen.
```bash
git push --force-with-lease
```
Find a "lost" commit after a bad reset --hard.
```bash
git reflog
```
---
## 🎯 Full-Speed Round (No Hints)
1. Locate a button by its accessible name.
2. Assert an element is visible before interacting with it.
3. Upload a file to a file input.
4. Wait for a specific API call to complete before asserting.
5. Send a POST request with a JSON body via the `request` fixture.
6. Run only `@smoke`-tagged tests.
7. Open the trace from a CI failure you can't reproduce locally.
8. Undo a commit that teammates have already pulled.
Answers
1. `page.getByRole('button', { name: '...' })`
2. `await expect(locator).toBeVisible();`
3. `await locator.setInputFiles('path/to/file.pdf');`
4. `await page.waitForResponse(resp => resp.url().includes('/api/'));`
5. `await request.post('/api/posts', { data: {...} });`
6. `npx playwright test --grep @smoke`
7. `npx playwright show-trace trace.zip`
8. `git revert `
</details>
---