Skip to main content
Version: 1.12.0

Assertions

Playwright provides convenience APIs for common tasks, like reading the text content of an element. These APIs can be used in your test assertions.

Text content#

var content = await page.TextContentAsync("nav:first-child");
Assert.Equals("home", content);

API reference#

Inner text#

var content = await page.InnerTextAsync(".selected");
Assert.Equals("value", content);

API reference#

Attribute value#

var value = await page.GetAttributeAsync("input", "alt");
Assert.Equals("Text", value);

Checkbox state#

var checked = await page.IsCheckedAsync("input");
Assert.True(checked);

API reference#

JS expression#

var content = await page.EvalOnSelectorAsync("nav:first-child", "e => e.textContent");
Assert.Equals("home", content);

API reference#

Inner HTML#

var html = await page.InnerHTMLAsync("div.result");
Assert.Equals(html, "<p>Result</p>");

API reference#

Visibility#

var visibility = await page.IsVisibleAsync("input");
Assert.True(visibility);

API reference#

Enabled state#

var enabled = await page.IsEnabledAsync("input");
Assert.True(enabled);

API reference#

Custom assertions#

With Playwright, you can also write custom JavaScript to run in the context of the browser. This is useful in situations where you want to assert for values that are not covered by the convenience APIs above.

// Assert local storage value
var userId = await page.EvaluateAsync<string>("() => window.localStorage.getItem('userId')");
Assert.NotNull(userId);
// Assert value for input element
await page.WaitForSelectorAsync("#search");
var value = await page.EvalOnSelectorAsync("#search", "el => el.value");
Assert.Equals("query", value);
// Assert computed style
var fontSize = await page.EvalOnSelectorAsync<string>("div", "el => window.getComputedStyle(el).fontSize");
Assert.Equals("16px", fontSize);
// Assert list length
var length = await page.EvalOnSelectorAllAsync<int>("li.selected", "items => items.length");
Assert.Equals(3, length);

API reference#