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

const content = await page.textContent('nav:first-child');
expect(content).toBe('home');

API reference

Inner text

const text = await page.innerText('.selected');
expect(text).toBe('value');

API reference

Attribute value

const alt = await page.getAttribute('input', 'alt');
expect(alt).toBe('Text');

Checkbox state

const checked = await page.isChecked('input');
expect(checked).toBeTruthy();

API reference

JS expression

const content = await page.locator('nav:first-child').textContent();
expect(content).toBe('home');

API reference

Inner HTML

const html = await page.innerHTML('div.result');
expect(html).toBe('<p>Result</p>');

API reference

Visibility

const visible = await page.isVisible('input');
expect(visible).toBeTruthy();

API reference

Enabled state

const enabled = await page.isEnabled('input');
expect(enabled).toBeTruthy();

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
const userId = page.evaluate(() => window.localStorage.getItem('userId'));
expect(userId).toBeTruthy();

// Assert value for input element
const value = await page.locator('#search').inputValue();
expect(value === 'query').toBeTruthy();

// Assert computed style
const fontSize = await page.locator('div').evaluate(el => window.getComputedStyle(el).fontSize);
expect(fontSize === '16px').toBeTruthy();

// Assert list length
const length = await page.locator('li.selected').count();
expect(length === 3).toBeTruthy();