docs(dotnet): enable web-first assertions (#12113)

This commit is contained in:
Max Schmitt 2022-02-21 14:01:53 +01:00 committed by GitHub
parent 29a2354df8
commit 768242db12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 243 additions and 51 deletions

View file

@ -1,5 +1,4 @@
# class: LocatorAssertions # class: LocatorAssertions
* langs: java, python, js
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`]: 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`]:
@ -46,8 +45,31 @@ def test_status_becomes_submitted(page: Page) -> None:
expect(page.locator(".status")).to_have_text("Submitted") expect(page.locator(".status")).to_have_text("Submitted")
``` ```
```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");
}
}
}
```
## method: LocatorAssertions.not ## method: LocatorAssertions.not
* langs: java, js * langs: java, js, csharp
- returns: <[LocatorAssertions]> - returns: <[LocatorAssertions]>
Makes the assertion check for the opposite condition. For example, this code tests that the Locator doesn't contain text `"error"`: Makes the assertion check for the opposite condition. For example, this code tests that the Locator doesn't contain text `"error"`:
@ -60,56 +82,60 @@ await expect(locator).not.toContainText('error');
assertThat(locator).not().containsText("error"); assertThat(locator).not().containsText("error");
``` ```
## method: LocatorAssertions.NotToBeChecked ```csharp
await Expect(locator).Not.ToContainTextAsync("error");
```
## async method: LocatorAssertions.NotToBeChecked
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeChecked`]. The opposite of [`method: LocatorAssertions.toBeChecked`].
### option: LocatorAssertions.NotToBeChecked.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeChecked.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeDisabled ## async method: LocatorAssertions.NotToBeDisabled
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeDisabled`]. The opposite of [`method: LocatorAssertions.toBeDisabled`].
### option: LocatorAssertions.NotToBeDisabled.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeDisabled.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeEditable ## async method: LocatorAssertions.NotToBeEditable
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeEditable`]. The opposite of [`method: LocatorAssertions.toBeEditable`].
### option: LocatorAssertions.NotToBeEditable.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeEditable.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeEmpty ## async method: LocatorAssertions.NotToBeEmpty
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeEmpty`]. The opposite of [`method: LocatorAssertions.toBeEmpty`].
### option: LocatorAssertions.NotToBeEmpty.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeEmpty.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeEnabled ## async method: LocatorAssertions.NotToBeEnabled
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeEnabled`]. The opposite of [`method: LocatorAssertions.toBeEnabled`].
### option: LocatorAssertions.NotToBeEnabled.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeEnabled.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeFocused ## async method: LocatorAssertions.NotToBeFocused
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeFocused`]. The opposite of [`method: LocatorAssertions.toBeFocused`].
### option: LocatorAssertions.NotToBeFocused.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeFocused.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeHidden ## async method: LocatorAssertions.NotToBeHidden
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeHidden`]. The opposite of [`method: LocatorAssertions.toBeHidden`].
### option: LocatorAssertions.NotToBeHidden.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeHidden.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToBeVisible ## async method: LocatorAssertions.NotToBeVisible
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toBeVisible`]. The opposite of [`method: LocatorAssertions.toBeVisible`].
@ -117,7 +143,7 @@ The opposite of [`method: LocatorAssertions.toBeVisible`].
### option: LocatorAssertions.NotToBeVisible.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToBeVisible.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToContainText ## async method: LocatorAssertions.NotToContainText
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toContainText`]. The opposite of [`method: LocatorAssertions.toContainText`].
@ -134,7 +160,7 @@ Whether to use `element.innerText` instead of `element.textContent` when retriev
### option: LocatorAssertions.NotToContainText.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToContainText.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveAttribute ## async method: LocatorAssertions.NotToHaveAttribute
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveAttribute`]. The opposite of [`method: LocatorAssertions.toHaveAttribute`].
@ -151,7 +177,7 @@ Expected attribute value.
### option: LocatorAssertions.NotToHaveAttribute.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveAttribute.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveClass ## async method: LocatorAssertions.NotToHaveClass
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveClass`]. The opposite of [`method: LocatorAssertions.toHaveClass`].
@ -163,7 +189,7 @@ Expected class or RegExp or a list of those.
### option: LocatorAssertions.NotToHaveClass.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveClass.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveCount ## async method: LocatorAssertions.NotToHaveCount
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveCount`]. The opposite of [`method: LocatorAssertions.toHaveCount`].
@ -175,7 +201,7 @@ Expected count.
### option: LocatorAssertions.NotToHaveCount.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveCount.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveCSS ## async method: LocatorAssertions.NotToHaveCSS
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveCSS`]. The opposite of [`method: LocatorAssertions.toHaveCSS`].
@ -192,7 +218,7 @@ CSS property value.
### option: LocatorAssertions.NotToHaveCSS.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveCSS.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveId ## async method: LocatorAssertions.NotToHaveId
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveId`]. The opposite of [`method: LocatorAssertions.toHaveId`].
@ -204,7 +230,7 @@ Element id.
### option: LocatorAssertions.NotToHaveId.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveId.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveJSProperty ## async method: LocatorAssertions.NotToHaveJSProperty
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveJSProperty`]. The opposite of [`method: LocatorAssertions.toHaveJSProperty`].
@ -215,13 +241,13 @@ The opposite of [`method: LocatorAssertions.toHaveJSProperty`].
Property name. Property name.
### param: LocatorAssertions.NotToHaveJSProperty.value ### param: LocatorAssertions.NotToHaveJSProperty.value
- `value` <[Serializable]> - `value` <[any]>
Property value. Property value.
### option: LocatorAssertions.NotToHaveJSProperty.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveJSProperty.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveText ## async method: LocatorAssertions.NotToHaveText
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveText`]. The opposite of [`method: LocatorAssertions.toHaveText`].
@ -238,7 +264,7 @@ Whether to use `element.innerText` instead of `element.textContent` when retriev
### option: LocatorAssertions.NotToHaveText.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveText.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.NotToHaveValue ## async method: LocatorAssertions.NotToHaveValue
* langs: python * langs: python
The opposite of [`method: LocatorAssertions.toHaveValue`]. The opposite of [`method: LocatorAssertions.toHaveValue`].
@ -251,7 +277,7 @@ Expected value.
### option: LocatorAssertions.NotToHaveValue.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.NotToHaveValue.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeChecked ## async method: LocatorAssertions.toBeChecked
* langs: * langs:
- alias-java: isChecked - alias-java: isChecked
@ -280,13 +306,18 @@ locator = page.locator(".subscribe")
expect(locator).to_be_checked() expect(locator).to_be_checked()
``` ```
```csharp
var locator = Page.Locator(".subscribe");
await Expect(locator).ToBeCheckedAsync();
```
### option: LocatorAssertions.toBeChecked.checked ### option: LocatorAssertions.toBeChecked.checked
- `checked` <[boolean]> - `checked` <[boolean]>
### option: LocatorAssertions.toBeChecked.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeChecked.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeDisabled ## async method: LocatorAssertions.toBeDisabled
* langs: * langs:
- alias-java: isDisabled - alias-java: isDisabled
@ -315,10 +346,15 @@ locator = page.locator("button.submit")
expect(locator).to_be_disabled() expect(locator).to_be_disabled()
``` ```
```csharp
var locator = Page.Locator("button.submit");
await Expect(locator).ToBeDisabledAsync();
```
### option: LocatorAssertions.toBeDisabled.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeDisabled.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeEditable ## async method: LocatorAssertions.toBeEditable
* langs: * langs:
- alias-java: isEditable - alias-java: isEditable
@ -347,10 +383,15 @@ locator = page.locator(".input")
expect(locator).to_be_editable() expect(locator).to_be_editable()
``` ```
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToBeEditableAsync();
```
### option: LocatorAssertions.toBeEditable.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeEditable.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeEmpty ## async method: LocatorAssertions.toBeEmpty
* langs: * langs:
- alias-java: isEmpty - alias-java: isEmpty
@ -379,10 +420,15 @@ locator = page.locator("div.warning")
expect(locator).to_be_empty() expect(locator).to_be_empty()
``` ```
```csharp
var locator = Page.Locator("div.warning");
await Expect(locator).ToBeEmptyAsync();
```
### option: LocatorAssertions.toBeEmpty.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeEmpty.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeEnabled ## async method: LocatorAssertions.toBeEnabled
* langs: * langs:
- alias-java: isEnabled - alias-java: isEnabled
@ -411,10 +457,15 @@ locator = page.locator("button.submit")
expect(locator).to_be_enabled() expect(locator).to_be_enabled()
``` ```
```csharp
var locator = Page.Locator("button.submit");
await Expect(locator).toBeEnabledAsync();
```
### option: LocatorAssertions.toBeEnabled.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeEnabled.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeFocused ## async method: LocatorAssertions.toBeFocused
* langs: * langs:
- alias-java: isFocused - alias-java: isFocused
@ -443,10 +494,15 @@ locator = page.locator('input')
expect(locator).to_be_focused() expect(locator).to_be_focused()
``` ```
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToBeFocusedAsync();
```
### option: LocatorAssertions.toBeFocused.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeFocused.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeHidden ## async method: LocatorAssertions.toBeHidden
* langs: * langs:
- alias-java: isHidden - alias-java: isHidden
@ -475,10 +531,15 @@ locator = page.locator('.my-element')
expect(locator).to_be_hidden() expect(locator).to_be_hidden()
``` ```
```csharp
var locator = Page.Locator(".my-element");
await Expect(locator).ToBeHiddenAsync();
```
### option: LocatorAssertions.toBeHidden.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeHidden.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toBeVisible ## async method: LocatorAssertions.toBeVisible
* langs: * langs:
- alias-java: isVisible - alias-java: isVisible
@ -507,9 +568,14 @@ locator = page.locator('.my-element')
expect(locator).to_be_visible() expect(locator).to_be_visible()
``` ```
```csharp
var locator = Page.Locator(".my-element");
await Expect(locator).ToBeVisibleAsync();
```
### option: LocatorAssertions.toBeVisible.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toBeVisible.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toContainText ## async method: LocatorAssertions.toContainText
* langs: * langs:
- alias-java: containsText - alias-java: containsText
@ -543,6 +609,12 @@ expect(locator).to_contain_text("substring")
expect(locator).to_contain_text(re.compile(r"\d messages")) expect(locator).to_contain_text(re.compile(r"\d messages"))
``` ```
```csharp
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: Note that if array is passed as an expected value, entire lists of elements can be asserted:
```js ```js
@ -570,6 +642,11 @@ locator = page.locator("list > .list-item")
expect(locator).to_contain_text(["Text 1", "Text 4", "Text 5"]) expect(locator).to_contain_text(["Text 1", "Text 4", "Text 5"])
``` ```
```csharp
var locator = Page.Locator("list > .list-item");
await Expect(locator).ToContainTextAsync(new string[] { "Text 1", "Text 4", "Text 5" });
```
### param: LocatorAssertions.toContainText.expected ### param: LocatorAssertions.toContainText.expected
* langs: python, js * langs: python, js
- `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>> - `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>>
@ -577,7 +654,7 @@ expect(locator).to_contain_text(["Text 1", "Text 4", "Text 5"])
Expected substring or RegExp or a list of those. Expected substring or RegExp or a list of those.
### param: LocatorAssertions.toContainText.expected ### param: LocatorAssertions.toContainText.expected
* langs: java * langs: java, csharp
- `expected` <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>> - `expected` <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Expected substring or RegExp or a list of those. Expected substring or RegExp or a list of those.
@ -590,7 +667,7 @@ Whether to use `element.innerText` instead of `element.textContent` when retriev
### option: LocatorAssertions.toContainText.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toContainText.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveAttribute ## async method: LocatorAssertions.toHaveAttribute
* langs: * langs:
- alias-java: hasAttribute - alias-java: hasAttribute
@ -619,6 +696,11 @@ locator = page.locator("input")
expect(locator).to_have_attribute("type", "text") expect(locator).to_have_attribute("type", "text")
``` ```
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToHaveAttributeAsync("type", "text");
```
### param: LocatorAssertions.toHaveAttribute.name ### param: LocatorAssertions.toHaveAttribute.name
- `name` <[string]> - `name` <[string]>
@ -631,7 +713,7 @@ Expected attribute value.
### option: LocatorAssertions.toHaveAttribute.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toHaveAttribute.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveClass ## async method: LocatorAssertions.toHaveClass
* langs: * langs:
- alias-java: hasClass - alias-java: hasClass
@ -660,6 +742,11 @@ locator = page.locator("#component")
expect(locator).to_have_class(re.compile(r"selected")) expect(locator).to_have_class(re.compile(r"selected"))
``` ```
```csharp
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: Note that if array is passed as an expected value, entire lists of elements can be asserted:
```js ```js
@ -685,6 +772,11 @@ locator = page.locator("list > .component")
expect(locator).to_have_class(["component", "component selected", "component"]) expect(locator).to_have_class(["component", "component selected", "component"])
``` ```
```csharp
var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveClassAsync(new string[]{"component", "component selected", "component"});
```
### param: LocatorAssertions.toHaveClass.expected ### param: LocatorAssertions.toHaveClass.expected
* langs: python, js * langs: python, js
- `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>> - `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>>
@ -692,7 +784,7 @@ expect(locator).to_have_class(["component", "component selected", "component"])
Expected class or RegExp or a list of those. Expected class or RegExp or a list of those.
### param: LocatorAssertions.toHaveClass.expected ### param: LocatorAssertions.toHaveClass.expected
* langs: java * langs: java, csharp
- `expected` <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>> - `expected` <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Expected class or RegExp or a list of those. Expected class or RegExp or a list of those.
@ -700,7 +792,7 @@ Expected class or RegExp or a list of those.
### option: LocatorAssertions.toHaveClass.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toHaveClass.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveCount ## async method: LocatorAssertions.toHaveCount
* langs: * langs:
- alias-java: hasCount - alias-java: hasCount
@ -729,6 +821,11 @@ locator = page.locator("list > .component")
expect(locator).to_have_count(3) expect(locator).to_have_count(3)
``` ```
```csharp
var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveCountAsync(3);
```
### param: LocatorAssertions.toHaveCount.count ### param: LocatorAssertions.toHaveCount.count
- `count` <[int]> - `count` <[int]>
@ -736,7 +833,7 @@ Expected count.
### option: LocatorAssertions.toHaveCount.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toHaveCount.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveCSS ## async method: LocatorAssertions.toHaveCSS
* langs: * langs:
- alias-java: hasCSS - alias-java: hasCSS
@ -765,6 +862,11 @@ locator = page.locator("button")
expect(locator).to_have_css("display", "flex") expect(locator).to_have_css("display", "flex")
``` ```
```csharp
var locator = Page.Locator("button");
await Expect(locator).ToHaveCSSAsync("display", "flex");
```
### param: LocatorAssertions.toHaveCSS.name ### param: LocatorAssertions.toHaveCSS.name
- `name` <[string]> - `name` <[string]>
@ -777,7 +879,7 @@ CSS property value.
### option: LocatorAssertions.toHaveCSS.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toHaveCSS.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveId ## async method: LocatorAssertions.toHaveId
* langs: * langs:
- alias-java: hasId - alias-java: hasId
@ -806,6 +908,11 @@ locator = page.locator("input")
expect(locator).to_have_id("lastname") expect(locator).to_have_id("lastname")
``` ```
```csharp
var locator = Page.Locator("input");
await Expect(locator).ToHaveIdAsync("lastname");
```
### param: LocatorAssertions.toHaveId.id ### param: LocatorAssertions.toHaveId.id
- `id` <[string]|[RegExp]> - `id` <[string]|[RegExp]>
@ -814,7 +921,7 @@ Element id.
### option: LocatorAssertions.toHaveId.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toHaveId.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveJSProperty ## async method: LocatorAssertions.toHaveJSProperty
* langs: * langs:
- alias-java: hasJSProperty - alias-java: hasJSProperty
@ -844,19 +951,24 @@ locator = page.locator(".component")
expect(locator).to_have_js_property("loaded", True) expect(locator).to_have_js_property("loaded", True)
``` ```
```csharp
var locator = Page.Locator(".component");
await Expect(locator).ToHaveJSPropertyAsync("loaded", true);
```
### param: LocatorAssertions.toHaveJSProperty.name ### param: LocatorAssertions.toHaveJSProperty.name
- `name` <[string]> - `name` <[string]>
Property name. Property name.
### param: LocatorAssertions.toHaveJSProperty.value ### param: LocatorAssertions.toHaveJSProperty.value
- `value` <[Serializable]> - `value` <[any]>
Property value. Property value.
### option: LocatorAssertions.toHaveJSProperty.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toHaveJSProperty.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveText ## async method: LocatorAssertions.toHaveText
* langs: * langs:
- alias-java: hasText - alias-java: hasText
@ -891,6 +1003,13 @@ expect(locator).to_have_text(re.compile(r"Welcome, Test User"))
expect(locator).to_have_text(re.compile(r"Welcome, .*")) expect(locator).to_have_text(re.compile(r"Welcome, .*"))
``` ```
```csharp
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: Note that if array is passed as an expected value, entire lists of elements can be asserted:
```js ```js
@ -916,6 +1035,11 @@ locator = page.locator("list > .component")
expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"]) expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"])
``` ```
```csharp
var locator = Page.Locator("list > .component");
await Expect(locator).toHaveTextAsync(new string[]{ "Text 1", "Text 2", "Text 3" });
```
### param: LocatorAssertions.toHaveText.expected ### param: LocatorAssertions.toHaveText.expected
* langs: python, js * langs: python, js
- `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>> - `expected` <[string]|[RegExp]|[Array]<[string]|[RegExp]>>
@ -923,7 +1047,7 @@ expect(locator).to_have_text(["Text 1", "Text 2", "Text 3"])
Expected substring or RegExp or a list of those. Expected substring or RegExp or a list of those.
### param: LocatorAssertions.toHaveText.expected ### param: LocatorAssertions.toHaveText.expected
* langs: java * langs: java, csharp
- `expected` <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>> - `expected` <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Expected substring or RegExp or a list of those. Expected substring or RegExp or a list of those.
@ -935,7 +1059,7 @@ Whether to use `element.innerText` instead of `element.textContent` when retriev
### option: LocatorAssertions.toHaveText.timeout = %%-assertions-timeout-%% ### option: LocatorAssertions.toHaveText.timeout = %%-assertions-timeout-%%
## method: LocatorAssertions.toHaveValue ## async method: LocatorAssertions.toHaveValue
* langs: * langs:
- alias-java: hasValue - alias-java: hasValue
@ -966,6 +1090,11 @@ locator = page.locator("input[type=number]")
expect(locator).to_have_value(re.compile(r"[0-9]")) expect(locator).to_have_value(re.compile(r"[0-9]"))
``` ```
```csharp
var locator = Page.Locator("input[type=number]");
await Expect(locator).ToHaveValueAsync(new Regex("[0-9]"));
```
### param: LocatorAssertions.toHaveValue.value ### param: LocatorAssertions.toHaveValue.value
- `value` <[string]|[RegExp]> - `value` <[string]|[RegExp]>

