πŸ’‘ If you like this website, please share it with your friends and network! πŸš€

Lesson 3.5: Assertions & Validations

πŸ” The expect() API

Unlike Selenium where you had to write your own waits, Playwright's expect() automatically retries until the condition is met or a timeout occurs.

typescript

// Common Assertions

await expect(page.getByRole('heading')).toHaveText('Welcome Back');

await expect(page.getByLabel('Email')).toHaveValue('user@example.com');

await expect(page).toHaveURL('/dashboard');

🧊 Soft Assertions

With regular assertions, the test stops on the first failure. Soft assertions let ALL checks run, then report all failures together.

typescript

await expect.soft(page.getByText('Welcome')).toBeVisible();

await expect.soft(page.locator('.stats-card')).toHaveCount(4);

πŸ“Έ Visual Comparisons (Screenshot Testing)

typescript

// Takes a screenshot on first run (baseline), then compares on next runs

await expect(page).toHaveScreenshot('homepage.png');

// Update snapshots when the UI intentionally changes:

// npx playwright test --update-snapshots

    CareerRaah: The Ultimate Interview Preparation Platform