Playwright Negating Matchers

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")
Thu, 12/29/2022 - 07:47
Ashwin possesses over five years of experience in the Quality Assurance industry and is currently serving as a Technical Lead at iVagus. His expertise encompasses a broad range of technologies, including Cypress, Rest Assured, Selenium, Cucumber, JavaScript and TypeScript.

Video Tutorial: Use Not with Assertion in Playwright