Skip to main content
Version: 1.12.0


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

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#


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

API reference#

Enabled state#

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

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 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#