Playwright Negating Matchers

Profile picture for user arilio666

We can add .not property to the front of matches like toEqual(), toContainText(), toHaveAttribute(), or any other matcher to expect the opposite. It makes the assertion check for the opposite condition.

Table of Contents

  1. Demo Website
  2. Examples
  3. Video Tutorial

Demo Website

For a demo, visit form6 of the Auto Pract Website. Here you will see a checkbox where the element's state will change on the checkbox selection.

When Checkbox is Selected:

Playwright Negative Assertions

When Checkbox is Not Selected:

Playwright check negative assertion condition


Number and String Match

Assert Number Not Equal

let a = 10;

Assert String Not Equal

let b = "hello";

When Checkbox is Not Selected

Assert Checkbox Not Checked

await expect(page.locator("#mycheckbox")).not.toBeChecked();

Assert Button Not Enabled

await expect(page.locator("#mybutton")).not.toBeEnabled();

Assert Link Not Visible

await expect(page.locator("#mylink")).not.toBeVisible();

When Checkbox is  Selected

Select Checkbox First


Assert Button Not Disabled

 await expect(page.locator("#mybutton")).not.toBeDisabled() 

Assert Link Not Hidden

await expect(page.locator("#mylink")).not.toBeHidden();

Assert Label Not Contain Text

await expect(page.locator("#mylabel")).not.toContainText("Checkbox is not selected.")

Assert Not Have Attribute

await expect(page.locator("#mybutton")).not.toHaveAttribute("disabled","disabled")

Video Tutorial: Playwright Not Expect