Table of Contents

Interface ILocatorAssertions

Namespace
Microsoft.Playwright
Assembly
Microsoft.Playwright.dll

The ILocatorAssertions class provides assertion methods that can be used to make assertions about the ILocator state in the tests.

using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;

namespace PlaywrightTests;

[TestClass] public class ExampleTests : PageTest { [TestMethod] public async Task StatusBecomesSubmitted() { // ... await Page.GetByRole(AriaRole.Button, new() { Name = "Sign In" }).ClickAsync(); await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted"); } }

public interface ILocatorAssertions

Properties

Not

Makes the assertion check for the opposite condition. For example, this code tests that the Locator doesn't contain text "error":

await Expect(locator).Not.ToContainTextAsync("error");
ILocatorAssertions Not { get; }

Property Value

ILocatorAssertions

Methods

ToBeAttachedAsync(LocatorAssertionsToBeAttachedOptions?)

Ensures that ILocator points to an element that is connected to a Document or a ShadowRoot.

**Usage**

await Expect(Page.GetByText("Hidden text")).ToBeAttachedAsync();
Task ToBeAttachedAsync(LocatorAssertionsToBeAttachedOptions? options = null)

Parameters

options LocatorAssertionsToBeAttachedOptions

Call options

Returns

Task

ToBeCheckedAsync(LocatorAssertionsToBeCheckedOptions?)

Ensures the ILocator points to a checked input.

**Usage**

var locator = Page.GetByLabel("Subscribe to newsletter");
await Expect(locator).ToBeCheckedAsync();
Task ToBeCheckedAsync(LocatorAssertionsToBeCheckedOptions? options = null)

Parameters

options LocatorAssertionsToBeCheckedOptions

Call options

Returns

Task

ToBeDisabledAsync(LocatorAssertionsToBeDisabledOptions?)

Ensures the ILocator points to a disabled element. Element is disabled if it has "disabled" attribute or is disabled via 'aria-disabled'. Note that only native control elements such as HTML button, input, select, textarea, option, optgroup can be disabled by setting "disabled" attribute. "disabled" attribute on other elements is ignored by the browser.

**Usage**

var locator = Page.Locator("button.submit");
await Expect(locator).ToBeDisabledAsync();
Task ToBeDisabledAsync(LocatorAssertionsToBeDisabledOptions? options = null)

Parameters

options LocatorAssertionsToBeDisabledOptions

Call options

Returns

Task

ToBeEditableAsync(LocatorAssertionsToBeEditableOptions?)

Ensures the ILocator points to an editable element.

**Usage**

var locator = Page.GetByRole(AriaRole.Textbox);
await Expect(locator).ToBeEditableAsync();
Task ToBeEditableAsync(LocatorAssertionsToBeEditableOptions? options = null)

Parameters

options LocatorAssertionsToBeEditableOptions

Call options

Returns

Task

ToBeEmptyAsync(LocatorAssertionsToBeEmptyOptions?)

Ensures the ILocator points to an empty editable element or to a DOM node that has no text.

**Usage**

var locator = Page.Locator("div.warning");
await Expect(locator).ToBeEmptyAsync();
Task ToBeEmptyAsync(LocatorAssertionsToBeEmptyOptions? options = null)

Parameters

options LocatorAssertionsToBeEmptyOptions

Call options

Returns

Task

ToBeEnabledAsync(LocatorAssertionsToBeEnabledOptions?)

Ensures the ILocator points to an enabled element.

**Usage**

var locator = Page.Locator("button.submit");
await Expect(locator).toBeEnabledAsync();
Task ToBeEnabledAsync(LocatorAssertionsToBeEnabledOptions? options = null)

Parameters

options LocatorAssertionsToBeEnabledOptions

Call options

Returns

Task

ToBeFocusedAsync(LocatorAssertionsToBeFocusedOptions?)

Ensures the ILocator points to a focused DOM node.

**Usage**

var locator = Page.GetByRole(AriaRole.Textbox);
await Expect(locator).ToBeFocusedAsync();
Task ToBeFocusedAsync(LocatorAssertionsToBeFocusedOptions? options = null)

Parameters

options LocatorAssertionsToBeFocusedOptions

Call options

Returns

Task

ToBeHiddenAsync(LocatorAssertionsToBeHiddenOptions?)

Ensures that ILocator either does not resolve to any DOM node, or resolves to a non-visible one.

**Usage**

var locator = Page.Locator(".my-element");
await Expect(locator).ToBeHiddenAsync();
Task ToBeHiddenAsync(LocatorAssertionsToBeHiddenOptions? options = null)

Parameters

options LocatorAssertionsToBeHiddenOptions

Call options

Returns

Task

ToBeInViewportAsync(LocatorAssertionsToBeInViewportOptions?)

Ensures the ILocator points to an element that intersects viewport, according to the intersection observer API.

**Usage**

var locator = Page.GetByRole(AriaRole.Button);
// Make sure at least some part of element intersects viewport.
await Expect(locator).ToBeInViewportAsync();
// Make sure element is fully outside of viewport.
await Expect(locator).Not.ToBeInViewportAsync();
// Make sure that at least half of the element intersects viewport.
await Expect(locator).ToBeInViewportAsync(new() { Ratio = 0.5 });
Task ToBeInViewportAsync(LocatorAssertionsToBeInViewportOptions? options = null)

Parameters

options LocatorAssertionsToBeInViewportOptions

Call options

Returns

Task

ToBeVisibleAsync(LocatorAssertionsToBeVisibleOptions?)

Ensures that ILocator points to an attached and visible DOM node.

To check that at least one element from the list is visible, use First.

**Usage**

// A specific element is visible.
await Expect(Page.GetByText("Welcome")).ToBeVisibleAsync();

// At least one item in the list is visible. await Expect(Page.GetByTestId("todo-item").First).ToBeVisibleAsync();

// At least one of the two elements is visible, possibly both. await Expect( Page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }) .Or(Page.GetByRole(AriaRole.Button, new() { Name = "Sign up" })) .First ).ToBeVisibleAsync();

Task ToBeVisibleAsync(LocatorAssertionsToBeVisibleOptions? options = null)

Parameters

options LocatorAssertionsToBeVisibleOptions

Call options

Returns

Task

ToContainTextAsync(IEnumerable<string>, LocatorAssertionsToContainTextOptions?)

Ensures the ILocator points to an element that contains the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToContainTextAsync("substring");
await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Contains the right items in the right order
await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 1", "Text 3", "Text 4"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 3", "Text 2"});

