Makes the assertion check for the opposite condition. For example, this code tests that the Locator doesn't contain
text "error"
:
await expect(locator).not.toContainText('error');
Ensures that Locator points to an attached DOM node.
Usage
await expect(page.getByText('Hidden text')).toBeAttached();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to a checked input.
Usage
const locator = page.getByLabel('Subscribe to newsletter');
await expect(locator).toBeChecked();
Optional
options: { Optional
checked?: booleanOptional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to a disabled element. Element is disabled if it has "disabled" attribute or is
disabled via
'aria-disabled'. Note
that only native control elements such as HTML button
, input
, select
, textarea
, option
, optgroup
can be
disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored by the browser.
Usage
const locator = page.locator('button.submit');
await expect(locator).toBeDisabled();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an editable element.
Usage
const locator = page.getByRole('textbox');
await expect(locator).toBeEditable();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an enabled element.
Usage
const locator = page.locator('button.submit');
await expect(locator).toBeEnabled();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to a focused DOM node.
Usage
const locator = page.getByRole('textbox');
await expect(locator).toBeFocused();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures that Locator either does not resolve to any DOM node, or resolves to a non-visible one.
Usage
const locator = page.locator('.my-element');
await expect(locator).toBeHidden();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an element that intersects viewport, according to the intersection observer API. ratio in options was not supported Usage
const locator = page.getByRole('button');
// Make sure at least some part of element intersects viewport.
await expect(locator).toBeInViewport();
// Make sure element is fully outside of viewport.
await expect(locator).not.toBeInViewport();
// Make sure that at least half of the element intersects viewport.
await expect(locator).toBeInViewport();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures that Locator points to an attached and visible DOM node.
To check that at least one element from the list is visible, use locator.first().
Usage
// A specific element is visible.
await expect(page.getByText('Welcome')).toBeVisible();
// At least one item in the list is visible.
await expect(page.getByTestId('todo-item').first()).toBeVisible();
// At least one of the two elements is visible, possibly both.
await expect(
page.getByRole('button', { name: 'Sign in' })
.or(page.getByRole('button', { name: 'Sign up' }))
.first()
).toBeVisible();
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Optional
visible?: booleanEnsures the Locator points to an element that contains the given text. You can use regular expressions for the value as well.
Details
When expected
parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual
text and in the expected string before matching. When regular expression is used, the actual text is matched as is.
Usage
const locator = page.locator('.title');
await expect(locator).toContainText('substring');
await expect(locator).toContainText(/\d messages/);
If you pass an array as an expected value, the expectations are:
For example, consider the following list:
<ul>
<li>Item Text 1</li>
<li>Item Text 2</li>
<li>Item Text 3</li>
</ul>
Let's see how we can use the assertion:
// ✓ Contains the right items in the right order
await expect(page.locator('ul > li')).toContainText(['Text 1', 'Text 3']);
// ✖ Wrong order
await expect(page.locator('ul > li')).toContainText(['Text 3', 'Text 2']);
// ✖ No item contains this text
await expect(page.locator('ul > li')).toContainText(['Some 33']);
// ✖ Locator points to the outer list element, not to the list items
await expect(page.locator('ul')).toContainText(['Text 3']);
Expected substring or RegExp or a list of those.
Optional
options: { Optional
ignoreWhether to perform case-insensitive match. ignoreCase
option takes precedence over the corresponding regular
expression flag if specified.
Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an element with given attribute.
Usage
const locator = page.locator('input');
await expect(locator).toHaveAttribute('type', 'text');
Attribute name.
Expected attribute value.
Optional
options: { Optional
ignoreWhether to perform case-insensitive match. ignoreCase
option takes precedence over the corresponding regular
expression flag if specified.
Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an element with given attribute. The method will assert attribute presence.
const locator = page.locator('input');
// Assert attribute existence.
await expect(locator).toHaveAttribute('disabled');
await expect(locator).not.toHaveAttribute('open');
Attribute name.
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator resolves to an element with the given computed CSS style.
Usage
const locator = page.getByRole('button');
await expect(locator).toHaveCSS('display', 'flex');
CSS property name.
CSS property value.
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an element with given CSS classes. This needs to be a full match or using a relaxed regular expression.
Usage
<div class='selected row' id='component'></div>
const locator = page.locator('#component');
await expect(locator).toHaveClass(/selected/);
await expect(locator).toHaveClass('selected row');
Note that if array is passed as an expected value, entire lists of elements can be asserted:
const locator = page.locator('list > .component');
await expect(locator).toHaveClass(['component', 'component selected', 'component']);
Expected class or RegExp or a list of those.
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator resolves to an exact number of DOM nodes.
Usage
const list = page.locator('list > .component');
await expect(list).toHaveCount(3);
Expected count.
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an element with the given text. You can use regular expressions for the value as well.
Details
When expected
parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual
text and in the expected string before matching. When regular expression is used, the actual text is matched as is.
Usage
const locator = page.locator('.title');
await expect(locator).toHaveText(/Welcome, Test User/);
await expect(locator).toHaveText(/Welcome, .*/);
If you pass an array as an expected value, the expectations are:
For example, consider the following list:
<ul>
<li>Text 1</li>
<li>Text 2</li>
<li>Text 3</li>
</ul>
Let's see how we can use the assertion:
// ✓ Has the right items in the right order
await expect(page.locator('ul > li')).toHaveText(['Text 1', 'Text 2', 'Text 3']);
// ✖ Wrong order
await expect(page.locator('ul > li')).toHaveText(['Text 3', 'Text 2', 'Text 1']);
// ✖ Last item does not match
await expect(page.locator('ul > li')).toHaveText(['Text 1', 'Text 2', 'Text']);
// ✖ Locator points to the outer list element, not to the list items
await expect(page.locator('ul')).toHaveText(['Text 1', 'Text 2', 'Text 3']);
Expected string or RegExp or a list of those.
Optional
options: { Optional
ignoreWhether to perform case-insensitive match. ignoreCase
option takes precedence over the corresponding regular
expression flag if specified.
Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
Ensures the Locator points to an element with the given input value. You can use regular expressions for the value as well.
Usage
const locator = page.locator('input[type=number]');
await expect(locator).toHaveValue(/[0-9]/);
Expected value.
Optional
options: { Optional
timeout?: numberTime to retry the assertion for in milliseconds. Defaults to timeout
in TestConfig.expect
.
The LocatorAssertions class provides assertion methods that can be used to make assertions about the Locator state in the tests.