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
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
LocatorAssertionsToBeAttachedOptionsCall options
Returns
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
LocatorAssertionsToBeCheckedOptionsCall options
Returns
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
LocatorAssertionsToBeDisabledOptionsCall options
Returns
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
LocatorAssertionsToBeEditableOptionsCall options
Returns
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
LocatorAssertionsToBeEmptyOptionsCall options
Returns
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
LocatorAssertionsToBeEnabledOptionsCall options
Returns
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
LocatorAssertionsToBeFocusedOptionsCall options
Returns
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
LocatorAssertionsToBeHiddenOptionsCall options
Returns
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
LocatorAssertionsToBeInViewportOptionsCall options
Returns
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
LocatorAssertionsToBeVisibleOptionsCall options
Returns
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
LocatorAssertionsToContainTextOptionsCall options
Returns
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
LocatorAssertionsToContainTextOptionsCall options
Returns
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
stringExpected substring or RegExp or a list of those.
options
LocatorAssertionsToContainTextOptionsCall options
Returns
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
RegexExpected substring or RegExp or a list of those.
options
LocatorAssertionsToContainTextOptionsCall options
Returns
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
stringExpected accessible description.
options
LocatorAssertionsToHaveAccessibleDescriptionOptionsCall options
Returns
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
RegexExpected accessible description.
options
LocatorAssertionsToHaveAccessibleDescriptionOptionsCall options
Returns
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
stringExpected accessible name.
options
LocatorAssertionsToHaveAccessibleNameOptionsCall options
Returns
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
RegexExpected accessible name.
options
LocatorAssertionsToHaveAccessibleNameOptionsCall options
Returns
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
stringAttribute name.
value
stringExpected attribute value.
options
LocatorAssertionsToHaveAttributeOptionsCall options
Returns
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
stringAttribute name.
value
RegexExpected attribute value.
options
LocatorAssertionsToHaveAttributeOptionsCall options
Returns
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
stringCSS property name.
value
stringCSS property value.
options
LocatorAssertionsToHaveCSSOptionsCall options
Returns
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
stringCSS property name.
value
RegexCSS property value.
options
LocatorAssertionsToHaveCSSOptionsCall options
Returns
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
LocatorAssertionsToHaveClassOptionsCall options
Returns
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
LocatorAssertionsToHaveClassOptionsCall options
Returns
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
stringExpected class or RegExp or a list of those.
options
LocatorAssertionsToHaveClassOptionsCall options
Returns
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
RegexExpected class or RegExp or a list of those.
options
LocatorAssertionsToHaveClassOptionsCall options
Returns
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
intExpected count.
options
LocatorAssertionsToHaveCountOptionsCall options
Returns
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
stringElement id.
options
LocatorAssertionsToHaveIdOptionsCall options
Returns
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
RegexElement id.
options
LocatorAssertionsToHaveIdOptionsCall options
Returns
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
stringProperty name.
value
objectProperty value.
options
LocatorAssertionsToHaveJSPropertyOptionsCall options
Returns
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
AriaRoleRequired aria role.
options
LocatorAssertionsToHaveRoleOptionsCall options
Returns
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
LocatorAssertionsToHaveTextOptionsCall options
Returns
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
LocatorAssertionsToHaveTextOptionsCall options
Returns
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
stringExpected string or RegExp or a list of those.
options
LocatorAssertionsToHaveTextOptionsCall options
Returns
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
RegexExpected string or RegExp or a list of those.
options
LocatorAssertionsToHaveTextOptionsCall options
Returns
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
stringExpected value.
options
LocatorAssertionsToHaveValueOptionsCall options
Returns
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
RegexExpected value.
options
LocatorAssertionsToHaveValueOptionsCall options
Returns
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
LocatorAssertionsToHaveValuesOptionsCall options
Returns
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
LocatorAssertionsToHaveValuesOptionsCall options