Friday, April 11, 2025

Negating matchers with expect Assertion in playwright

 In Playwright, you can negate matchers using the .not modifier with the expect assertion API. This is super useful when you want to assert that something does not happen or does not exist.

Negating matchers with expect Assertion in playwright


Syntax

await expect(locator).not.toHaveText('Unwanted Text');

Most commonly used examples :


Assert that an element does not contain specific text

await expect(page.locator('h1')).not.toHaveText('Error');

Assert that a checkbox is not checked

await expect(page.locator('#subscribe')).not.toBeChecked();


❌ Assert that an element is not visible

await expect(page.locator('.loading-spinner')).not.toBeVisible();

 Assert that a URL does not match a certain pattern

await expect(page).not.toHaveURL(/login/);

This is all about Negating matchers with expect Assertion in playwright.


No comments:

Post a Comment