2021-10-22 16:56:58 -07:00
# class: LocatorAssertions
2021-11-24 21:58:35 +01:00
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` ]:
2021-11-09 12:44:02 -08:00
2021-11-30 20:04:44 +01:00
```js
import { test, expect } from '@playwright/test ';
test('status becomes submitted', async ({ page }) => {
// ...
await page.click('#submit -button');
await expect(page.locator('.status')).toHaveText('Submitted');
});
```
2021-11-09 12:44:02 -08:00
```java
...
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestLocator {
...
@Test
void statusBecomesSubmitted() {
...
page.click("#submit -button");
assertThat(page.locator(".status")).hasText("Submitted");
}
}
```
2021-10-22 16:56:58 -07:00
2021-11-30 20:04:44 +01:00
```python async
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")
```
```python sync
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")
```
2022-02-21 14:01:53 +01:00
```csharp
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
using static Microsoft.Playwright.Assertions;
namespace PlaywrightTests
{
public class ExampleTests : PageTest
{
[Test]
public async Task StatusBecomesSubmitted()
{
// ..
await Page.ClickAsync("#submit -button");
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}
}
```
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.not
2022-02-21 14:01:53 +01:00
* langs: java, js, csharp
2021-11-18 00:46:30 +01:00
- returns: < [LocatorAssertions]>
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
Makes the assertion check for the opposite condition. For example, this code tests that the Locator doesn't contain text `"error"` :
2021-10-22 16:56:58 -07:00
2021-11-30 20:04:44 +01:00
```js
await expect(locator).not.toContainText('error');
```
2021-10-22 16:56:58 -07:00
```java
2021-11-18 00:46:30 +01:00
assertThat(locator).not().containsText("error");
2021-10-22 16:56:58 -07:00
```
2022-02-21 14:01:53 +01:00
```csharp
await Expect(locator).Not.ToContainTextAsync("error");
```
## async method: LocatorAssertions.NotToBeChecked
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toBeChecked` ].
### option: LocatorAssertions.NotToBeChecked.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToBeDisabled
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toBeDisabled` ].
### option: LocatorAssertions.NotToBeDisabled.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToBeEditable
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toBeEditable` ].
### option: LocatorAssertions.NotToBeEditable.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToBeEmpty
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toBeEmpty` ].
### option: LocatorAssertions.NotToBeEmpty.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToBeEnabled
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toBeEnabled` ].
### option: LocatorAssertions.NotToBeEnabled.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToBeFocused
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toBeFocused` ].
### option: LocatorAssertions.NotToBeFocused.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToBeHidden
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toBeHidden` ].
### option: LocatorAssertions.NotToBeHidden.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToBeVisible
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toBeVisible` ].
### option: LocatorAssertions.NotToBeVisible.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToContainText
2021-11-18 00:46:30 +01:00
* langs: python
The opposite of [`method: LocatorAssertions.toContainText` ].
### param: LocatorAssertions.NotToContainText.expected
2021-11-22 18:39:10 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]|[RegExp]>>
2021-10-22 16:56:58 -07:00
Expected substring or RegExp or a list of those.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToContainText.useInnerText
2021-10-22 16:56:58 -07:00
- `useInnerText` < [boolean]>
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToContainText.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveAttribute
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveAttribute` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveAttribute.name
2021-10-22 16:56:58 -07:00
- `name` < [string]>
Attribute name.
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveAttribute.value
2021-10-22 16:56:58 -07:00
- `value` < [string]|[RegExp]>
Expected attribute value.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveAttribute.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveClass
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveClass` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveClass.expected
2021-11-22 18:39:10 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]|[RegExp]>>
2021-10-22 16:56:58 -07:00
Expected class or RegExp or a list of those.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveClass.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveCount
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveCount` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveCount.count
2021-10-22 16:56:58 -07:00
- `count` < [int]>
Expected count.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveCount.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveCSS
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveCSS` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveCSS.name
2021-10-22 16:56:58 -07:00
- `name` < [string]>
CSS property name.
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveCSS.value
2021-10-22 16:56:58 -07:00
- `value` < [string]|[RegExp]>
CSS property value.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveCSS.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveId
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveId` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveId.id
- `id` < [string]|[RegExp]>
2021-10-22 16:56:58 -07:00
Element id.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveId.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveJSProperty
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveJSProperty` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveJSProperty.name
2021-10-22 16:56:58 -07:00
- `name` < [string]>
Property name.
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveJSProperty.value
2022-02-21 14:01:53 +01:00
- `value` < [any]>
2021-10-22 16:56:58 -07:00
Property value.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveJSProperty.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveText
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveText` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveText.expected
2021-11-22 18:39:10 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]|[RegExp]>>
2021-10-22 16:56:58 -07:00
Expected substring or RegExp or a list of those.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveText.useInnerText
2021-10-22 16:56:58 -07:00
- `useInnerText` < [boolean]>
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveText.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.NotToHaveValue
2021-11-18 00:46:30 +01:00
* langs: python
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
The opposite of [`method: LocatorAssertions.toHaveValue` ].
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.NotToHaveValue.value
2021-10-22 16:56:58 -07:00
- `value` < [string]|[RegExp]>
Expected value.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveValue.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeChecked
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isChecked
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to a checked input.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('.subscribe');
await expect(locator).toBeChecked();
```
2021-10-22 16:56:58 -07:00
```java
assertThat(page.locator(".subscribe")).isChecked();
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator(".subscribe")
await expect(locator).to_be_checked()
```
```python sync
from playwright.sync_api import expect
locator = page.locator(".subscribe")
expect(locator).to_be_checked()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator(".subscribe");
await Expect(locator).ToBeCheckedAsync();
```
2021-12-02 10:31:26 -08:00
### option: LocatorAssertions.toBeChecked.checked
- `checked` < [boolean]>
2021-11-18 00:46:30 +01:00
2021-12-02 10:31:26 -08:00
### option: LocatorAssertions.toBeChecked.timeout = %%-assertions-timeout-%%
2021-11-18 00:46:30 +01:00
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeDisabled
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isDisabled
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to a disabled element.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('button.submit');
await expect(locator).toBeDisabled();
```
2021-10-22 16:56:58 -07:00
```java
assertThat(page.locator("button.submit")).isDisabled();
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("button.submit")
await expect(locator).to_be_disabled()
```
```python sync
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_disabled()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("button.submit");
await Expect(locator).ToBeDisabledAsync();
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeDisabled.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeEditable
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isEditable
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to an editable element.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('input');
await expect(locator).toBeEditable();
```
2021-10-22 16:56:58 -07:00
```java
assertThat(page.locator("input")).isEditable();
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator(".input")
await expect(locator).to_be_editable()
```
```python sync
from playwright.sync_api import expect
locator = page.locator(".input")
expect(locator).to_be_editable()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToBeEditableAsync();
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeEditable.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeEmpty
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isEmpty
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to an empty editable element or to a DOM node that has no text.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('div.warning');
await expect(locator).toBeEmpty();
```
2021-10-22 16:56:58 -07:00
```java
assertThat(page.locator("div.warning")).isEmpty();
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("div.warning")
await expect(locator).to_be_empty()
```
```python sync
from playwright.sync_api import expect
locator = page.locator("div.warning")
expect(locator).to_be_empty()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("div.warning");
await Expect(locator).ToBeEmptyAsync();
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeEmpty.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeEnabled
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isEnabled
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to an enabled element.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('button.submit');
await expect(locator).toBeEnabled();
```
2021-10-22 16:56:58 -07:00
```java
assertThat(page.locator("button.submit")).isEnabled();
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("button.submit")
await expect(locator).to_be_enabled()
```
```python sync
from playwright.sync_api import expect
locator = page.locator("button.submit")
expect(locator).to_be_enabled()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("button.submit");
await Expect(locator).toBeEnabledAsync();
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeEnabled.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeFocused
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isFocused
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to a focused DOM node.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('input');
await expect(locator).toBeFocused();
```
2021-10-22 16:56:58 -07:00
```java
assertThat(page.locator("input")).isFocused();
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator('input')
await expect(locator).to_be_focused()
```
```python sync
from playwright.sync_api import expect
locator = page.locator('input')
expect(locator).to_be_focused()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToBeFocusedAsync();
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeFocused.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeHidden
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isHidden
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to a hidden DOM node, which is the opposite of [visible ](./actionability.md#visible ).
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('.my-element');
await expect(locator).toBeHidden();
```
2021-10-22 16:56:58 -07:00
```java
assertThat(page.locator(".my-element")).isHidden();
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator('.my-element')
await expect(locator).to_be_hidden()
```
```python sync
from playwright.sync_api import expect
locator = page.locator('.my-element')
expect(locator).to_be_hidden()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator(".my-element");
await Expect(locator).ToBeHiddenAsync();
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeHidden.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toBeVisible
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: isVisible
2021-10-22 16:56:58 -07:00
Ensures the [Locator] points to a [visible ](./actionability.md#visible ) DOM node.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('.my-element');
await expect(locator).toBeVisible();
```
2021-10-22 16:56:58 -07:00
```java
2021-11-18 00:46:30 +01:00
assertThat(page.locator(".my-element")).toBeVisible();
2021-10-22 16:56:58 -07:00
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator('.my-element')
await expect(locator).to_be_visible()
```
```python sync
from playwright.sync_api import expect
locator = page.locator('.my-element')
expect(locator).to_be_visible()
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator(".my-element");
await Expect(locator).ToBeVisibleAsync();
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeVisible.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toContainText
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: containsText
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
Ensures the [Locator] points to an element that contains the given text. You can use regular expressions for the value as well.
2021-10-22 16:56:58 -07:00
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('.title');
await expect(locator).toContainText('substring');
await expect(locator).toContainText(/\d messages/);
```
2021-10-22 16:56:58 -07:00
```java
2021-11-18 00:46:30 +01:00
assertThat(page.locator(".title")).containsText("substring");
2021-10-22 16:56:58 -07:00
```
2021-11-18 00:46:30 +01:00
2021-11-30 20:04:44 +01:00
```python async
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"))
```
```python sync
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"))
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator(".title");
await Expect(locator).ToContainTextAsync("substring");
await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));
```
2022-01-07 09:13:46 -08:00
Note that if array is passed as an expected value, entire lists of elements can be asserted:
2021-11-18 00:46:30 +01:00
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('list > .list-item');
await expect(locator).toContainText(['Text 1', 'Text 4', 'Text 5']);
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("list > .list-item")).containsText(new String[] {"Text 1", "Text 4", "Text 5"});
```
2021-11-30 20:04:44 +01:00
```python async
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"])
```
```python sync
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"])
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("list > .list-item");
await Expect(locator).ToContainTextAsync(new string[] { "Text 1", "Text 4", "Text 5" });
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toContainText.expected
2021-11-24 21:58:35 +01:00
* langs: python, js
2021-11-22 18:39:10 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]|[RegExp]>>
Expected substring or RegExp or a list of those.
### param: LocatorAssertions.toContainText.expected
2022-02-21 14:01:53 +01:00
* langs: java, csharp
2021-11-18 00:46:30 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]>|[Array]< [RegExp]>>
Expected substring or RegExp or a list of those.
### option: LocatorAssertions.toContainText.useInnerText
- `useInnerText` < [boolean]>
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
### option: LocatorAssertions.toContainText.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveAttribute
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasAttribute
Ensures the [Locator] points to an element with given attribute.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('input');
await expect(locator).toHaveAttribute('type', 'text');
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("input")).hasAttribute("type", "text");
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("input")
await expect(locator).to_have_attribute("type", "text")
```
```python sync
from playwright.sync_api import expect
locator = page.locator("input")
expect(locator).to_have_attribute("type", "text")
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToHaveAttributeAsync("type", "text");
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveAttribute.name
- `name` < [string]>
Attribute name.
### param: LocatorAssertions.toHaveAttribute.value
- `value` < [string]|[RegExp]>
Expected attribute value.
### option: LocatorAssertions.toHaveAttribute.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveClass
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasClass
Ensures the [Locator] points to an element with given CSS class.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('#component ');
await expect(locator).toHaveClass(/selected/);
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("#component ")).hasClass(Pattern.compile("selected"));
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("#component ")
await expect(locator).to_have_class(re.compile(r"selected"))
```
```python sync
from playwright.sync_api import expect
locator = page.locator("#component ")
expect(locator).to_have_class(re.compile(r"selected"))
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("#component ");
await Expect(locator).ToHaveClassAsync(new Regex("selected"));
```
2022-01-07 09:13:46 -08:00
Note that if array is passed as an expected value, entire lists of elements can be asserted:
2021-11-18 00:46:30 +01:00
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('list > .component');
await expect(locator).toHaveClass(['component', 'component selected', 'component']);
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("list > .component")).hasClass(new String[] {"component", "component selected", "component"});
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("list > .component")
await expect(locator).to_have_class(["component", "component selected", "component"])
```
```python sync
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_class(["component", "component selected", "component"])
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveClassAsync(new string[]{"component", "component selected", "component"});
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveClass.expected
2021-11-24 21:58:35 +01:00
* langs: python, js
2021-11-22 18:39:10 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]|[RegExp]>>
Expected class or RegExp or a list of those.
### param: LocatorAssertions.toHaveClass.expected
2022-02-21 14:01:53 +01:00
* langs: java, csharp
2021-11-18 00:46:30 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]>|[Array]< [RegExp]>>
Expected class or RegExp or a list of those.
### option: LocatorAssertions.toHaveClass.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveCount
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasCount
Ensures the [Locator] resolves to an exact number of DOM nodes.
2021-11-24 21:58:35 +01:00
```js
const list = page.locator('list > .component');
await expect(list).toHaveCount(3);
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("list > .component")).hasCount(3);
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("list > .component")
await expect(locator).to_have_count(3)
```
```python sync
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_count(3)
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveCountAsync(3);
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveCount.count
- `count` < [int]>
Expected count.
### option: LocatorAssertions.toHaveCount.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveCSS
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasCSS
Ensures the [Locator] resolves to an element with the given computed CSS style.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('button');
await expect(locator).toHaveCSS('display', 'flex');
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("button")).hasCSS("display", "flex");
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("button")
await expect(locator).to_have_css("display", "flex")
```
```python sync
from playwright.sync_api import expect
locator = page.locator("button")
expect(locator).to_have_css("display", "flex")
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("button");
await Expect(locator).ToHaveCSSAsync("display", "flex");
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveCSS.name
- `name` < [string]>
CSS property name.
### param: LocatorAssertions.toHaveCSS.value
- `value` < [string]|[RegExp]>
CSS property value.
### option: LocatorAssertions.toHaveCSS.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveId
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasId
Ensures the [Locator] points to an element with the given DOM Node ID.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('input');
await expect(locator).toHaveId('lastname');
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("input")).hasId("lastname");
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("input")
await expect(locator).to_have_id("lastname")
```
```python sync
from playwright.sync_api import expect
locator = page.locator("input")
expect(locator).to_have_id("lastname")
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToHaveIdAsync("lastname");
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveId.id
- `id` < [string]|[RegExp]>
Element id.
### option: LocatorAssertions.toHaveId.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveJSProperty
2021-11-18 00:46:30 +01:00
* langs:
- alias-java: hasJSProperty
2021-11-24 21:58:35 +01:00
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.
```js
const locator = page.locator('.component');
await expect(locator).toHaveJSProperty('loaded', true);
```
2021-11-18 00:46:30 +01:00
```java
2021-11-30 20:04:44 +01:00
assertThat(page.locator("input")).hasJSProperty("loaded", true);
```
```python async
from playwright.async_api import expect
locator = page.locator(".component")
await expect(locator).to_have_js_property("loaded", True)
```
```python sync
from playwright.sync_api import expect
locator = page.locator(".component")
expect(locator).to_have_js_property("loaded", True)
2021-11-18 00:46:30 +01:00
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator(".component");
await Expect(locator).ToHaveJSPropertyAsync("loaded", true);
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveJSProperty.name
- `name` < [string]>
Property name.
### param: LocatorAssertions.toHaveJSProperty.value
2022-02-21 14:01:53 +01:00
- `value` < [any]>
2021-11-18 00:46:30 +01:00
Property value.
### option: LocatorAssertions.toHaveJSProperty.timeout = %%-assertions-timeout-%%
2022-02-28 13:25:59 -07:00
## async method: LocatorAssertions.toHaveScreenshot
* 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.
```js
const locator = page.locator('button');
await expect(locator).toHaveScreenshot();
```
### option: LocatorAssertions.toHaveScreenshot.timeout = %%-assertions-timeout-%%
### option: LocatorAssertions.toHaveScreenshot.disableAnimations = %%-screenshot-option-disable-animations-%%
### option: LocatorAssertions.toHaveScreenshot.omitBackground = %%-screenshot-option-omit-background-%%
### option: LocatorAssertions.toHaveScreenshot.mask = %%-screenshot-option-mask-%%
### option: LocatorAssertions.toHaveScreenshot.pixelCount = %%-assertions-pixel-count-%%
### option: LocatorAssertions.toHaveScreenshot.pixelRatio = %%-assertions-pixel-ratio-%%
### option: LocatorAssertions.toHaveScreenshot.threshold = %%-assertions-threshold-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveText
2021-11-18 00:46:30 +01:00
* 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.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('.title');
await expect(locator).toHaveText(/Welcome, Test User/);
await expect(locator).toHaveText(/Welcome, .*/);
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator(".title")).hasText("Welcome, Test User");
assertThat(page.locator(".title")).hasText(Pattern.compile("Welcome, .*"));
```
2021-11-30 20:04:44 +01:00
```python async
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, .*"))
```
```python sync
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, .*"))
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator(".title");
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, Test User"));
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, .*"));
```
2022-01-07 09:13:46 -08:00
Note that if array is passed as an expected value, entire lists of elements can be asserted:
2021-11-18 00:46:30 +01:00
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('list > .component');
await expect(locator).toHaveText(['Text 1', 'Text 2', 'Text 3']);
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("list > .component")).hasText(new String[] {"Text 1", "Text 2", "Text 3"});
```
2021-11-30 20:04:44 +01:00
```python async
from playwright.async_api import expect
locator = page.locator("list > .component")
await expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"])
```
```python sync
from playwright.sync_api import expect
locator = page.locator("list > .component")
expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"])
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("list > .component");
await Expect(locator).toHaveTextAsync(new string[]{ "Text 1", "Text 2", "Text 3" });
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveText.expected
2021-11-24 21:58:35 +01:00
* langs: python, js
2021-11-22 18:39:10 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]|[RegExp]>>
Expected substring or RegExp or a list of those.
### param: LocatorAssertions.toHaveText.expected
2022-02-21 14:01:53 +01:00
* langs: java, csharp
2021-11-18 00:46:30 +01:00
- `expected` < [string]|[RegExp]|[Array]< [string]>|[Array]< [RegExp]>>
Expected substring or RegExp or a list of those.
### option: LocatorAssertions.toHaveText.useInnerText
- `useInnerText` < [boolean]>
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
### option: LocatorAssertions.toHaveText.timeout = %%-assertions-timeout-%%
2022-02-21 14:01:53 +01:00
## async method: LocatorAssertions.toHaveValue
2021-11-18 00:46:30 +01:00
* 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.
2021-11-24 21:58:35 +01:00
```js
const locator = page.locator('input[type=number]');
await expect(locator).toHaveValue(/[0-9]/);
```
2021-11-18 00:46:30 +01:00
```java
assertThat(page.locator("input[type=number]")).hasValue(Pattern.compile("[0-9]"));
```
2021-11-30 20:04:44 +01:00
```python async
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]"))
```
```python sync
import re
from playwright.sync_api import expect
locator = page.locator("input[type=number]")
expect(locator).to_have_value(re.compile(r"[0-9]"))
```
2022-02-21 14:01:53 +01:00
```csharp
var locator = Page.Locator("input[type=number]");
await Expect(locator).ToHaveValueAsync(new Regex("[0-9]"));
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveValue.value
- `value` < [string]|[RegExp]>
Expected value.
### option: LocatorAssertions.toHaveValue.timeout = %%-assertions-timeout-%%