Back to All Questions
Question 34 of 100
Assertions
Intermediate
How to use Negative Assertions in Playwright?
The Answer
Prepend `.not` before any Web-First assertion to invert it. Like positive assertions, `.not` assertions also auto-retry, waiting for the condition to become false.
Deep Dive Explanation
Avoid using `locator.isVisible()` or `locator.isHidden()` for assertions β they return immediately without waiting. Always use the assertion form `expect(locator).not.toBeVisible()` which retries.
example.spec.ts
// Negative assertions - all auto-retry
await expect(page.locator('.error-banner')).not.toBeVisible();
await expect(page.getByRole('button', { name: 'Submit' })).not.toBeDisabled();
await expect(page.locator('#user-menu')).not.toHaveText('Guest');
await expect(page).not.toHaveURL('/login');
// Useful after actions
await page.getByRole('button', { name: 'Delete' }).click();
await expect(page.getByText('Record #42')).not.toBeVisible({ timeout: 5000 });