// ✖ No item contains this text await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Some 33"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToContainTextAsync(new string[] {"Text 3"});

Task ToContainTextAsync(IEnumerable<string> expected, LocatorAssertionsToContainTextOptions? options = null)

Parameters

expected IEnumerable<string>

Expected substring or RegExp or a list of those.

options LocatorAssertionsToContainTextOptions

Call options

Returns

Task

ToContainTextAsync(IEnumerable<Regex>, LocatorAssertionsToContainTextOptions?)

Ensures the ILocator points to an element that contains the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToContainTextAsync("substring");
await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Contains the right items in the right order
await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 1", "Text 3", "Text 4"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 3", "Text 2"});

// ✖ No item contains this text await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Some 33"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToContainTextAsync(new string[] {"Text 3"});

Task ToContainTextAsync(IEnumerable<Regex> expected, LocatorAssertionsToContainTextOptions? options = null)

Parameters

expected IEnumerable<Regex>

Expected substring or RegExp or a list of those.

options LocatorAssertionsToContainTextOptions

Call options

Returns

Task

ToContainTextAsync(string, LocatorAssertionsToContainTextOptions?)

Ensures the ILocator points to an element that contains the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToContainTextAsync("substring");
await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Contains the right items in the right order
await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 1", "Text 3", "Text 4"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 3", "Text 2"});

// ✖ No item contains this text await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Some 33"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToContainTextAsync(new string[] {"Text 3"});

Task ToContainTextAsync(string expected, LocatorAssertionsToContainTextOptions? options = null)

Parameters

expected string

Expected substring or RegExp or a list of those.

options LocatorAssertionsToContainTextOptions

Call options

Returns

Task

ToContainTextAsync(Regex, LocatorAssertionsToContainTextOptions?)

