Version: 1.10.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#

String content = page.textContent("nav:first-child");
assertEquals("home", content);

API reference#

Inner text#

String text = page.innerText(".selected");
assertEquals("value", text);

API reference#

Attribute value#

String alt = page.getAttribute("input", "alt");
assertEquals("Text", alt);

Checkbox state#

boolean checked = page.isChecked("input");
assertTrue(checked);

API reference#

JS expression#

Object content = page.evalOnSelector("nav:first-child", "e => e.textContent");
assertEquals("home", content);

API reference#

Inner HTML#

String html = page.innerHTML("div.result");
assertEquals("<p>Result</p>", html);

API reference#

Visibility#

boolean visible = page.isVisible("input");
assertTrue(visible);

API reference#

Enabled state#

boolean enabled = page.isEnabled("input");
assertTrue(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
Object userId = page.evaluate("() => window.localStorage.getItem('userId')");
assertNotNull(userId);
// Assert value for input element
page.waitForSelector("#search");
Object value = page.evalOnSelector("#search", "el => el.value");
assertEquals("query", value);
// Assert computed style
Object fontSize = page.evalOnSelector("div", "el => window.getComputedStyle(el).fontSize");
assertEquals("16px", fontSize);
// Assert list length
Object length = page.evalOnSelectorAll("li.selected", "items => items.length");
assertEquals(3, length);

API reference#