11 KiB
| id | title |
|---|---|
| test-assertions | Assertions |
Playwright Test uses expect library for test assertions. This library provides
a lot of matchers like toEqual, toContain, toMatch, toMatchSnapshot and many more:
expect(success).toBeTruthy();
Playwright also extends it with convenience async matchers that will wait until
the expected condition is met. In general, we can expect the opposite to be true by adding a .not to the front
of the matchers:
expect(value).not.toEqual(0);
await expect(locator).not.toContainText("some text");
By default, failed assertion will terminate test execution. Playwright also supports soft assertions: failed soft assertions do not terminate test execution, but mark the test as failed.
// Make a few checks that will not stop the test when failed...
await expect.soft(page.locator('#status')).toHaveText('Success');
await expect.soft(page.locator('#eta')).toHaveText('1 day');
// ... and continue the test to check more things.
await page.locator('#next-page').click();
await expect.soft(page.locator('#title')).toHaveText('Make another order');
You can specify a custom error message as a second argument to the expect function, for example:
expect(value, 'my custom error message').toBe(42);
expect.soft(value, 'my soft assertion').toBe(56);
Matching
Consider the following example:
await expect(page.locator('.status')).toHaveText('Submitted');
Playwright Test will be re-testing the node with the selector .status until fetched Node has the "Submitted"
text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is
reached. You can either pass this timeout or configure it once via the [property: TestConfig.expect] value
in test config.
By default, the timeout for assertions is set to 5 seconds. Learn more about various timeouts.
expect(locator).toBeChecked([options])
optionschecked<[boolean]>timeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to the checked input.
const locator = page.locator('.subscribe');
await expect(locator).toBeChecked();
expect(locator).toBeDisabled([options])
optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to a disabled element.
const locator = page.locator('button.submit');
await expect(locator).toBeDisabled();
expect(locator).toBeEditable([options])
optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an editable element.
const locator = page.locator('input');
await expect(locator).toBeEditable();
expect(locator).toBeEmpty([options])
optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an empty editable element or to a DOM node that has no text.
const locator = page.locator('div.warning');
await expect(locator).toBeEmpty();
expect(locator).toBeEnabled([options])
optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an enabled element.
const locator = page.locator('button.submit');
await expect(locator).toBeEnabled();
expect(locator).toBeFocused([options])
optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to a focused DOM node.
const locator = page.locator('input');
await expect(locator).toBeFocused();
expect(locator).toBeHidden([options])
optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to a hidden DOM node, which is the opposite of visible.
const locator = page.locator('.my-element');
await expect(locator).toBeHidden();
expect(locator).toBeVisible([options])
optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to a visible DOM node.
const locator = page.locator('.my-element');
await expect(locator).toBeVisible();
expect(locator).toContainText(expected[, options])
expected<[string] | [RegExp] | [Array]<[string]|[RegExp]>>optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].useInnerText<[boolean]> Whether to useelement.innerTextinstead ofelement.textContentwhen retrieving DOM node text.
Ensures [Locator] points to an element that contains the given text. You can use regular expressions for the value as well.
const locator = page.locator('.title');
await expect(locator).toContainText('substring');
await expect(locator).toContainText(/\d messages/);
Note that if array is passed as an expected value, entire lists can be asserted:
const locator = page.locator('list > .list-item');
await expect(locator).toContainText(['Text 1', 'Text 4', 'Text 5']);
expect(locator).toHaveAttribute(name, value[, options])
name<[string]> Attribute namevalue<[string]|[RegExp]> Attribute valueoptionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an element with given attribute.
const locator = page.locator('input');
await expect(locator).toHaveAttribute('type', 'text');
expect(locator).toHaveClass(expected[, options])
expected<[string] | [RegExp] | [Array]<[string]|[RegExp]>>optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an element with given CSS class.
const locator = page.locator('#component');
await expect(locator).toHaveClass(/selected/);
Note that if array is passed as an expected value, entire lists can be asserted:
const locator = page.locator('list > .component');
await expect(locator).toHaveClass(['component', 'component selected', 'component']);
expect(locator).toHaveCount(count[, options])
count<[number]>optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] resolves to an exact number of DOM nodes.
const list = page.locator('list > .component');
await expect(list).toHaveCount(3);
expect(locator).toHaveCSS(name, value[, options])
name<[string]> CSS property namevalue<[string]|[RegExp]> CSS property valueoptionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] resolves to an element with the given computed CSS style.
const locator = page.locator('button');
await expect(locator).toHaveCSS('display', 'flex');
expect(locator).toHaveId(id[, options])
id<[string]> Element idoptionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an element with the given DOM Node ID.
const locator = page.locator('input');
await expect(locator).toHaveId('lastname');
expect(locator).toHaveJSProperty(name, value[, options])
name<[string]> Property namevalue<[any]> Property valueoptionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an element with given JavaScript property. Note that this property can be of a primitive type as well as a plain serializable JavaScript object.
const locator = page.locator('.component');
await expect(locator).toHaveJSProperty('loaded', true);
expect(locator).toHaveText(expected[, options])
expected<[string] | [RegExp] | [Array]<[string]|[RegExp]>>optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].useInnerText<[boolean]> Whether to useelement.innerTextinstead ofelement.textContentwhen retrieving DOM node text.
Ensures [Locator] points to an element with the given text. You can use regular expressions for the value as well.
const locator = page.locator('.title');
await expect(locator).toHaveText(/Welcome, Test User/);
await expect(locator).toHaveText(/Welcome, .*/);
Note that if array is passed as an expected value, entire lists can be asserted:
const locator = page.locator('list > .component');
await expect(locator).toHaveText(['Text 1', 'Text 2', 'Text 3']);
expect(locator).toHaveValue(value[, options])
value<[string] | [RegExp]>optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures [Locator] points to an element with the given input value. You can use regular expressions for the value as well.
const locator = page.locator('input[type=number]');
await expect(locator).toHaveValue(/[0-9]/);
expect(page).toHaveTitle(title[, options])
title<[string] | [RegExp]>optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures page has a given title.
await expect(page).toHaveTitle(/.*checkout/);
expect(page).toHaveURL(url[, options])
url<[string] | [RegExp]>optionstimeout<[number]> Time to retry assertion for, defaults totimeoutin [property: TestConfig.expect].
Ensures page is navigated to a given URL.
await expect(page).toHaveURL(/.*checkout/);
expect(value).toMatchSnapshot(name[, options])
name<[string] | [Array]<[string]>> Snapshot name.optionsthreshold<[float]> Image matching threshold between zero (strict) and one (lax), default is configurable with [property: TestConfig.expect].
Ensures that passed value, either a [string] or a [Buffer], matches the expected snapshot stored in the test snapshots directory.
// Basic usage.
expect(await page.screenshot()).toMatchSnapshot('landing-page.png');
// Configure image matching threshold.
expect(await page.screenshot()).toMatchSnapshot('landing-page.png', { threshold: 0.3 });
// Bring some structure to your snapshot files by passing file path segments.
expect(await page.screenshot()).toMatchSnapshot(['landing', 'step2.png']);
expect(await page.screenshot()).toMatchSnapshot(['landing', 'step3.png']);
Learn more about visual comparisons.