LocatorAssertions Class

The assertions of a locator, as the web-first assertions of Playwright: each assertion runs the event loop of the page until it passes, and throws an ExpectationFailedException when it still fails after the timeout. The assertions on one element throw a StrictModeViolationException when the locator finds more than one.

public sealed class LocatorAssertions

Properties

NameDescription
NotGets the negated assertions (Expect(locator).Not.ToBeVisibleAsync()).

Gets the negated assertions (Expect(locator).Not.ToBeVisibleAsync()).

public LocatorAssertions Not { get; }

Property value

LocatorAssertions

Methods

NameDescription
ToBeAttachedAsync(TimeSpan?, CancellationToken)Waits until the locator finds an element.
ToBeCheckedAsync(TimeSpan?, CancellationToken)Waits until the check box, the radio button or the element with a checked state is checked.
ToBeDisabledAsync(TimeSpan?, CancellationToken)Waits until the element is disabled: a disabled form control, or aria-disabled="true".
ToBeEditableAsync(TimeSpan?, CancellationToken)Waits until the field is editable: enabled and not read-only.
ToBeEmptyAsync(TimeSpan?, CancellationToken)Waits until the element has no text, or, for a field, no value.
ToBeEnabledAsync(TimeSpan?, CancellationToken)Waits until the element is enabled.
ToBeFocusedAsync(TimeSpan?, CancellationToken)Waits until the element has the focus.
ToBeHiddenAsync(TimeSpan?, CancellationToken)Waits until the locator finds no element, or a hidden element.
ToBeVisibleAsync(TimeSpan?, CancellationToken)Waits until the locator finds a visible element: a non-empty box, and not visibility: hidden.
ToContainTextAsync(string, TimeSpan?, CancellationToken)Waits until the text of the element contains a text, once the white space of both texts is collapsed.
ToHaveAccessibilitySnapshotAsync(string, TimeSpan?, CancellationToken)Waits until the accessibility tree of the element is a snapshot (see Locator.GetAccessibilitySnapshot), ignoring the line endings and the trailing white space.
ToHaveAccessibleNameAsync(string, TimeSpan?, CancellationToken)Waits until the accessible name of the element is a text, once the white space of both texts is collapsed.
ToHaveAccessibleNameAsync(Regex, TimeSpan?, CancellationToken)Waits until the accessible name of the element matches a regular expression.
ToHaveAttributeAsync(string, TimeSpan?, CancellationToken)Waits until the element has an attribute.
ToHaveAttributeAsync(string, string, TimeSpan?, CancellationToken)Waits until an attribute of the element has a value.
ToHaveAttributeAsync(string, Regex, TimeSpan?, CancellationToken)Waits until the value of an attribute of the element matches a regular expression.
ToHaveClassAsync(string, TimeSpan?, CancellationToken)Waits until the class attribute of the element is a list of classes, once the white space of both lists is collapsed.
ToHaveClassAsync(Regex, TimeSpan?, CancellationToken)Waits until the class attribute of the element matches a regular expression (new Regex(@"\bactive\b")).
ToHaveCountAsync(int, TimeSpan?, CancellationToken)Waits until the locator finds a number of elements.
ToHaveCssAsync(string, string, TimeSpan?, CancellationToken)Waits until a computed style property of the element has a value (ToHaveCssAsync("display", "none")).
ToHaveRoleAsync(string, TimeSpan?, CancellationToken)Waits until the computed ARIA role of the element is a role.
ToHaveTextAsync(string, TimeSpan?, CancellationToken)Waits until the text of the element is a text, once the white space of both texts is collapsed.
ToHaveTextAsync(Regex, TimeSpan?, CancellationToken)Waits until the text of the element, with collapsed white space, matches a regular expression.
ToHaveValueAsync(string, TimeSpan?, CancellationToken)Waits until the value of the field (input, textarea or select) is a value.
ToHaveValueAsync(Regex, TimeSpan?, CancellationToken)Waits until the value of the field matches a regular expression.

ToBeAttachedAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the locator finds an element.

public Task ToBeAttachedAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeCheckedAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the check box, the radio button or the element with a checked state is checked.

