2021-10-22 16:56:58 -07:00
# class: LocatorAssertions
2021-11-24 21:58:35 +01:00
* langs: java, python, js
2021-10-22 16:56:58 -07:00
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")
```
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.not
2021-11-24 21:58:35 +01:00
* langs: java, js
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
```
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToBeChecked
* 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-%%
## method: LocatorAssertions.NotToBeDisabled
* langs: python
The opposite of [`method: LocatorAssertions.toBeDisabled` ].
### option: LocatorAssertions.NotToBeDisabled.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeEditable
* langs: python
The opposite of [`method: LocatorAssertions.toBeEditable` ].
### option: LocatorAssertions.NotToBeEditable.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeEmpty
* langs: python
The opposite of [`method: LocatorAssertions.toBeEmpty` ].
### option: LocatorAssertions.NotToBeEmpty.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeEnabled
* langs: python
The opposite of [`method: LocatorAssertions.toBeEnabled` ].
### option: LocatorAssertions.NotToBeEnabled.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeFocused
* langs: python
The opposite of [`method: LocatorAssertions.toBeFocused` ].
### option: LocatorAssertions.NotToBeFocused.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeHidden
* langs: python
The opposite of [`method: LocatorAssertions.toBeHidden` ].
### option: LocatorAssertions.NotToBeHidden.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToBeVisible
* langs: python
The opposite of [`method: LocatorAssertions.toBeVisible` ].
### option: LocatorAssertions.NotToBeVisible.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToContainText
* 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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveAttribute
* 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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveClass
* 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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveCount
* 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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveCSS
* 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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveId
* 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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveJSProperty
* 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
2021-10-22 16:56:58 -07:00
- `value` < [Serializable]>
Property value.
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.NotToHaveJSProperty.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveText
* 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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.NotToHaveValue
* 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
## method: LocatorAssertions.toBeChecked
* 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()
```
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
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.toBeDisabled
* 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()
```
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
## method: LocatorAssertions.toBeEditable
* 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()
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeEditable.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.toBeEmpty
* 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()
```
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
## method: LocatorAssertions.toBeEnabled
* 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()
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeEnabled.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.toBeFocused
* 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()
```
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
## method: LocatorAssertions.toBeHidden
* 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()
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeHidden.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.toBeVisible
* 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()
```
2021-11-18 00:46:30 +01:00
### option: LocatorAssertions.toBeVisible.timeout = %%-assertions-timeout-%%
2021-10-22 16:56:58 -07:00
2021-11-18 00:46:30 +01:00
## method: LocatorAssertions.toContainText
* 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"))
```
2021-11-18 00:46:30 +01:00
Note that if array is passed as an expected value, entire lists can be asserted:
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"])
```
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
* langs: java
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-%%
## method: LocatorAssertions.toHaveAttribute
* 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")
```
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-%%
## method: LocatorAssertions.toHaveClass
* 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"))
```
2021-11-18 00:46:30 +01:00
Note that if array is passed as an expected value, entire lists can be asserted:
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"])
```
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
* langs: java
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-%%
## method: LocatorAssertions.toHaveCount
* 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)
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveCount.count
- `count` < [int]>
Expected count.
### option: LocatorAssertions.toHaveCount.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveCSS
* 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")
```
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-%%
## method: LocatorAssertions.toHaveId
* 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")
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveId.id
- `id` < [string]|[RegExp]>
Element id.
### option: LocatorAssertions.toHaveId.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveJSProperty
* 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
```
### param: LocatorAssertions.toHaveJSProperty.name
- `name` < [string]>
Property name.
### param: LocatorAssertions.toHaveJSProperty.value
- `value` < [Serializable]>
Property value.
### option: LocatorAssertions.toHaveJSProperty.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveText
* 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, .*"))
```
2021-11-18 00:46:30 +01:00
Note that if array is passed as an expected value, entire lists can be asserted:
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"])
```
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
* langs: java
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-%%
## method: LocatorAssertions.toHaveValue
* 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]"))
```
2021-11-18 00:46:30 +01:00
### param: LocatorAssertions.toHaveValue.value
- `value` < [string]|[RegExp]>
Expected value.
### option: LocatorAssertions.toHaveValue.timeout = %%-assertions-timeout-%%