Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Assertions

Playwright Assertions

This chapter comprehensively introduces Playwright's assertion system, including the use of auto-retrying assertions, generic assertions, and soft assertions.


What is an assertion

An assertion (Assertion) is used to verify whether the expected result of a test matches the actual result.

Playwright provides two sets of assertions: generic assertions (from Jest) and auto-retrying assertions (unique to Playwright).


Auto-retrying assertions vs generic assertions

Auto-retrying assertions (Async Matchers)

Playwright's web-specific assertions retry automatically until the condition is met or a timeout occurs; you must use await。

Example

// Auto-retrying assertion - waits for the condition to be met

await expect

(

page.

getByText

(

'RUNOOB'

)

)

.

toBeVisible

(

)

;

Generic assertions

Used for non-web-specific value comparisons; does not retry automatically.

Example

// Generic assertion - executes immediately, no waiting

expect

(

1

1

)

.

toBe

(

2

)

;

expect

(

'hello'

)

.

toContain

(

'ell'

)

;

expect

(

true

)

.

toBeTruthy

(

)

;

Type Auto-retry or not Whether await is needed Applicable scenarios
Auto-retrying assertion Is Must await Web-specific content such as page elements, URLs, and titles
Generic assertions No Not required JavaScript values such as ordinary variables, arrays, and objects

Page-level assertions

Example

// Page title assertion

await expect

(

page

)

.

toHaveTitle

(

'RUNOOB - Learning is not just about technology, it is about dreams!'

)

;

await expect

(

page

)

.

toHaveTitle

(

/RUNOOB/

)

;

// Page URL assertion

await expect

(

page

)

.

toHaveURL

(

'https://www.runoob.com/'

)

;

await expect

(

page

)

.

toHaveURL

(

/runoob.com/

)

;


Visibility assertions

Example

const

btn

=

page.

getByRole

(

'button'

,

{

name

:

'Submit'

}

)

;

// Element is visible

await expect

(

btn

)

.

toBeVisible

(

)

;

// Element is not visible

await expect

(

btn

)

.

toBeHidden

(

)

;

// Element exists in the DOM (but not necessarily visible)

await expect

(

btn

)

.

toBeAttached

(

)

;

// Element is visible within the viewport

await expect

(

btn

)

.

toBeInViewport

(

)

;


Text and value assertions

Example

const

heading

=

page.

getByRole

(

'heading'

,

{

name

:

'RUNOOB'

}

)

;

// Element's text content matches exactly

await expect

(

heading

)

.

toHaveText

(

'Learning is not just about technology, it is about dreams!'

)

;

// Element's text content contains a substring

await expect

(

heading

)

.

toContainText

(

'Ji shu' (a Chinese phrase)

)

;

// Current value of input

await expect

(

page.

getByLabel

(

'Username'

)

)

.

toHaveValue

(

'runoob_user'

)

;

// Element content is empty

await expect

(

page.

getByTestId

(

'error-msg'

)

)

.

toBeEmpty

(

)

;


State assertions

Example

const

checkbox

=

page.

getByLabel

(

'Agree to the terms'

)

;

const

input

=

page.

getByLabel

(

'Email'

)

;

// Element is enabled

await expect

(

input

)

.

toBeEnabled

(

)

;

// Element is disabled

await expect

(

input

)

.

toBeDisabled

(

)

;

// Element is editable

await expect

(

input

)

.

toBeEditable

(

)

;

// Checkbox is checked

await expect

(

checkbox

)

.

toBeChecked

(

)

;

// Element has focus

await expect

(

input

)

.

toBeFocused

(

)

;


Attribute assertions

Example

const

element

=

page.

locator

(

'.runoob-logo'

)

;

// DOM attribute assertion

await expect

(

element

)

.

toHaveAttribute

(

'src'

,

'/static/images/logo.png'

)

;

// Only check that the attribute exists

await expect

(

element

)

.

toHaveAttribute

(

'alt'

)

;

// CSS class name assertion

await expect

(

element

)

.

toHaveClass

(

'logo-primary'

)

;

// CSS property value assertion

await expect

(

element

)

.

toHaveCSS

(

'display'

,

'block'

)

;

// Element ID assertion

await expect

(

element

)

.

toHaveId

(

'main-logo'

)

;

// Accessible name

await expect

(

page.

getByRole

(

'button'

)

)

.

toHaveAccessibleName

(

'Submit'

)

;


Count assertions

Example

// Number of matching elements

const

items

=

page.

getByRole

(

'listitem'

)

;

await expect

(

items

)

.

toHaveCount

(

5

)

;

// Use with filter

const

activeItems

=

page.

getByRole

(

'listitem'

)

.

filter

(

{

hasText

:

'Activated'

}

)

;

await expect

(

activeItems

)

.

toHaveCount

(

3

)

;


Generic assertions quick reference

Method Description Example
toBe(value) Strict equality (===) expect(x).toBe(42)
toEqual(value) Deep equality expect(obj).toEqual({ a: 1 })
toContain(item) Array/string contains expect(arr).toContain('a')
toBeTruthy() Value is truthy expect(x).toBeTruthy()
toBeFalsy() Value is falsy expect(x).toBeFalsy()
toBeNull() Value is null expect(x).toBeNull()
toBeGreaterThan(n) Greater than n expect(x).toBeGreaterThan(10)
toBeLessThan(n) Less than n expect(x).toBeLessThan(100)
toHaveLength(n) Array length expect(arr).toHaveLength(3)

expect.soft() soft assertions

A normal assertion terminates the test immediately upon failure.

A soft assertion does not terminate on failure; it continues executing subsequent code and reports all failures together at the end of the test.

Example

test

(

'Multiple form validation checks'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

'https://example.com/form'

)

;

 

// Soft assertion - failure does not stop the test

  await expect.

soft

(

page.

getByLabel

(

'Username'

)

)

.

toBeVisible

(

)

;

  await expect.

soft

(

page.

getByLabel

(

'Password'

)

)

.

toBeVisible

(

)

;

  await expect.

soft

(

page.

getByLabel

(

'Email'

)

)

.

toBeVisible

(

)

;

  await expect.

soft

(

page.

getByRole

(

'button'

,

{

name

:

'Register'

}

)

)

.

toBeEnabled

(

)

;

 

// If any of the above assertions fail, checking continues and all are reported together at the end

}

)

;

Use when testing form pages expect.soft() You can discover all missing fields at once, without having to fix them one by one to see the next problem. This greatly improves debugging efficiency.


Assertion timeout

Auto-retrying assertions time out after 5 seconds by default; this can be customized in the config file or per assertion:

Example

// Set timeout for a single assertion (in milliseconds)

await expect

(

page.

getByText

(

'Loading...'

)

)

.

toBeHidden

(

{

timeout

:

10000

}

)

;

// Set globally in the config file

// playwright.config.ts:

// export default defineConfig({

//   expect: { timeout: 10000 },

// });