Playwright Generic Assertions
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']);
});
| Assertion | Checks 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();