The [LocatorAssertions] class provides assertion methods that can be used to make assertions about the [Locator] state in the tests. A new instance of [LocatorAssertions] is created by calling [method: PlaywrightAssertions.expectLocator
]:
import { test, expect } from '@playwright/test';
test('status becomes submitted', async ({ page }) => {
// ...
await page.click('#submit-button');
await expect(page.locator('.status')).toHaveText('Submitted');
});
...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestLocator {
...
@Test
void statusBecomesSubmitted() {
...
page.click("#submit-button");
assertThat(page.locator(".status")).hasText("Submitted");
}
}
from playwright.async_api import Page, expect
async def test_status_becomes_submitted(page: Page) -> None:
# ..
await page.click("#submit-button")
await expect(page.locator(".status")).to_have_text("Submitted")
from playwright.sync_api import Page, expect
def test_status_becomes_submitted(page: Page) -> None:
# ..
page.click("#submit-button")
expect(page.locator(".status")).to_have_text("Submitted")
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
public class ExampleTests : PageTest
{
[Test]
public async Task StatusBecomesSubmitted()
{
// ..
await Page.ClickAsync("#submit-button");
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}
- langs: java, js, csharp
- returns: <[LocatorAssertions]>
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');
assertThat(locator).not().containsText("error");
await Expect(locator).Not.ToContainTextAsync("error");
- langs: python
The opposite of [method: LocatorAssertions.toBeChecked
].
- langs: python
The opposite of [method: LocatorAssertions.toBeDisabled
].
- langs: python
The opposite of [method: LocatorAssertions.toBeEditable
].
- langs: python
The opposite of [method: LocatorAssertions.toBeEmpty
].
- langs: python
The opposite of [method: LocatorAssertions.toBeEnabled
].
- langs: python
The opposite of [method: LocatorAssertions.toBeFocused
].
async method: LocatorAssertions.NotToBeHidden
- langs: python
The opposite of [method: LocatorAssertions.toBeHidden
].
option: LocatorAssertions.NotToBeHidden.timeout = %%-js-assertions-timeout-%%
option: LocatorAssertions.NotToBeHidden.timeout = %%-csharp-java-python-assertions-timeout-%%
- langs: python
The opposite of [method: LocatorAssertions.toBeVisible
].
- langs: python
The opposite of [method: LocatorAssertions.toContainText
].
expected
<[string]|[RegExp]|[Array]<[string]|[RegExp]>>
Expected substring or RegExp or a list of those.
useInnerText
<[boolean]>
Whether to use element.innerText
instead of element.textContent
when retrieving DOM node text.
- langs: python
The opposite of [method: LocatorAssertions.toHaveAttribute
].
name
<[string]>
Attribute name.
value
<[string]|[RegExp]>
Expected attribute value.
- langs: python
The opposite of [method: LocatorAssertions.toHaveClass
].
expected
<[string]|[RegExp]|[Array]<[string]|[RegExp]>>
Expected class or RegExp or a list of those.
- langs: python
The opposite of [method: LocatorAssertions.toHaveCount
].
count
<[int]>
Expected count.
- langs: python
The opposite of [method: LocatorAssertions.toHaveCSS
].
name
<[string]>
CSS property name.
value
<[string]|[RegExp]>
CSS property value.
- langs: python
The opposite of [method: LocatorAssertions.toHaveId
].
id
<[string]|[RegExp]>
Element id.
- langs: python
The opposite of [method: LocatorAssertions.toHaveJSProperty
].
name
<[string]>
Property name.
value
<[any]>
Property value.
- langs: python
The opposite of [method: LocatorAssertions.toHaveText
].
expected
<[string]|[RegExp]|[Array]<[string]|[RegExp]>>
Expected substring or RegExp or a list of those.
useInnerText
<[boolean]>
Whether to use element.innerText
instead of element.textContent
when retrieving DOM node text.
- langs: python
The opposite of [method: LocatorAssertions.toHaveValue
].
value
<[string]|[RegExp]>
Expected value.
- langs:
- alias-java: isChecked
Ensures the [Locator] points to a checked input.
const locator = page.locator('.subscribe');
await expect(locator).toBeChecked();
assertThat(page.locator(".subscribe")).isChecked();
from playwright.async_api import expect
locator = page.locator(".subscribe")
await expect(locator).to_be_checked()
from playwright.sync_api import expect
locator = page.locator(".subscribe")
expect(locator).to_be_checked()
var locator = Page.Locator(".subscribe");
await Expect(locator).ToBeCheckedAsync();
checked
<[boolean]>
- langs:
- alias-java: isDisabled
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.
const locator = page.locator('button.submit');
await expect(locator).toBeDisabled();
assertThat(page.locator("button.submit")).isDisabled();
from playwright.async_api import expect
locator = page.locator("button.submit")
await expect(locator).to_be_disabled()
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_disabled()
var locator = Page.Locator("button.submit");
await Expect(locator).ToBeDisabledAsync();
- langs:
- alias-java: isEditable
Ensures the [Locator] points to an editable element.
const locator = page.locator('input');
await expect(locator).toBeEditable();
assertThat(page.locator("input")).isEditable();
from playwright.async_api import expect
locator = page.locator(".input")
await expect(locator).to_be_editable()
from playwright.sync_api import expect
locator = page.locator(".input")
expect(locator).to_be_editable()
var locator = Page.Locator("input");
await Expect(locator).ToBeEditableAsync();
- langs:
- alias-java: isEmpty
Ensures the [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();
assertThat(page.locator("div.warning")).isEmpty();
from playwright.async_api import expect
locator = page.locator("div.warning")
await expect(locator).to_be_empty()
from playwright.sync_api import expect
locator = page.locator("div.warning")
expect(locator).to_be_empty()
var locator = Page.Locator("div.warning");
await Expect(locator).ToBeEmptyAsync();
- langs:
- alias-java: isEnabled
Ensures the [Locator] points to an enabled element.
const locator = page.locator('button.submit');
await expect(locator).toBeEnabled();
assertThat(page.locator("button.submit")).isEnabled();
from playwright.async_api import expect
locator = page.locator("button.submit")
await expect(locator).to_be_enabled()
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_enabled()
var locator = Page.Locator("button.submit");
await Expect(locator).toBeEnabledAsync();
- langs:
- alias-java: isFocused
Ensures the [Locator] points to a focused DOM node.
const locator = page.locator('input');
await expect(locator).toBeFocused();
assertThat(page.locator("input")).isFocused();
from playwright.async_api import expect
locator = page.locator('input')
await expect(locator).to_be_focused()
from playwright.sync_api import expect
locator = page.locator('input')
expect(locator).to_be_focused()
var locator = Page.Locator("input");
await Expect(locator).ToBeFocusedAsync();
async method: LocatorAssertions.toBeHidden
- langs:
- alias-java: isHidden
Ensures the [Locator] points to a hidden DOM node, which is the opposite of visible.
const locator = page.locator('.my-element');
await expect(locator).toBeHidden();
assertThat(page.locator(".my-element")).isHidden();
from playwright.async_api import expect
locator = page.locator('.my-element')
await expect(locator).to_be_hidden()
from playwright.sync_api import expect
locator = page.locator('.my-element')
expect(locator).to_be_hidden()
var locator = Page.Locator(".my-element");
await Expect(locator).ToBeHiddenAsync();
option: LocatorAssertions.toBeHidden.timeout = %%-js-assertions-timeout-%%
option: LocatorAssertions.toBeHidden.timeout = %%-csharp-java-python-assertions-timeout-%%
- langs:
- alias-java: isVisible
Ensures the [Locator] points to a visible DOM node.
const locator = page.locator('.my-element');
await expect(locator).toBeVisible();
assertThat(page.locator(".my-element")).isVisible();
from playwright.async_api import expect
locator = page.locator('.my-element')
await expect(locator).to_be_visible()
from playwright.sync_api import expect
locator = page.locator('.my-element')
expect(locator).to_be_visible()
var locator = Page.Locator(".my-element");
await Expect(locator).ToBeVisibleAsync();
- langs:
- alias-java: containsText
Ensures the [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/);
assertThat(page.locator(".title")).containsText("substring");
import re
from playwright.async_api import expect
locator = page.locator('.title')
await expect(locator).to_contain_text("substring")
await expect(locator).to_contain_text(re.compile(r"\d messages"))
import re
from playwright.sync_api import expect
locator = page.locator('.title')
expect(locator).to_contain_text("substring")
expect(locator).to_contain_text(re.compile(r"\d messages"))
var locator = Page.Locator(".title");
await Expect(locator).ToContainTextAsync("substring");
await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));
Note that if array is passed as an expected value, entire lists of elements can be asserted:
const locator = page.locator('list > .list-item');
await expect(locator).toContainText(['Text 1', 'Text 4', 'Text 5']);
assertThat(page.locator("list > .list-item")).containsText(new String[] {"Text 1", "Text 4", "Text 5"});
import re
from playwright.async_api import expect
locator = page.locator("list > .list-item")
await expect(locator).to_contain_text(["Text 1", "Text 4", "Text 5"])
import re
from playwright.sync_api import expect
locator = page.locator("list > .list-item")
expect(locator).to_contain_text(["Text 1", "Text 4", "Text 5"])
var locator = Page.Locator("list > .list-item");
await Expect(locator).ToContainTextAsync(new string[] { "Text 1", "Text 4", "Text 5" });
- langs: python, js
expected
<[string]|[RegExp]|[Array]<[string]|[RegExp]>>
Expected substring or RegExp or a list of those.
- langs: java, csharp
expected
<[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Expected substring or RegExp or a list of those.
useInnerText
<[boolean]>
Whether to use element.innerText
instead of element.textContent
when retrieving DOM node text.
- langs:
- alias-java: hasAttribute
Ensures the [Locator] points to an element with given attribute.
const locator = page.locator('input');
await expect(locator).toHaveAttribute('type', 'text');
assertThat(page.locator("input")).hasAttribute("type", "text");
from playwright.async_api import expect
locator = page.locator("input")
await expect(locator).to_have_attribute("type", "text")
from playwright.sync_api import expect
locator = page.locator("input")
expect(locator).to_have_attribute("type", "text")
var locator = Page.Locator("input");
await Expect(locator).ToHaveAttributeAsync("type", "text");
name
<[string]>
Attribute name.
value
<[string]|[RegExp]>
Expected attribute value.
- langs:
- alias-java: hasClass
Ensures the [Locator] points to an element with given CSS class.
const locator = page.locator('#component');
await expect(locator).toHaveClass(/selected/);
assertThat(page.locator("#component")).hasClass(Pattern.compile("selected"));
from playwright.async_api import expect
locator = page.locator("#component")
await expect(locator).to_have_class(re.compile(r"selected"))
from playwright.sync_api import expect
locator = page.locator("#component")
expect(locator).to_have_class(re.compile(r"selected"))
var locator = Page.Locator("#component");
await Expect(locator).ToHaveClassAsync(new Regex("selected"));
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']);
assertThat(page.locator("list > .component")).hasClass(new String[] {"component", "component selected", "component"});
from playwright.async_api import expect
locator = page.locator("list > .component")
await expect(locator).to_have_class(["component", "component selected", "component"])
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_class(["component", "component selected", "component"])
var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveClassAsync(new string[]{"component", "component selected", "component"});
- langs: python, js
expected
<[string]|[RegExp]|[Array]<[string]|[RegExp]>>
Expected class or RegExp or a list of those.
- langs: java, csharp
expected
<[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Expected class or RegExp or a list of those.
- langs:
- alias-java: hasCount
Ensures the [Locator] resolves to an exact number of DOM nodes.
const list = page.locator('list > .component');
await expect(list).toHaveCount(3);
assertThat(page.locator("list > .component")).hasCount(3);
from playwright.async_api import expect
locator = page.locator("list > .component")
await expect(locator).to_have_count(3)
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_count(3)
var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveCountAsync(3);
count
<[int]>
Expected count.
- langs:
- alias-java: hasCSS
Ensures the [Locator] resolves to an element with the given computed CSS style.
const locator = page.locator('button');
await expect(locator).toHaveCSS('display', 'flex');
assertThat(page.locator("button")).hasCSS("display", "flex");
from playwright.async_api import expect
locator = page.locator("button")
await expect(locator).to_have_css("display", "flex")
from playwright.sync_api import expect
locator = page.locator("button")
expect(locator).to_have_css("display", "flex")
var locator = Page.Locator("button");
await Expect(locator).ToHaveCSSAsync("display", "flex");
name
<[string]>
CSS property name.
value
<[string]|[RegExp]>
CSS property value.
- langs:
- alias-java: hasId
Ensures the [Locator] points to an element with the given DOM Node ID.
const locator = page.locator('input');
await expect(locator).toHaveId('lastname');
assertThat(page.locator("input")).hasId("lastname");
from playwright.async_api import expect
locator = page.locator("input")
await expect(locator).to_have_id("lastname")
from playwright.sync_api import expect
locator = page.locator("input")
expect(locator).to_have_id("lastname")
var locator = Page.Locator("input");
await Expect(locator).ToHaveIdAsync("lastname");
id
<[string]|[RegExp]>
Element id.
- langs:
- alias-java: hasJSProperty
Ensures the [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);
assertThat(page.locator("input")).hasJSProperty("loaded", true);
from playwright.async_api import expect
locator = page.locator(".component")
await expect(locator).to_have_js_property("loaded", True)
from playwright.sync_api import expect
locator = page.locator(".component")
expect(locator).to_have_js_property("loaded", True)
var locator = Page.Locator(".component");
await Expect(locator).ToHaveJSPropertyAsync("loaded", true);
name
<[string]>
Property name.
value
<[any]>
Property value.
- langs: js
Ensures that [Locator] resolves to a given screenshot. This function will re-take screenshots until it matches with the saved expectation.
If there's no expectation yet, it will wait until two consecutive screenshots yield the same result, and save the last one as an expectation.
const locator = page.locator('button');
await expect(locator).toHaveScreenshot('image.png');
name
<[string]|[Array]<[string]>>
Snapshot name.
option: LocatorAssertions.toHaveScreenshot#1.animations = %%-screenshot-option-animations-default-disabled-%%
option: LocatorAssertions.toHaveScreenshot#1.omitBackground = %%-screenshot-option-omit-background-%%
option: LocatorAssertions.toHaveScreenshot#1.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
- langs: js
Ensures that [Locator] resolves to a given screenshot. This function will re-take screenshots until it matches with the saved expectation.
If there's no expectation yet, it will wait until two consecutive screenshots yield the same result, and save the last one as an expectation.
const locator = page.locator('button');
await expect(locator).toHaveScreenshot();
option: LocatorAssertions.toHaveScreenshot#2.animations = %%-screenshot-option-animations-default-disabled-%%
option: LocatorAssertions.toHaveScreenshot#2.omitBackground = %%-screenshot-option-omit-background-%%
option: LocatorAssertions.toHaveScreenshot#2.maxDiffPixelRatio = %%-assertions-max-diff-pixel-ratio-%%
- langs:
- alias-java: hasText
Ensures the [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, .*/);
assertThat(page.locator(".title")).hasText("Welcome, Test User");
assertThat(page.locator(".title")).hasText(Pattern.compile("Welcome, .*"));
import re
from playwright.async_api import expect
locator = page.locator(".title")
await expect(locator).to_have_text(re.compile(r"Welcome, Test User"))
await expect(locator).to_have_text(re.compile(r"Welcome, .*"))
import re
from playwright.sync_api import expect
locator = page.locator(".title")
expect(locator).to_have_text(re.compile(r"Welcome, Test User"))
expect(locator).to_have_text(re.compile(r"Welcome, .*"))
var locator = Page.Locator(".title");
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, Test User"));
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, .*"));
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).toHaveText(['Text 1', 'Text 2', 'Text 3']);
assertThat(page.locator("list > .component")).hasText(new String[] {"Text 1", "Text 2", "Text 3"});
from playwright.async_api import expect
locator = page.locator("list > .component")
await expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"])
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"])
var locator = Page.Locator("list > .component");
await Expect(locator).toHaveTextAsync(new string[]{ "Text 1", "Text 2", "Text 3" });
- langs: python, js
expected
<[string]|[RegExp]|[Array]<[string]|[RegExp]>>
Expected substring or RegExp or a list of those.
- langs: java, csharp
expected
<[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Expected substring or RegExp or a list of those.
useInnerText
<[boolean]>
Whether to use element.innerText
instead of element.textContent
when retrieving DOM node text.
- langs:
- alias-java: hasValue
Ensures the [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]/);
assertThat(page.locator("input[type=number]")).hasValue(Pattern.compile("[0-9]"));
import re
from playwright.async_api import expect
locator = page.locator("input[type=number]")
await expect(locator).to_have_value(re.compile(r"[0-9]"))
import re
from playwright.sync_api import expect
locator = page.locator("input[type=number]")
expect(locator).to_have_value(re.compile(r"[0-9]"))
var locator = Page.Locator("input[type=number]");
await Expect(locator).ToHaveValueAsync(new Regex("[0-9]"));
value
<[string]|[RegExp]>
Expected value.