Intermediate
Chapter 6 · 8 min read
Assertions
Verify your app with auto-retrying assertions — toBeVisible, toHaveText, toHaveValue, negation, and plain value assertions.
Locator Assertions
Locator assertions take a locator, auto-wait, and retry until the condition is met or the timeout expires (5 seconds by default). They must always be awaited. These cover the state of any element:
state.test.ts
import { test, expect } from '@mobilewright/test';
test('element state assertions', async ({ screen }) => {
await expect(screen.getByText('Welcome')).toBeVisible();
await expect(screen.getByText('Loading')).toBeHidden();
await expect(screen.getByRole('button', { name: 'Submit' })).toBeEnabled();
await expect(screen.getByRole('checkbox')).toBeChecked();
await expect(screen.getByRole('tab', { name: 'Home' })).toBeSelected();
// Text and value checks — strings or regular expressions
await expect(screen.getByTestId('greeting')).toHaveText('Hello, World');
await expect(screen.getByTestId('greeting')).toContainText('Hello');
await expect(screen.getByLabel('Email')).toHaveValue(/example\.com/);
});
state.test.js
const { test, expect } = require('@mobilewright/test');
test('element state assertions', async ({ screen }) => {
await expect(screen.getByText('Welcome')).toBeVisible();
await expect(screen.getByText('Loading')).toBeHidden();
await expect(screen.getByRole('button', { name: 'Submit' })).toBeEnabled();
await expect(screen.getByRole('checkbox')).toBeChecked();
await expect(screen.getByRole('tab', { name: 'Home' })).toBeSelected();
// Text and value checks — strings or regular expressions
await expect(screen.getByTestId('greeting')).toHaveText('Hello, World');
await expect(screen.getByTestId('greeting')).toContainText('Hello');
await expect(screen.getByLabel('Email')).toHaveValue(/example\.com/);
});
| Assertion | Passes when |
|---|---|
toBeVisible() | Element exists and is visible |
toBeHidden() | Element does not exist or is not visible |
toBeEnabled() / toBeDisabled() | Element is enabled / disabled |
toBeChecked() | Checkbox or switch is checked |
toBeFocused() | Element has focus |
toBeSelected() | Element (e.g. a tab) is selected |
toHaveText(expected) | Text matches exactly (string) or by pattern (RegExp) |
toContainText(expected) | Text contains the substring |
toHaveValue(expected) | Input value matches string or RegExp |
Negation and Timeouts
Add .not before any assertion to flip it, and pass a timeout option when one check needs more (or less) time than the default:
negation.test.ts
import { test, expect } from '@mobilewright/test';
test('negation and custom timeouts', async ({ screen }) => {
// .not flips any assertion
await expect(screen.getByText('Error')).not.toBeVisible();
await expect(screen.getByRole('button')).not.toBeDisabled();
// Give a slow screen more time (10 seconds instead of 5)
await expect(screen.getByText('Done')).toBeVisible({ timeout: 10_000 });
});
negation.test.js
const { test, expect } = require('@mobilewright/test');
test('negation and custom timeouts', async ({ screen }) => {
// .not flips any assertion
await expect(screen.getByText('Error')).not.toBeVisible();
await expect(screen.getByRole('button')).not.toBeDisabled();
// Give a slow screen more time (10 seconds instead of 5)
await expect(screen.getByText('Done')).toBeVisible({ timeout: 10_000 });
});
Value Assertions
When expect receives a plain value instead of a locator, you get the standard matchers you know from Jest. Important: these check the value once — they do not auto-wait or retry:
values.test.ts
import { test, expect } from '@mobilewright/test';
test('plain value assertions', async ({ screen }) => {
const count = await screen.getByRole('listitem').count();
expect(count).toBe(5); // strict equality
expect(count).toBeGreaterThan(0); // numeric comparison
expect([1, 2, 3]).toContain(2); // arrays and strings
expect('hello world').toContain('world');
expect({ a: 1 }).toEqual({ a: 1 }); // deep equality
});
values.test.js
const { test, expect } = require('@mobilewright/test');
test('plain value assertions', async ({ screen }) => {
const count = await screen.getByRole('listitem').count();
expect(count).toBe(5); // strict equality
expect(count).toBeGreaterThan(0); // numeric comparison
expect([1, 2, 3]).toContain(2); // arrays and strings
expect('hello world').toContain('world');
expect({ a: 1 }).toEqual({ a: 1 }); // deep equality
});
Tip: Rule of thumb: assert on locators whenever possible — they auto-wait and make tests stable. Only use value assertions for data you have already read out of the app.
Mobilewright
Intermediate
Assertions
expect
Written by PV
© 2026 All Rights Reserved