Assertions are one of the most important parts of any automation framework. They help verify whether the application behaves as expected during test execution.
Playwright provides powerful built-in assertions that automatically wait for conditions to become true, making tests more stable and reliable.
In this article, we'll explore the most commonly used Playwright assertions with Java examples.
What Are Assertions?
Assertions validate the actual result against the expected result.
For example:
- Verify page title
- Verify element visibility
- Verify text values
- Verify URLs
- Verify attributes
Without assertions, an automation script simply performs actions without confirming whether the application works correctly.
Setting Up Assertions in Playwright
Import the assertion package:
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;1. Verify Element Visibility
This is one of the most commonly used assertions.
Locator loginButton = page.locator("#loginButton");
assertThat(loginButton).isVisible();This assertion automatically waits until the element becomes visible.
2. Verify Element Contains Text
Locator successMessage = page.locator(".success-message");
assertThat(successMessage)
.containsText("Login Successful");Useful for validating confirmation messages and notifications.
3. Verify Exact Text
Locator heading = page.locator("h1");
assertThat(heading)
.hasText("Dashboard");This checks for an exact match.
4. Verify Page Title
assertThat(page)
.hasTitle("Amazon.in");This is useful for verifying navigation success.
5. Verify Current URL
assertThat(page)
.hasURL("https://www.amazon.in/");This confirms that the user is redirected to the correct page.
6. Verify Checkbox Is Checked
Locator rememberMe = page.locator("#remember");
assertThat(rememberMe)
.isChecked();7. Verify Element Is Enabled
Locator submitButton = page.locator("#submit");
assertThat(submitButton)
.isEnabled();Useful before clicking buttons or submitting forms.
8. Verify Element Is Disabled
Locator checkoutButton = page.locator("#checkout");
assertThat(checkoutButton)
.isDisabled();9. Verify Input Field Value
Locator username = page.locator("#username");
assertThat(username)
.hasValue("admin");10. Verify Attribute Value
Locator logo = page.locator("#companyLogo");
assertThat(logo)
.hasAttribute(
"alt",
"Company Logo"
);Why Playwright Assertions Are Better
Traditional frameworks often require explicit waits:
Thread.sleep(5000);
Assert.assertTrue(element.isDisplayed());Playwright eliminates this problem through automatic waiting:
assertThat(locator).isVisible();This makes tests:
- Faster
- More stable
- Less flaky
- Easier to maintain
Soft Assertions vs Hard Assertions
Playwright assertions are generally hard assertions.
If an assertion fails:
- Test execution stops immediately.
- Remaining steps are skipped.
This behavior helps identify failures quickly.
Best Practices
Use meaningful assertions
Good:
assertThat(loginSuccessMessage)
.containsText("Welcome");Avoid:
assertThat(page.locator("div"))
.isVisible();Validate business functionality
Assertions should verify actual business outcomes rather than implementation details.
Examples:
- Order placed successfully
- User logged in
- Payment completed
Avoid unnecessary assertions
Too many assertions can make tests harder to maintain.
Focus on validating critical application behavior.
Common Assertions Cheat Sheet
| Assertion | Purpose |
|---|---|
| isVisible() | Verify element visibility |
| isHidden() | Verify element is hidden |
| hasText() | Verify exact text |
| containsText() | Verify partial text |
| hasValue() | Verify input value |
| isChecked() | Verify checkbox selection |
| isEnabled() | Verify enabled state |
| isDisabled() | Verify disabled state |
| hasURL() | Verify page URL |
| hasTitle() | Verify page title |
| hasAttribute() | Verify attribute values |
Conclusion
Assertions are the backbone of any automation framework.
Playwright simplifies assertions by providing built-in auto-waiting capabilities, making tests more reliable and easier to maintain.
In this article, we covered:
- Visibility assertions
- Text validation
- URL verification
- Title checks
- Attribute validation
- Input field assertions
Mastering these assertions will significantly improve the quality of your automation scripts and reduce flaky test failures.
In the next article, we will learn how to capture screenshots and videos in Playwright using Java.
No comments:
Post a Comment