public Task ToBeCheckedAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeDisabledAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the element is disabled: a disabled form control, or aria-disabled="true".

public Task ToBeDisabledAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeEditableAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the field is editable: enabled and not read-only.

public Task ToBeEditableAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeEmptyAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the element has no text, or, for a field, no value.

public Task ToBeEmptyAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeEnabledAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the element is enabled.

public Task ToBeEnabledAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeFocusedAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the element has the focus.

public Task ToBeFocusedAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeHiddenAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the locator finds no element, or a hidden element.

public Task ToBeHiddenAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToBeVisibleAsync(TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the locator finds a visible element: a non-empty box, and not visibility: hidden.

public Task ToBeVisibleAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToContainTextAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the text of the element contains a text, once the white space of both texts is collapsed.

public Task ToContainTextAsync(string expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

expected string
The text.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToHaveAccessibilitySnapshotAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the accessibility tree of the element is a snapshot (see Locator.GetAccessibilitySnapshot), ignoring the line endings and the trailing white space.

public Task ToHaveAccessibilitySnapshotAsync(string expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

expected string
The snapshot, in the YAML format of the ARIA snapshots of Playwright.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToHaveAccessibleNameAsync overloads

ToHaveAccessibleNameAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the accessible name of the element is a text, once the white space of both texts is collapsed.

public Task ToHaveAccessibleNameAsync(string name, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
name string
The accessible name.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveAccessibleNameAsync(Regex, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the accessible name of the element matches a regular expression.

public Task ToHaveAccessibleNameAsync(Regex name, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
name Regex
The regular expression.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveAttributeAsync overloads

ToHaveAttributeAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the element has an attribute.

public Task ToHaveAttributeAsync(string name, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
name string
The name of the attribute.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveAttributeAsync(string, string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until an attribute of the element has a value.

public Task ToHaveAttributeAsync(string name, string value, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
name string
The name of the attribute.
value string
The value.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveAttributeAsync(string, Regex, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the value of an attribute of the element matches a regular expression.

public Task ToHaveAttributeAsync(string name, Regex value, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
name string
The name of the attribute.
value Regex
The regular expression.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveClassAsync overloads

ToHaveClassAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the class attribute of the element is a list of classes, once the white space of both lists is collapsed.

public Task ToHaveClassAsync(string expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
expected string
The classes, separated by spaces.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveClassAsync(Regex, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the class attribute of the element matches a regular expression (new Regex(@"\bactive\b")).

public Task ToHaveClassAsync(Regex expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
expected Regex
The regular expression.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveCountAsync(int, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the locator finds a number of elements.

public Task ToHaveCountAsync(int count, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

count int
The number of elements.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToHaveCssAsync(string, string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until a computed style property of the element has a value (ToHaveCssAsync("display", "none")).

public Task ToHaveCssAsync(string name, string value, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

name string
The name of the property.
value string
The computed value.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToHaveRoleAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the computed ARIA role of the element is a role.

public Task ToHaveRoleAsync(string role, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

role string
The role.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.

Returns

Task A task completed once the assertion passes.

ToHaveTextAsync overloads

ToHaveTextAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the text of the element is a text, once the white space of both texts is collapsed.

public Task ToHaveTextAsync(string expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
expected string
The text.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveTextAsync(Regex, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the text of the element, with collapsed white space, matches a regular expression.

public Task ToHaveTextAsync(Regex expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
expected Regex
The regular expression.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveValueAsync overloads

ToHaveValueAsync(string, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the value of the field (input, textarea or select) is a value.

public Task ToHaveValueAsync(string expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
expected string
The value.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.

ToHaveValueAsync(Regex, TimeSpan?, CancellationToken)

LocatorAssertions.cs

Waits until the value of the field matches a regular expression.

public Task ToHaveValueAsync(Regex expected, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
expected Regex
The regular expression.
timeout TimeSpan?
The maximum time to wait, or null for TestPage.DefaultTimeout.
cancellationToken CancellationToken
A token to cancel the wait.
Returns

Task A task completed once the assertion passes.