- May 12, 2025
- admin
- 0
Assertions in Playwright:
Playwright includes test assertions in the form of expect function. To make an assertion, call expect(value) and choose a matcher that reflects the expectation.
There are many generic matchers like toEqual, toContain, toBeTruthy that can be used to assert any conditions.
Here is the list of the most popular async assertions.
| Assertion | Description |
|---|---|
| expect(locator).toBeChecked() | Checkbox is checked |
| expect(locator).toBeEnabled() | Control is enabled |
| expect(locator).toBeVisible() | Element is visible |
| expect(locator).toContainText() | Element contains text |
| expect(locator).toHaveAttribute() | Element has attribute |
| expect(locator).toHaveCount() | List of elements has given length |
| expect(locator).toHaveText() | Element matches text |
| expect(locator).toHaveValue() | Input element has value |
| expect(page).toHaveTitle() | Page has title |
| expect(page).toHaveURL() | Page has URL |
| Assertion | Description |
|---|---|
| toBeVisible | // Expects page to have a heading with the name of Installation. await expect(page.getByRole(‘heading’, { name: ‘Installation’ })).toBeVisible(); |
| toBeChecked | //Verify checkbox is checked const toggleAll = page.getByLabel(‘Mark all as complete’); await toggleAll.check(); await expect(toggleAll).toBeChecked(); |
| toBeHidden | await expect(page.getByRole(‘button’, { name: ‘Clear completed’ })).toBeHidden(); |
| toHaveText | // Verify element having text. await expect(todoCount).toHaveText(‘3 items left’); await expect(todoCount).toContainText(‘3’); |
| toHaveAttribute | //Verify Element has attribute const locator = page.locator(‘input’); await expect(locator).toHaveAttribute(‘type’, ‘text’); |
| toHaveCount | //Verify List of elements has given length const list = page.locator(‘list > .component’); await expect(list).toHaveCount(3); |
| toHaveValue | //Verify Input element has value const locator = page.locator(‘input[type=number]’); await expect(locator).toHaveValue(/[0–9]/); |
| toHaveTitle | //Verify page is having Title await expect(page).toHaveTitle(/.*checkout/); |
| toHaveURL | //Verify page URL // Check for the page URL to be ‘https://playwright.dev/docs/intro’ (including query string) await expect(page).toHaveURL(‘https://playwright.dev/docs/intro’); |
| toContainClass | Ensures the Locator points to an element with given CSS classes. const locator = page.locator('#component'); await expect(locator).toContainClass('middle selected row'); |
| toHaveAccessibleDescription | Ensures the Locator points to an element with a given accessible description. const locator = page.getByTestId('save-button'); await expect(locator).toHaveAccessibleDescription('Save results to disk'); |
| toHaveAccessibleErrorMessage | Ensures the Locator points to an element with a given aria errormessage. const locator = page.getByTestId('username-input'); await expect(locator).toHaveAccessibleErrorMessage('Username is required.'); |
| toHaveAccessibleName | Ensures the Locator points to an element with a given accessible name. const locator = page.getByTestId('save-button'); await expect(locator).toHaveAccessibleName('Save to disk'); |
