2021-08-06 14:02:41 -07:00
|
|
|
---
|
|
|
|
id: test-assertions
|
|
|
|
title: "Assertions"
|
|
|
|
---
|
|
|
|
|
|
|
|
Playwright Test uses [expect](https://jestjs.io/docs/expect) library for test assertions. This library provides
|
|
|
|
a lot of matchers like `toEqual`, `toContain`, `toMatch`, `toMatchSnapshot` and many more:
|
|
|
|
|
|
|
|
```js
|
|
|
|
expect(success).toBeTruthy();
|
|
|
|
```
|
|
|
|
|
|
|
|
Playwright also extends it with convenience async matchers that will wait until
|
2022-02-08 15:44:44 -07:00
|
|
|
the expected condition is met. Consider the following example:
|
|
|
|
|
|
|
|
```js
|
|
|
|
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](./test-timeouts.md).
|
|
|
|
|
|
|
|
<!-- TOC -->
|
|
|
|
|
|
|
|
## Negating Matchers
|
|
|
|
|
|
|
|
In general, we can expect the opposite to be true by adding a `.not` to the front
|
2021-10-28 08:35:35 -07:00
|
|
|
of the matchers:
|
|
|
|
|
|
|
|
```js
|
|
|
|
expect(value).not.toEqual(0);
|
|
|
|
await expect(locator).not.toContainText("some text");
|
|
|
|
```
|
2021-08-06 14:02:41 -07:00
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
## Soft Assertions
|
|
|
|
|
2022-02-02 19:33:51 -07:00
|
|
|
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.
|
|
|
|
|
|
|
|
```js
|
|
|
|
// 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');
|
|
|
|
```
|
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
At any point during test execution, you can check whether there were any
|
|
|
|
soft assertion failures:
|
|
|
|
|
|
|
|
```js
|
|
|
|
// 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');
|
|
|
|
|
|
|
|
// Avoid running further if there were soft assertion failures.
|
|
|
|
expect(test.info().errors).toBeEmpty();
|
|
|
|
```
|
|
|
|
|
|
|
|
## Custom Expect Message
|
|
|
|
|
2022-02-02 19:33:51 -07:00
|
|
|
You can specify a custom error message as a second argument to the `expect` function, for example:
|
2022-01-31 18:14:59 -07:00
|
|
|
|
|
|
|
```js
|
2022-02-08 15:44:44 -07:00
|
|
|
await expect(page.locator('text=Name'), 'should be logged in').toBeVisible();
|
2022-01-31 18:14:59 -07:00
|
|
|
```
|
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
The error would look like this:
|
2021-08-06 14:02:41 -07:00
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
```bash
|
|
|
|
Error: should be logged in
|
2021-08-06 14:02:41 -07:00
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
Call log:
|
|
|
|
- expect.toBeVisible with timeout 5000ms
|
|
|
|
- waiting for selector "text=Name"
|
2021-08-06 14:02:41 -07:00
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
|
|
|
|
2 |
|
|
|
|
3 | test('example test', async({ page }) => {
|
|
|
|
> 4 | await expect(page.locator('text=Name'), 'should be logged in').toBeVisible();
|
|
|
|
| ^
|
|
|
|
5 | });
|
|
|
|
6 |
|
2021-08-06 14:02:41 -07:00
|
|
|
```
|
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
The same works with soft assertions:
|
2021-08-06 14:02:41 -07:00
|
|
|
|
2022-02-08 15:44:44 -07:00
|
|
|
```js
|
|
|
|
expect.soft(value, 'my soft assertion').toBe(56);
|
|
|
|
```
|
2021-08-06 14:02:41 -07:00
|
|
|
|
2022-03-03 10:06:14 -08:00
|
|
|
## API reference
|
|
|
|
See the following pages for Playwright-specific assertions:
|
|
|
|
- [APIResponseAssertions] assertions for [APIResponse]
|
|
|
|
- [LocatorAssertions] assertions for [Locator]
|
|
|
|
- [PageAssertions] assertions for [Page]
|
|
|
|
- [ScreenshotAssertions] for comparing screenshot with stored value
|