Skip to main content
Version: 1.17

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

content = page.text_content("nav:first-child")
assert content == "home"

API reference

Inner text

text = page.inner_text(".selected")
assert text == "value"

API reference

Attribute value

alt = page.get_attribute("input", "alt")
assert alt == "Text"

Checkbox state

checked = page.is_checked("input")
assert checked

API reference

JS expression

content = page.locator("nav:first-child").text_content()
assert content == "home"

API reference

Inner HTML

html = page.inner_html("div.result")
assert html == "<p>Result</p>"

API reference

Visibility

visible = page.is_visible("input")
assert visible

API reference

Enabled state

enabled = page.is_enabled("input")
assert 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
user_id = page.evaluate("() => window.localStorage.getItem('user_id')")
assert user_id

# Assert value for input element
value = page.locator('#search').input_value()
assert value == 'query'

# Assert computed style
font_size = page.locator('div').evaluate('el => window.getComputedStyle(el).fontSize')
assert font_size == '16px'

# Assert list length
length = page.locator('li.selected').count()
assert length == 3