Skip to main content

Assertions

Playwright gives you Web-First Assertions with convenience methods for creating assertions that will wait and retry until the expected condition is met.

Consider the following example:

using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;

namespace PlaywrightTests;

[TestFixture]
public class ExampleTests : PageTest
{
[Test]
public async Task StatusBecomesSubmitted()
{
await Page.Locator("#submit-button").ClickAsync();
await Expect(Page.Locator(".status")).ToHaveTextAsync("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 reached. You can pass this timeout as an option.

By default, the timeout for assertions is set to 5 seconds.

Expect(Locator).Not

Added in: v1.20

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");

Expect(Locator).ToBeCheckedAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeCheckedOptions?>
    • Checked <bool?> Added in: v1.18#
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to a checked input.

var locator = Page.GetByLabel("Subscribe to newsletter");
await Expect(locator).ToBeCheckedAsync();

Expect(Locator).ToBeDisabledAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeDisabledOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.Locator("button.submit");
await Expect(locator).ToBeDisabledAsync();

Expect(Locator).ToBeEditableAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeEditableOptions?>
    • Editable <bool?> Added in: v1.26#
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an editable element.

var locator = Page.GetByRole("textbox");
await Expect(locator).ToBeEditableAsync();

Expect(Locator).ToBeEmptyAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeEmptyOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.Locator("div.warning");
await Expect(locator).ToBeEmptyAsync();

Expect(Locator).ToBeEnabledAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeEnabledOptions?>
    • Enabled <bool?> Added in: v1.26#
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an enabled element.

var locator = Page.Locator("button.submit");
await Expect(locator).toBeEnabledAsync();

Expect(Locator).ToBeFocusedAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeFocusedOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to a focused DOM node.

var locator = Page.GetByRole("textbox");
await Expect(locator).ToBeFocusedAsync();

Expect(Locator).ToBeHiddenAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeHiddenOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.Locator(".my-element");
await Expect(locator).ToBeHiddenAsync();

Expect(Locator).ToBeVisibleAsync(options)

Added in: v1.20
  • options <LocatorAssertionsToBeVisibleOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
    • Visible <bool?> Added in: v1.26#
  • returns: <void>#

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

var locator = Page.Locator(".my-element");
await Expect(locator).ToBeVisibleAsync();

Expect(Locator).ToContainTextAsync(expected, options)

Added in: v1.20
  • expected <string|Regex|IEnumerable<string>|IEnumerable<Regex>> Expected substring or RegExp or a list of those. Added in: v1.18#
  • options <LocatorAssertionsToContainTextOptions?>
    • IgnoreCase <bool?> Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified. Added in: v1.23#
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
    • UseInnerText <bool?> Whether to use element.innerText instead of element.textContent when retrieving DOM node text. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element that contains the given text. You can use regular expressions for the value as well.

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:

  1. Locator resolves to a list of elements.
  2. Elements from a subset of this list contain text from the expected array, respectively.
  3. The matching subset of elements has the same order as the expected array.
  4. Each text value from the expected array is matched by some element from the list.

For example, consider the following list:

<ul>
<li>Item Text 1</li>
<li>Item Text 2</li>
<li>Item Text 3</li>
</ul>

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"});

Expect(Locator).ToHaveAttributeAsync(name, value, options)

Added in: v1.20
  • name <string> Attribute name. Added in: v1.18#
  • value <string|Regex> Expected attribute value. Added in: v1.18#
  • options <LocatorAssertionsToHaveAttributeOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the Locator points to an element with given attribute.

var locator = Page.Locator("input");
await Expect(locator).ToHaveAttributeAsync("type", "text");

Expect(Locator).ToHaveClassAsync(expected, options)

Added in: v1.20

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

<div class='selected row' id='component'></div>
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"});

Expect(Locator).ToHaveCountAsync(count, options)

Added in: v1.20
  • count <int> Expected count. Added in: v1.18#
  • options <LocatorAssertionsToHaveCountOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.Locator("list > .component");
await Expect(locator).ToHaveCountAsync(3);

Expect(Locator).ToHaveCSSAsync(name, value, options)

Added in: v1.20
  • name <string> CSS property name. Added in: v1.18#
  • value <string|Regex> CSS property value. Added in: v1.18#
  • options <LocatorAssertionsToHaveCSSOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.GetByRole("button");
await Expect(locator).ToHaveCSSAsync("display", "flex");

Expect(Locator).ToHaveIdAsync(id, options)

Added in: v1.20
  • id <string|Regex> Element id. Added in: v1.18#
  • options <LocatorAssertionsToHaveIdOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.GetByRole("textbox");
await Expect(locator).ToHaveIdAsync("lastname");

Expect(Locator).ToHaveJSPropertyAsync(name, value, options)

Added in: v1.20
  • name <string> Property name. Added in: v1.18#
  • value <[object]> Property value. Added in: v1.18#
  • options <LocatorAssertionsToHaveJSPropertyOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.Locator(".component");
await Expect(locator).ToHaveJSPropertyAsync("loaded", true);

Expect(Locator).ToHaveTextAsync(expected, options)

Added in: v1.20
  • expected <string|Regex|IEnumerable<string>|IEnumerable<Regex>> Expected substring or RegExp or a list of those. Added in: v1.18#
  • options <LocatorAssertionsToHaveTextOptions?>
    • IgnoreCase <bool?> Whether to perform case-insensitive match. ignoreCase option takes precedence over the corresponding regular expression flag if specified. Added in: v1.23#
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
    • UseInnerText <bool?> Whether to use element.innerText instead of element.textContent when retrieving DOM node text. Added in: v1.18#
  • returns: <void>#

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

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:

  1. Locator resolves to a list of elements.
  2. The number of elements equals the number of expected values in the array.
  3. Elements from the list have text matching expected array values, one by one, in order.

For example, consider the following list:

<ul>
<li>Text 1</li>
<li>Text 2</li>
<li>Text 3</li>
</ul>

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"});

Expect(Locator).ToHaveValueAsync(value, options)

Added in: v1.20
  • value <string|Regex> Expected value. Added in: v1.18#
  • options <LocatorAssertionsToHaveValueOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

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

var locator = Page.Locator("input[type=number]");
await Expect(locator).ToHaveValueAsync(new Regex("[0-9]"));

Expect(Locator).ToHaveValuesAsync(values, options)

Added in: v1.23

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

For example, given the following element:

<select id="favorite-colors" multiple>
<option value="R">Red</option>
<option value="G">Green</option>
<option value="B">Blue</option>
</select>
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") });

Expect(Page).Not

Added in: v1.20

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

await Expect(page).Not.ToHaveURL("error");

Expect(Page).ToHaveTitleAsync(titleOrRegExp, options)

Added in: v1.20
  • titleOrRegExp <string|Regex> Expected title or RegExp. Added in: v1.18#
  • options <PageAssertionsToHaveTitleOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the page has the given title.

await Expect(page).ToHaveTitle("Playwright");

Expect(Page).ToHaveURLAsync(urlOrRegExp, options)

Added in: v1.20
  • urlOrRegExp <string|Regex> Expected URL string or RegExp. Added in: v1.18#
  • options <PageAssertionsToHaveURLOptions?>
    • Timeout <double?> Time to retry the assertion for. Added in: v1.18#
  • returns: <void>#

Ensures the page is navigated to the given URL.

await Expect(page).ToHaveURL(new Regex(".*checkout"));

Expect(ApiResponse).Not

Added in: v1.20

Makes the assertion check for the opposite condition. For example, this code tests that the response status is not successful:

Expect(ApiResponse).ToBeOKAsync()

Added in: v1.18

Ensures the response status code is within 200..299 range.