Ensures the ILocator points to an element that contains the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToContainTextAsync("substring");
await Expect(locator).ToContainTextAsync(new Regex("\\d messages"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Contains the right items in the right order
await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 1", "Text 3", "Text 4"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Text 3", "Text 2"});

// ✖ No item contains this text await Expect(Page.Locator("ul > li")).ToContainTextAsync(new string[] {"Some 33"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToContainTextAsync(new string[] {"Text 3"});

Task ToContainTextAsync(Regex expected, LocatorAssertionsToContainTextOptions? options = null)

Parameters

expected Regex

Expected substring or RegExp or a list of those.

options LocatorAssertionsToContainTextOptions

Call options

Returns

Task

ToHaveAccessibleDescriptionAsync(string, LocatorAssertionsToHaveAccessibleDescriptionOptions?)

Ensures the ILocator points to an element with a given accessible description.

**Usage**

var locator = Page.GetByTestId("save-button");
await Expect(locator).toHaveAccessibleDescriptionAsync("Save results to disk");
Task ToHaveAccessibleDescriptionAsync(string description, LocatorAssertionsToHaveAccessibleDescriptionOptions? options = null)

Parameters

description string

Expected accessible description.

options LocatorAssertionsToHaveAccessibleDescriptionOptions

Call options

Returns

Task

ToHaveAccessibleDescriptionAsync(Regex, LocatorAssertionsToHaveAccessibleDescriptionOptions?)

Ensures the ILocator points to an element with a given accessible description.

**Usage**

var locator = Page.GetByTestId("save-button");
await Expect(locator).toHaveAccessibleDescriptionAsync("Save results to disk");
Task ToHaveAccessibleDescriptionAsync(Regex description, LocatorAssertionsToHaveAccessibleDescriptionOptions? options = null)

Parameters

description Regex

Expected accessible description.

options LocatorAssertionsToHaveAccessibleDescriptionOptions

Call options

Returns

Task

ToHaveAccessibleNameAsync(string, LocatorAssertionsToHaveAccessibleNameOptions?)

Ensures the ILocator points to an element with a given accessible name.

**Usage**

var locator = Page.GetByTestId("save-button");
await Expect(locator).toHaveAccessibleNameAsync("Save to disk");
Task ToHaveAccessibleNameAsync(string name, LocatorAssertionsToHaveAccessibleNameOptions? options = null)

Parameters

name string

Expected accessible name.

options LocatorAssertionsToHaveAccessibleNameOptions

Call options

Returns

Task

ToHaveAccessibleNameAsync(Regex, LocatorAssertionsToHaveAccessibleNameOptions?)

Ensures the ILocator points to an element with a given accessible name.

**Usage**

var locator = Page.GetByTestId("save-button");
await Expect(locator).toHaveAccessibleNameAsync("Save to disk");
Task ToHaveAccessibleNameAsync(Regex name, LocatorAssertionsToHaveAccessibleNameOptions? options = null)

Parameters

name Regex

Expected accessible name.

options LocatorAssertionsToHaveAccessibleNameOptions

Call options

Returns

Task

ToHaveAttributeAsync(string, string, LocatorAssertionsToHaveAttributeOptions?)

Ensures the ILocator points to an element with given attribute.

**Usage**

var locator = Page.Locator("input");
await Expect(locator).ToHaveAttributeAsync("type", "text");
Task ToHaveAttributeAsync(string name, string value, LocatorAssertionsToHaveAttributeOptions? options = null)

Parameters

name string

Attribute name.

value string

Expected attribute value.

options LocatorAssertionsToHaveAttributeOptions

Call options

Returns

Task

ToHaveAttributeAsync(string, Regex, LocatorAssertionsToHaveAttributeOptions?)

Ensures the ILocator points to an element with given attribute.

**Usage**

var locator = Page.Locator("input");
await Expect(locator).ToHaveAttributeAsync("type", "text");
Task ToHaveAttributeAsync(string name, Regex value, LocatorAssertionsToHaveAttributeOptions? options = null)

Parameters

name string

Attribute name.

value Regex

Expected attribute value.

options LocatorAssertionsToHaveAttributeOptions

Call options

Returns

Task

