Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright Generic Assertions


Share

Playwright Generic Assertions

A generic assertion checks a plain JavaScript value once, right away.

It does not wait for the page, so use it for values you already have, not for elements.

For elements, use the web-first assertions from the Assertions chapter, like toBeVisible().

They retry until the page is ready.


Generic Assertions

Instead of a locator, you can pass a plain value to expect(), like a number, a string or an array.

These are generic assertions.

They check the value once and do not retry, so you do not write await before them.

Example

tests/generic.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('generic assertions', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/shop.html');
  await expect(page.getByTestId('product')).toHaveCount(5);

  const names = await page.getByTestId('product').getByRole('heading').allTextContents();
  expect(names.length).toBe(5);
  expect(names[0]).toBe('Laptop Stand');
  expect(names).toContain('Webcam');
  expect(names.slice(0, 2)).toEqual(['Laptop Stand', 'USB-C Cable']);
});
AssertionChecks that
toBe(value)The value is exactly this value (the same number, string or object)
toEqual(value)The value has the same content, for example an array with the same items
toContain(item)An array contains this item, or a string contains this text
toBeTruthy()The value is true, or counts as true in JavaScript (for example 1 or a text that is not empty)
toBeGreaterThan(n)A number is greater than n

The first assertion waits with toHaveCount(5), so the products are loaded before the test reads their names.

allTextContents() returns the text of every matching element, as an array of strings.



Web-first or Generic?

Use web-first assertions for anything on the page.

A generic assertion on a value read from the page checks it only once, and the page may not be ready yet.

This test reads the welcome message right after clicking "Log in".

The practice login page takes 600 milliseconds to answer:

Example

tests/nowait.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('generic assertions do not wait', async ({ page }) => {
  await page.goto('https://www.w3schools.com/playwright/practice/login.html');
  await page.getByLabel('Username').fill('student');
  await page.getByLabel('Password').fill('w3schools');
  await page.getByRole('button', { name: 'Log in' }).click();

  const visible = await page.getByTestId('welcome').isVisible();
  expect(visible).toBe(true);
});

Output:

Running 1 test using 1 worker

[1/1] [chromium] › tests/nowait.spec.js:4:1 › generic assertions do not wait
  1) [chromium] › tests/nowait.spec.js:4:1 › generic assertions do not wait ────────────────────────

    Error: expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

       9 |
      10 |   const visible = await page.getByTestId('welcome').isVisible();
    > 11 |   expect(visible).toBe(true);
         |                   ^
      12 | });
      13 |
        at /home/user/my-project/tests/nowait.spec.js:11:19

  1 failed
    [chromium] › tests/nowait.spec.js:4:1 › generic assertions do not wait ─────────────────────────

The welcome message was not there yet when isVisible() looked.

The web-first version waits for it, and passes:

Example

await expect(page.getByTestId('welcome')).toBeVisible();


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.