Assertions
The Playwright API can be used to read element contents and properties for test assertions. These values are fetched from the browser page and asserted in Node.js.
The examples in this guide use the built-in assert
module, but they can be used with any assertion library (like Expect or Chai). See Test runners for more info.
#
Common patternsPlaywright provides convenience APIs for common assertion tasks, like finding the text content of an element. These APIs require a selector to locate the element.
#
API reference- page.textContent(selector[, options])
- page.innerText(selector[, options])
- page.innerHTML(selector[, options])
- page.getAttribute(selector, name[, options])
- frame.textContent(selector[, options])
- frame.innerText(selector[, options])
- frame.innerHTML(selector[, options])
- frame.getAttribute(selector, name[, options])
#
Element HandlesElementHandle objects represent in-page DOM elements. They can be used to assert for multiple properties of the element.
It is recommended to fetch the ElementHandle
object with
page.waitForSelector
or
frame.waitForSelector
. These
APIs wait for the element to be visible and then return an ElementHandle
.
#
API reference- elementHandle.textContent()
- elementHandle.innerText()
- elementHandle.innerHTML()
- elementHandle.getAttribute(name)
- elementHandle.boundingBox()
#
Custom assertionsWith 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.
The following APIs do not auto-wait for the element. It is recommended to use
page.waitForSelector
or
frame.waitForSelector
.
#
API reference- page.evaluate(pageFunction[, arg])
- page.$eval(selector, pageFunction[, arg])
- page.$$eval(selector, pageFunction[, arg])
- frame.evaluate(pageFunction[, arg])
- frame.$eval(selector, pageFunction[, arg])
- frame.$$eval(selector, pageFunction[, arg])
- elementHandle.$eval(selector, pageFunction[, arg])
- elementHandle.$$eval(selector, pageFunction[, arg])
- Evaluation argument examples