Version: Next

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#

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

Checkbox state#

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

API reference#

JS expression#

content = page.eval_on_selector("nav:first-child", "e => e.textContent")
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
page.wait_for_selector('#search')
value = page.eval_on_selector('#search', 'el => el.value')
assert value == 'query'
# Assert computed style
font_size = page.eval_on_selector('div', 'el => window.getComputedStyle(el).fontSize')
assert font_size == '16px'
# Assert list length
length = page.eval_on_selector_all('li.selected', '(items) => items.length')
assert length == 3

API reference#