2022-11-24 08:25:24 -08:00
|
|
|
---
|
|
|
|
id: test-assertions
|
|
|
|
title: "Assertions"
|
|
|
|
---
|
|
|
|
|
|
|
|
## List of assertions
|
|
|
|
|
|
|
|
| Assertion | Description |
|
|
|
|
| :- | :- |
|
2024-01-12 09:33:42 -08:00
|
|
|
| [`method: LocatorAssertions.toBeAttached`] | Element is attached |
|
2022-11-24 08:25:24 -08:00
|
|
|
| [`method: LocatorAssertions.toBeChecked`] | Checkbox is checked |
|
|
|
|
| [`method: LocatorAssertions.toBeDisabled`] | Element is disabled |
|
2023-04-10 21:11:19 +00:00
|
|
|
| [`method: LocatorAssertions.toBeEditable`] | Element is editable |
|
2022-11-24 08:25:24 -08:00
|
|
|
| [`method: LocatorAssertions.toBeEmpty`] | Container is empty |
|
|
|
|
| [`method: LocatorAssertions.toBeEnabled`] | Element is enabled |
|
|
|
|
| [`method: LocatorAssertions.toBeFocused`] | Element is focused |
|
|
|
|
| [`method: LocatorAssertions.toBeHidden`] | Element is not visible |
|
2024-01-12 09:33:42 -08:00
|
|
|
| [`method: LocatorAssertions.toBeInViewport`] | Element intersects viewport |
|
2022-11-24 08:25:24 -08:00
|
|
|
| [`method: LocatorAssertions.toBeVisible`] | Element is visible |
|
|
|
|
| [`method: LocatorAssertions.toContainText`] | Element contains text |
|
|
|
|
| [`method: LocatorAssertions.toHaveAttribute`] | Element has a DOM attribute |
|
|
|
|
| [`method: LocatorAssertions.toHaveClass`] | Element has a class property |
|
|
|
|
| [`method: LocatorAssertions.toHaveCount`] | List has exact number of children |
|
|
|
|
| [`method: LocatorAssertions.toHaveCSS`] | Element has CSS property |
|
|
|
|
| [`method: LocatorAssertions.toHaveId`] | Element has an ID |
|
|
|
|
| [`method: LocatorAssertions.toHaveJSProperty`] | Element has a JavaScript property |
|
|
|
|
| [`method: LocatorAssertions.toHaveText`] | Element matches text |
|
|
|
|
| [`method: LocatorAssertions.toHaveValue`] | Input has a value |
|
|
|
|
| [`method: LocatorAssertions.toHaveValues`] | Select has options selected |
|
|
|
|
| [`method: PageAssertions.toHaveTitle`] | Page has a title |
|
|
|
|
| [`method: PageAssertions.toHaveURL`] | Page has a URL |
|
|
|
|
| [`method: APIResponseAssertions.toBeOK`] | Response has an OK status |
|
2023-02-23 13:25:00 +01:00
|
|
|
|
|
|
|
## Custom Expect Message
|
|
|
|
* langs: python
|
|
|
|
|
|
|
|
You can specify a custom error message as a second argument to the `expect` function, for example:
|
|
|
|
|
|
|
|
```python
|
|
|
|
expect(page.get_by_text("Name"), "should be logged in").to_be_visible()
|
|
|
|
```
|
|
|
|
|
|
|
|
The error would look like this:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
def test_foobar(page: Page) -> None:
|
|
|
|
> expect(page.get_by_text("Name"), "should be logged in").to_be_visible()
|
|
|
|
E AssertionError: should be logged in
|
2024-01-12 09:33:42 -08:00
|
|
|
E Actual value: None
|
2023-02-23 13:25:00 +01:00
|
|
|
E Call log:
|
|
|
|
E LocatorAssertions.to_be_visible with timeout 5000ms
|
|
|
|
E waiting for get_by_text("Name")
|
|
|
|
E waiting for get_by_text("Name")
|
|
|
|
|
|
|
|
tests/test_foobar.py:22: AssertionError
|
|
|
|
```
|
2023-05-18 02:51:36 +02:00
|
|
|
|
|
|
|
## Setting a custom timeout
|
2024-01-31 17:49:01 +01:00
|
|
|
* langs: python, csharp
|
2023-05-18 02:51:36 +02:00
|
|
|
|
|
|
|
You can specify a custom timeout for assertions either globally or per assertion. The default timeout is 5 seconds.
|
|
|
|
|
|
|
|
### Global timeout
|
2024-01-31 17:49:01 +01:00
|
|
|
* langs: python
|
2023-05-18 02:51:36 +02:00
|
|
|
|
|
|
|
```python title="conftest.py"
|
|
|
|
from playwright.sync_api import expect
|
|
|
|
|
|
|
|
expect.set_options(timeout=10_000)
|
|
|
|
```
|
|
|
|
|
2024-01-31 17:49:01 +01:00
|
|
|
### Global timeout
|
|
|
|
* langs: csharp
|
|
|
|
|
|
|
|
<Tabs
|
|
|
|
groupId="test-runners"
|
|
|
|
defaultValue="nunit"
|
|
|
|
values={[
|
|
|
|
{label: 'NUnit', value: 'nunit'},
|
|
|
|
{label: 'MSTest', value: 'mstest'}
|
|
|
|
]
|
|
|
|
}>
|
|
|
|
<TabItem value="nunit">
|
|
|
|
|
|
|
|
```csharp title="UnitTest1.cs"
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
using Microsoft.Playwright.NUnit;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace PlaywrightTests;
|
|
|
|
|
|
|
|
[Parallelizable(ParallelScope.Self)]
|
|
|
|
[TestFixture]
|
|
|
|
public class Tests : PageTest
|
|
|
|
{
|
|
|
|
[OneTimeSetUp]
|
|
|
|
public void GlobalSetup()
|
|
|
|
{
|
|
|
|
SetDefaultExpectTimeout(10_000);
|
|
|
|
}
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</TabItem>
|
|
|
|
<TabItem value="mstest">
|
|
|
|
|
|
|
|
```csharp title="UnitTest1.cs"
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
using Microsoft.Playwright.MSTest;
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
|
|
|
namespace PlaywrightTests;
|
|
|
|
|
|
|
|
[TestClass]
|
|
|
|
public class UnitTest1 : PageTest
|
|
|
|
{
|
|
|
|
[ClassInitialize]
|
|
|
|
public static void GlobalSetup(TestContext context)
|
|
|
|
{
|
|
|
|
SetDefaultExpectTimeout(10_000);
|
|
|
|
}
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</TabItem>
|
|
|
|
</Tabs>
|
|
|
|
|
2023-05-18 02:51:36 +02:00
|
|
|
### Per assertion timeout
|
|
|
|
|
|
|
|
```python title="test_foobar.py"
|
|
|
|
from playwright.sync_api import expect
|
|
|
|
|
|
|
|
def test_foobar(page: Page) -> None:
|
|
|
|
expect(page.get_by_text("Name")).to_be_visible(timeout=10_000)
|
|
|
|
```
|
2024-01-31 17:49:01 +01:00
|
|
|
|
|
|
|
```csharp title="UnitTest1.cs"
|
|
|
|
await Expect(Page.GetByText("Name")).ToBeVisibleAsync(new() { Timeout = 10_000 });
|
|
|
|
```
|