ToHaveCSSAsync(string, string, LocatorAssertionsToHaveCSSOptions?)

Ensures the ILocator resolves to an element with the given computed CSS style.

**Usage**

var locator = Page.GetByRole(AriaRole.Button);
await Expect(locator).ToHaveCSSAsync("display", "flex");
Task ToHaveCSSAsync(string name, string value, LocatorAssertionsToHaveCSSOptions? options = null)

Parameters

name string

CSS property name.

value string

CSS property value.

options LocatorAssertionsToHaveCSSOptions

Call options

Returns

Task

ToHaveCSSAsync(string, Regex, LocatorAssertionsToHaveCSSOptions?)

Ensures the ILocator resolves to an element with the given computed CSS style.

**Usage**

var locator = Page.GetByRole(AriaRole.Button);
await Expect(locator).ToHaveCSSAsync("display", "flex");
Task ToHaveCSSAsync(string name, Regex value, LocatorAssertionsToHaveCSSOptions? options = null)

Parameters

name string

CSS property name.

value Regex

CSS property value.

options LocatorAssertionsToHaveCSSOptions

Call options

Returns

Task

ToHaveClassAsync(IEnumerable<string>, LocatorAssertionsToHaveClassOptions?)

Ensures the ILocator points to an element with given CSS classes. This needs to be a full match or using a relaxed regular expression.

**Usage**

var locator = Page.Locator("#component");
await Expect(locator).ToHaveClassAsync(new Regex("selected"));
await Expect(locator).ToHaveClassAsync("selected row");

Note that if array is passed as an expected value, entire lists of elements can be asserted:

var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveClassAsync(new string[]{"component", "component selected", "component"});
Task ToHaveClassAsync(IEnumerable<string> expected, LocatorAssertionsToHaveClassOptions? options = null)

Parameters

expected IEnumerable<string>

Expected class or RegExp or a list of those.

options LocatorAssertionsToHaveClassOptions

Call options

Returns

Task

ToHaveClassAsync(IEnumerable<Regex>, LocatorAssertionsToHaveClassOptions?)

Ensures the ILocator points to an element with given CSS classes. This needs to be a full match or using a relaxed regular expression.

**Usage**

var locator = Page.Locator("#component");
await Expect(locator).ToHaveClassAsync(new Regex("selected"));
await Expect(locator).ToHaveClassAsync("selected row");

Note that if array is passed as an expected value, entire lists of elements can be asserted:

var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveClassAsync(new string[]{"component", "component selected", "component"});
Task ToHaveClassAsync(IEnumerable<Regex> expected, LocatorAssertionsToHaveClassOptions? options = null)

Parameters

expected IEnumerable<Regex>

Expected class or RegExp or a list of those.

options LocatorAssertionsToHaveClassOptions

Call options

Returns

Task

ToHaveClassAsync(string, LocatorAssertionsToHaveClassOptions?)

Ensures the ILocator points to an element with given CSS classes. This needs to be a full match or using a relaxed regular expression.

**Usage**

var locator = Page.Locator("#component");
await Expect(locator).ToHaveClassAsync(new Regex("selected"));
await Expect(locator).ToHaveClassAsync("selected row");

Note that if array is passed as an expected value, entire lists of elements can be asserted:

var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveClassAsync(new string[]{"component", "component selected", "component"});
Task ToHaveClassAsync(string expected, LocatorAssertionsToHaveClassOptions? options = null)

Parameters

expected string

Expected class or RegExp or a list of those.

options LocatorAssertionsToHaveClassOptions

Call options

Returns

Task

ToHaveClassAsync(Regex, LocatorAssertionsToHaveClassOptions?)

Ensures the ILocator points to an element with given CSS classes. This needs to be a full match or using a relaxed regular expression.

**Usage**

var locator = Page.Locator("#component");
await Expect(locator).ToHaveClassAsync(new Regex("selected"));
await Expect(locator).ToHaveClassAsync("selected row");

Note that if array is passed as an expected value, entire lists of elements can be asserted:

var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveClassAsync(new string[]{"component", "component selected", "component"});
Task ToHaveClassAsync(Regex expected, LocatorAssertionsToHaveClassOptions? options = null)

Parameters

expected Regex

