Playwright's Assertion Library
An assertion is a way to verify whether the application's actual result matches the expected result.
A simple analogy is checking homework to confirm whether the answers are correct.
Another example is a security guard at a shopping mall checking:
- Is the door open?
- Is the shop name correct?
- Is the light turned on?
If something is incorrect, the guard raises an alert.
Similarly, Playwright assertions validate the application's behavior and fail the test if the expected condition is not met.
Interview Answer
"Playwright uses its built-in Playwright Test assertion library, based on expect() from
@playwright/test. It provides auto-waiting assertions that automatically wait until the expected condition is satisfied before failing the test. These assertions support web elements, web pages, and API responses, making automation more stable and reliable."
Assertion Library Details
Playwright assertions are provided through:
@playwright/test
using the familiar:
expect()
syntax.
Supported Assertions
UI Assertions
toBeVisible()toHaveText()toBeEnabled()
API Assertions
- Status code validation
- Response body validation
Main Advantage
The biggest advantage of Playwright assertions is auto-waiting.
Instead of failing immediately, Playwright continuously polls until:
- The expected condition becomes true, or
- The configured timeout is reached.
This removes the need for manual synchronization.
Asserting the URL
URL validation is performed using:
await expect(page).toHaveURL('https://example.com/dashboard');
Interview Answer
"In Playwright, URL validation is performed using
expect(page).toHaveURL(). The assertion automatically retries while the page is navigating or redirecting until the expected URL is reached or the timeout expires."
This works especially well after:
- Login
- Logout
- Payment gateway redirects
- Navigation between application pages
Validating Text
Playwright provides two commonly used text assertions.
Exact Text Match
await expect(page.locator('.welcome')).toHaveText('Welcome, Naveed');
Uses:
toHaveText()
Partial Text Match
await expect(page.locator('.message')).toContainText('successfully');
Uses:
toContainText()
Validating Element Visibility
Element visibility is validated using:
await expect(page.locator('#dashboard')).toBeVisible();
The assertion automatically waits until:
- The element becomes visible, or
- The configured timeout expires.
This eliminates the need for explicit waits before validating UI elements.
Validating Disabled State
Disabled elements are validated using:
await expect(page.locator('#submit')).toBeDisabled();
This confirms that the element cannot be interacted with.
The opposite validation uses:
toBeEnabled()
to verify that an element is enabled.
Validating Element Count
Element count validation uses:
await expect(page.locator('.cart-item')).toHaveCount(3);
This assertion verifies that the locator matches the expected number of elements.
It is commonly used for validating:
- Product lists
- Search results
- Tables
- Grid items
- Dynamic collections
Validating Dropdown Values
Dropdown values are validated by locating the dropdown options and verifying their displayed text using Playwright assertions.
Exact Value Validation
Use:
toHaveText()
Partial Value Validation
Use:
toContainText()
Interview Answer
"I locate the dropdown options and validate their values using
toHaveText()for exact matching andtoContainText()for partial matching."
Dropdown validation is typically performed after selecting an option to ensure that the correct value has been selected.
Taking Screenshots
A screenshot is simply a captured image of the application.
Screenshots are commonly used:
- During failures
- At important validation points
- For debugging
- For reporting
Capture a Screenshot
await page.screenshot({
path: 'checkout.png',
fullPage: true
});
Screenshot Options
Entire Page
Use:
fullPage: true
to capture the complete web page.
Specific Element
Playwright also supports capturing screenshots of individual elements instead of the full page.
Automatic Failure Screenshots
Screenshots can be automatically captured whenever a test fails by enabling screenshot configuration in Playwright Test.
Real Project Usage
"We enabled automatic screenshots on failure and captured full-page screenshots for failed executions. This provided visual evidence for debugging and made issue analysis much easier."
FAQs
What assertion library does Playwright use?
Playwright uses its built-in assertion library provided by:
@playwright/test
It uses the expect() syntax and supports auto-waiting assertions for web elements, pages, and API responses.
How do you validate the URL?
Use:
expect(page).toHaveURL()
The assertion automatically retries while navigation or redirects are occurring.
How do you validate text?
Use:
toHaveText()for exact matching.toContainText()for partial matching.
How do you validate element visibility and disabled state?
Use:
toBeVisible()toBeDisabled()
To validate enabled elements, use:
toBeEnabled()
How do you validate the number of elements?
Use:
toHaveCount()
to verify that the expected number of elements exists.
How do you validate dropdown values?
Locate the dropdown options and verify them using:
toHaveText()toContainText()
depending on whether exact or partial matching is required.
How do you take screenshots in Playwright?
Use:
page.screenshot()
Options include:
- Full-page screenshots using
fullPage: true - Element-level screenshots
- Automatic screenshots during test failures