Skip to main content

Assertions

Mobilewright uses the expect function for assertions. Locator assertions auto-wait and retry until the condition is met or the timeout expires (5 seconds by default).

Locator assertions​

These assertions accept a locator and retry automatically. They must be awaited.

State​

await expect(screen.getByText('Welcome')).toBeVisible();
await expect(screen.getByText('Loading')).toBeHidden();
await expect(screen.getByRole('button', { name: 'Submit' })).toBeEnabled();
await expect(screen.getByRole('button', { name: 'Submit' })).toBeDisabled();
await expect(screen.getByRole('checkbox')).toBeChecked();
await expect(screen.getByTestId('name-field')).toBeFocused();
await expect(screen.getByRole('tab', { name: 'Home' })).toBeSelected();
AssertionPasses when
toBeVisible()Element exists and is visible
toBeHidden()Element does not exist or is not visible
toBeEnabled()Element is enabled
toBeDisabled()Element is not enabled
toBeChecked()Element is checked
toBeFocused()Element has focus
toBeSelected()Element is selected

Text content​

await expect(screen.getByTestId('greeting')).toHaveText('Hello, World');
await expect(screen.getByTestId('greeting')).toHaveText(/Hello/);
await expect(screen.getByTestId('greeting')).toContainText('Hello');
AssertionPasses when
toHaveText(expected)Text matches exactly (string) or by pattern (RegExp)
toContainText(expected)Text contains the substring (string) or matches the pattern (RegExp)

Both matchers follow Playwright's text semantics: whitespace is trimmed and runs of whitespace collapse to a single space on both sides before comparing, so toHaveText('Total: $12.00') matches a label rendered as "Total:\n $12.00". Pass { ignoreCase: true } to compare case-insensitively:

await expect(screen.getByTestId('greeting')).toHaveText('hello, world', { ignoreCase: true });
await expect(screen.getByTestId('price')).toContainText(/\$\d+\.\d{2}/);

Value​

await expect(screen.getByLabel('Email')).toHaveValue('user@example.com');
await expect(screen.getByLabel('Email')).toHaveValue(/example\.com/);
AssertionPasses when
toHaveValue(expected)Value matches exactly (string) or by pattern (RegExp)

Negation​

Add .not before any assertion to negate it:

await expect(screen.getByText('Error')).not.toBeVisible();
await expect(screen.getByRole('button')).not.toBeDisabled();
await expect(screen.getByTestId('title')).not.toHaveText('Loading');

Timeouts​

Override the default timeout for a single assertion:

await expect(screen.getByText('Done')).toBeVisible({ timeout: 10_000 });

Value assertions​

When expect receives a plain value instead of a locator, it provides standard assertions. These do not auto-wait or retry.

Equality​

expect(count).toBe(5);
expect(result).toEqual({ name: 'Alice', age: 30 });
AssertionPasses when
toBe(expected)Values are identical (Object.is)
toEqual(expected)Values are deeply equal

Boolean​

expect(value).toBeTruthy();
expect(value).toBeFalsy();

Numeric​

expect(count).toBeGreaterThan(0);
expect(count).toBeLessThan(100);
expect(ratio).toBeCloseTo(0.3, 2);
AssertionPasses when
toBeGreaterThan(n)Value > n
toBeLessThan(n)Value < n
toBeCloseTo(n, precision?)Value is within tolerance (default precision: 2)

Collections and strings​

expect([1, 2, 3]).toContain(2);
expect('hello world').toContain('world');

Null and undefined​

expect(value).toBeNull();
expect(value).toBeUndefined();