Expected class or RegExp or a list of those.

options LocatorAssertionsToHaveClassOptions

Call options

Returns

Task

ToHaveCountAsync(int, LocatorAssertionsToHaveCountOptions?)

Ensures the ILocator resolves to an exact number of DOM nodes.

**Usage**

var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveCountAsync(3);
Task ToHaveCountAsync(int count, LocatorAssertionsToHaveCountOptions? options = null)

Parameters

count int

Expected count.

options LocatorAssertionsToHaveCountOptions

Call options

Returns

Task

ToHaveIdAsync(string, LocatorAssertionsToHaveIdOptions?)

Ensures the ILocator points to an element with the given DOM Node ID.

**Usage**

var locator = Page.GetByRole(AriaRole.Textbox);
await Expect(locator).ToHaveIdAsync("lastname");
Task ToHaveIdAsync(string id, LocatorAssertionsToHaveIdOptions? options = null)

Parameters

id string

Element id.

options LocatorAssertionsToHaveIdOptions

Call options

Returns

Task

ToHaveIdAsync(Regex, LocatorAssertionsToHaveIdOptions?)

Ensures the ILocator points to an element with the given DOM Node ID.

**Usage**

var locator = Page.GetByRole(AriaRole.Textbox);
await Expect(locator).ToHaveIdAsync("lastname");
Task ToHaveIdAsync(Regex id, LocatorAssertionsToHaveIdOptions? options = null)

Parameters

id Regex

Element id.

options LocatorAssertionsToHaveIdOptions

Call options

Returns

Task

ToHaveJSPropertyAsync(string, object, LocatorAssertionsToHaveJSPropertyOptions?)

Ensures the ILocator 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.

**Usage**

var locator = Page.Locator(".component");
await Expect(locator).ToHaveJSPropertyAsync("loaded", true);
Task ToHaveJSPropertyAsync(string name, object value, LocatorAssertionsToHaveJSPropertyOptions? options = null)

Parameters

name string

Property name.

value object

Property value.

options LocatorAssertionsToHaveJSPropertyOptions

Call options

Returns

Task

ToHaveRoleAsync(AriaRole, LocatorAssertionsToHaveRoleOptions?)

Ensures the ILocator points to an element with a given ARIA role.

Note that role is matched as a string, disregarding the ARIA role hierarchy. For example, asserting a superclass role "checkbox" on an element with a subclass role "switch" will fail.

**Usage**

var locator = Page.GetByTestId("save-button");
await Expect(locator).ToHaveRoleAsync(AriaRole.Button);
Task ToHaveRoleAsync(AriaRole role, LocatorAssertionsToHaveRoleOptions? options = null)

Parameters

role AriaRole

Required aria role.

options LocatorAssertionsToHaveRoleOptions

Call options

Returns

Task

ToHaveTextAsync(IEnumerable<string>, LocatorAssertionsToHaveTextOptions?)

Ensures the ILocator points to an element with the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, Test User"));
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, .*"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Has the right items in the right order
await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 3", "Text 2", "Text 1"});

// ✖ Last item does not match await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

Task ToHaveTextAsync(IEnumerable<string> expected, LocatorAssertionsToHaveTextOptions? options = null)

Parameters

expected IEnumerable<string>

Expected string or RegExp or a list of those.

options LocatorAssertionsToHaveTextOptions

Call options

Returns

Task

ToHaveTextAsync(IEnumerable<Regex>, LocatorAssertionsToHaveTextOptions?)

Ensures the ILocator points to an element with the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, Test User"));
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, .*"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Has the right items in the right order
await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 3", "Text 2", "Text 1"});

// ✖ Last item does not match await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

Task ToHaveTextAsync(IEnumerable<Regex> expected, LocatorAssertionsToHaveTextOptions? options = null)

Parameters

expected IEnumerable<Regex>

Expected string or RegExp or a list of those.

options LocatorAssertionsToHaveTextOptions

Call options

Returns

Task

ToHaveTextAsync(string, LocatorAssertionsToHaveTextOptions?)

Ensures the ILocator points to an element with the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, Test User"));
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, .*"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Has the right items in the right order
await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 3", "Text 2", "Text 1"});