View file

@ -1,5 +1,4 @@
# class: PageAssertions # class: PageAssertions
* langs: java, python, js
The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests. A new instance of [PageAssertions] is created by calling [`method: PlaywrightAssertions.expectPage`]: The [PageAssertions] class provides assertion methods that can be used to make assertions about the [Page] state in the tests. A new instance of [PageAssertions] is created by calling [`method: PlaywrightAssertions.expectPage`]:
@ -48,9 +47,31 @@ def test_navigates_to_login_page(page: Page) -> None:
expect(page).to_have_url(re.compile(r".*/login")) expect(page).to_have_url(re.compile(r".*/login"))
``` ```
```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 NavigatetoLoginPage()
{
// ..
await Page.ClickAsync("#login");
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
}
}
}
```
## method: PageAssertions.not ## method: PageAssertions.not
* langs: java, js * langs: java, js, csharp
- returns: <[PageAssertions]> - returns: <[PageAssertions]>
Makes the assertion check for the opposite condition. For example, this code tests that the page URL doesn't contain `"error"`: Makes the assertion check for the opposite condition. For example, this code tests that the page URL doesn't contain `"error"`:
@ -63,7 +84,11 @@ await expect(page).not.toHaveURL('error');
assertThat(page).not().hasURL("error"); assertThat(page).not().hasURL("error");
``` ```
## method: PageAssertions.NotToHaveTitle ```csharp
await Expect(page).Not.ToHaveURL("error");
```
## async method: PageAssertions.NotToHaveTitle
* langs: python * langs: python
The opposite of [`method: PageAssertions.toHaveTitle`]. The opposite of [`method: PageAssertions.toHaveTitle`].
@ -76,7 +101,7 @@ Expected title or RegExp.
### option: PageAssertions.NotToHaveTitle.timeout = %%-assertions-timeout-%% ### option: PageAssertions.NotToHaveTitle.timeout = %%-assertions-timeout-%%
## method: PageAssertions.NotToHaveURL ## async method: PageAssertions.NotToHaveURL
* langs: python * langs: python
- alias-java: hasURL - alias-java: hasURL
@ -89,7 +114,7 @@ Expected substring or RegExp.
### option: PageAssertions.NotToHaveURL.timeout = %%-assertions-timeout-%% ### option: PageAssertions.NotToHaveURL.timeout = %%-assertions-timeout-%%
## method: PageAssertions.toHaveTitle ## async method: PageAssertions.toHaveTitle
* langs: * langs:
- alias-java: hasTitle - alias-java: hasTitle
@ -119,6 +144,10 @@ from playwright.sync_api import expect
expect(page).to_have_title(re.compile(r".*checkout")) expect(page).to_have_title(re.compile(r".*checkout"))
``` ```
```csharp
await Expect(page).ToHaveTitle("Playwright");
```
### param: PageAssertions.toHaveTitle.titleOrRegExp ### param: PageAssertions.toHaveTitle.titleOrRegExp
- `titleOrRegExp` <[string]|[RegExp]> - `titleOrRegExp` <[string]|[RegExp]>
@ -126,7 +155,7 @@ Expected title or RegExp.
### option: PageAssertions.toHaveTitle.timeout = %%-assertions-timeout-%% ### option: PageAssertions.toHaveTitle.timeout = %%-assertions-timeout-%%
## method: PageAssertions.toHaveURL ## async method: PageAssertions.toHaveURL
* langs: * langs:
- alias-java: hasURL - alias-java: hasURL
@ -156,6 +185,10 @@ from playwright.sync_api import expect
expect(page).to_have_url(re.compile(".*checkout")) expect(page).to_have_url(re.compile(".*checkout"))
``` ```
```csharp
await Expect(page).ToHaveURL(new Regex(".*checkout"));
```
### param: PageAssertions.toHaveURL.urlOrRegExp ### param: PageAssertions.toHaveURL.urlOrRegExp
- `urlOrRegExp` <[string]|[RegExp]> - `urlOrRegExp` <[string]|[RegExp]>

View file

@ -1,5 +1,4 @@
# class: PlaywrightAssertions # class: PlaywrightAssertions
* langs: java, python, js
The [PlaywrightAssertions] class provides convenience methods for creating assertions that will wait until the expected condition is met. The [PlaywrightAssertions] class provides convenience methods for creating assertions that will wait until the expected condition is met.
@ -48,6 +47,26 @@ public class TestExample {
} }
``` ```
```csharp
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
using static Microsoft.Playwright.Assertions;
namespace Playwright.TestingHarnessTest.NUnit
{
public class ExampleTests : PageTest
{
[Test]
public async Task StatusBecomesSubmitted()
{
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}
}
```
Playwright will be re-testing the node with the selector `.status` until fetched Node has the `"Submitted"` Playwright will be re-testing the node with the selector `.status` until fetched Node has the `"Submitted"`
text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is
reached. You can pass this timeout as an option. reached. You can pass this timeout as an option.
@ -59,6 +78,7 @@ By default, the timeout for assertions is set to 5 seconds.
- alias-java: assertThat - alias-java: assertThat
- alias-python: expect - alias-python: expect
- alias-js: expect - alias-js: expect
- alias-csharp: Expect
- returns: <[APIResponseAssertions]> - returns: <[APIResponseAssertions]>
Creates a [APIResponseAssertions] object for the given [APIResponse]. Creates a [APIResponseAssertions] object for the given [APIResponse].
@ -73,10 +93,11 @@ PlaywrightAssertions.assertThat(response).isOK();
[APIResponse] object to use for assertions. [APIResponse] object to use for assertions.
## method: PlaywrightAssertions.expectLocator ## method: PlaywrightAssertions.expectLocator
* langs: java, python, js * langs:
- alias-java: assertThat - alias-java: assertThat
- alias-python: expect - alias-python: expect
- alias-js: expect - alias-js: expect
- alias-csharp: Expect
- returns: <[LocatorAssertions]> - returns: <[LocatorAssertions]>
Creates a [LocatorAssertions] object for the given [Locator]. Creates a [LocatorAssertions] object for the given [Locator].
@ -85,16 +106,21 @@ Creates a [LocatorAssertions] object for the given [Locator].
PlaywrightAssertions.assertThat(locator).isVisible(); PlaywrightAssertions.assertThat(locator).isVisible();
``` ```
```csharp
await Expect(locator).ToBeVisibleAsync();
```
### param: PlaywrightAssertions.expectLocator.locator ### param: PlaywrightAssertions.expectLocator.locator
- `locator` <[Locator]> - `locator` <[Locator]>
[Locator] object to use for assertions. [Locator] object to use for assertions.
## method: PlaywrightAssertions.expectPage ## method: PlaywrightAssertions.expectPage
* langs: java, python, js * langs:
- alias-java: assertThat - alias-java: assertThat
- alias-python: expect - alias-python: expect
- alias-js: expect - alias-js: expect
- alias-csharp: Expect
- returns: <[PageAssertions]> - returns: <[PageAssertions]>
Creates a [PageAssertions] object for the given [Page]. Creates a [PageAssertions] object for the given [Page].
@ -103,6 +129,10 @@ Creates a [PageAssertions] object for the given [Page].
PlaywrightAssertions.assertThat(page).hasTitle("News"); PlaywrightAssertions.assertThat(page).hasTitle("News");
``` ```
```csharp
await Expect(page).ToHaveTitleAsync("News");
```
### param: PlaywrightAssertions.expectPage.page ### param: PlaywrightAssertions.expectPage.page
- `page` <[Page]> - `page` <[Page]>

View file

@ -710,7 +710,7 @@ using the [`method: AndroidDevice.setDefaultTimeout`] method.
Time to retry the assertion for. Defaults to `timeout` in [`property: TestConfig.expect`]. Time to retry the assertion for. Defaults to `timeout` in [`property: TestConfig.expect`].
## assertions-timeout ## assertions-timeout
* langs: java, python * langs: java, python, csharp
- `timeout` <[float]> - `timeout` <[float]>
Time to retry the assertion for. Time to retry the assertion for.