// ✖ Last item does not match await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

Task ToHaveTextAsync(string expected, LocatorAssertionsToHaveTextOptions? options = null)

Parameters

expected string

Expected string or RegExp or a list of those.

options LocatorAssertionsToHaveTextOptions

Call options

Returns

Task

ToHaveTextAsync(Regex, LocatorAssertionsToHaveTextOptions?)

Ensures the ILocator points to an element with the given text. All nested elements will be considered when computing the text content of the element. You can use regular expressions for the value as well.

**Details**

When expected parameter is a string, Playwright will normalize whitespaces and line breaks both in the actual text and in the expected string before matching. When regular expression is used, the actual text is matched as is.

**Usage**

var locator = Page.Locator(".title");
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, Test User"));
await Expect(locator).ToHaveTextAsync(new Regex("Welcome, .*"));

If you pass an array as an expected value, the expectations are:

For example, consider the following list:

Let's see how we can use the assertion:

// ✓ Has the right items in the right order
await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

// ✖ Wrong order await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 3", "Text 2", "Text 1"});

// ✖ Last item does not match await Expect(Page.Locator("ul > li")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text"});

// ✖ Locator points to the outer list element, not to the list items await Expect(Page.Locator("ul")).ToHaveTextAsync(new string[] {"Text 1", "Text 2", "Text 3"});

Task ToHaveTextAsync(Regex expected, LocatorAssertionsToHaveTextOptions? options = null)

Parameters

expected Regex

Expected string or RegExp or a list of those.

options LocatorAssertionsToHaveTextOptions

Call options

Returns

Task

ToHaveValueAsync(string, LocatorAssertionsToHaveValueOptions?)

Ensures the ILocator points to an element with the given input value. You can use regular expressions for the value as well.

**Usage**

var locator = Page.Locator("input[type=number]");
await Expect(locator).ToHaveValueAsync(new Regex("[0-9]"));
Task ToHaveValueAsync(string value, LocatorAssertionsToHaveValueOptions? options = null)

Parameters

value string

Expected value.

options LocatorAssertionsToHaveValueOptions

Call options

Returns

Task

ToHaveValueAsync(Regex, LocatorAssertionsToHaveValueOptions?)

Ensures the ILocator points to an element with the given input value. You can use regular expressions for the value as well.

**Usage**

var locator = Page.Locator("input[type=number]");
await Expect(locator).ToHaveValueAsync(new Regex("[0-9]"));
Task ToHaveValueAsync(Regex value, LocatorAssertionsToHaveValueOptions? options = null)

Parameters

value Regex

Expected value.

options LocatorAssertionsToHaveValueOptions

Call options

Returns

Task

ToHaveValuesAsync(IEnumerable<string>, LocatorAssertionsToHaveValuesOptions?)

Ensures the ILocator points to multi-select/combobox (i.e. a select with the multiple attribute) and the specified values are selected.

**Usage**

For example, given the following element:

var locator = Page.Locator("id=favorite-colors");
await locator.SelectOptionAsync(new string[] { "R", "G" });
await Expect(locator).ToHaveValuesAsync(new Regex[] { new Regex("R"), new Regex("G") });
Task ToHaveValuesAsync(IEnumerable<string> values, LocatorAssertionsToHaveValuesOptions? options = null)

Parameters

values IEnumerable<string>

Expected options currently selected.

options LocatorAssertionsToHaveValuesOptions

Call options

Returns

Task

ToHaveValuesAsync(IEnumerable<Regex>, LocatorAssertionsToHaveValuesOptions?)

Ensures the ILocator points to multi-select/combobox (i.e. a select with the multiple attribute) and the specified values are selected.

**Usage**

For example, given the following element:

var locator = Page.Locator("id=favorite-colors");
await locator.SelectOptionAsync(new string[] { "R", "G" });
await Expect(locator).ToHaveValuesAsync(new Regex[] { new Regex("R"), new Regex("G") });
Task ToHaveValuesAsync(IEnumerable<Regex> values, LocatorAssertionsToHaveValuesOptions? options = null)

Parameters

values IEnumerable<Regex>

Expected options currently selected.

options LocatorAssertionsToHaveValuesOptions

Call options

Returns

Task