Cross_Column

Monday, 23 March 2026

Assertions & Validations In PlayWright



Playwright Assertions & Validations with Python

Assertions play a crucial role in automation testing because they verify whether the application is behaving as expected. Without validations, automation scripts would only perform actions without checking the results.

Playwright provides powerful built-in assertion capabilities that allow testers to validate page content, element visibility, URLs, titles, and many other conditions.

In this tutorial, we will explore different types of validations that can be performed using Playwright with Python.


Assertions in Playwright

Assertions are used to verify that the actual result of a test matches the expected result. Playwright provides built-in assertion methods through its testing library.

Example of a simple assertion:

from playwright.sync_api import expect

expect(page).to_have_title("Google")

If the page title does not match the expected value, the test will fail automatically.


Validating Page Title

One of the most common validations in web automation is checking the page title to ensure that the correct page has been loaded.

from playwright.sync_api import expect

expect(page).to_have_title("Example Domain")

This assertion verifies that the current page title matches the expected value.


Validating Text on Page

Sometimes we need to verify that specific text appears on a webpage. Playwright provides assertions that validate the text content of elements.

expect(page.locator("h1")).to_have_text("Welcome")

This verifies that the heading element contains the text "Welcome".

You can also validate partial text.

expect(page.locator("#message")).to_contain_text("Success")

Element Visibility Validation

Validating whether an element is visible on the page is another common automation task.

expect(page.locator("#login-button")).to_be_visible()

This assertion verifies that the login button is visible to the user.

You can also check if an element is hidden.

expect(page.locator("#loading")).to_be_hidden()

URL Validation

Another useful validation is verifying the current page URL. This ensures that navigation has occurred successfully.

expect(page).to_have_url("https://example.com/dashboard")

This assertion checks whether the browser has navigated to the expected URL.


Playwright Assertions – Quick Summary

The following table provides a quick reference for commonly used validation methods in Playwright.

Validation Type Playwright Assertion Description Example
Page Title to_have_title() Verify the title of the webpage expect(page).to_have_title("Google")
Page URL to_have_url() Validate current browser URL expect(page).to_have_url("https://example.com")
Element Text to_have_text() Verify exact text of an element expect(locator).to_have_text("Welcome")
Partial Text to_contain_text() Verify partial text inside element expect(locator).to_contain_text("Success")
Element Visible to_be_visible() Verify element is visible expect(locator).to_be_visible()
Element Hidden to_be_hidden() Verify element is not visible expect(locator).to_be_hidden()

Conclusion

Assertions and validations are essential for verifying application behavior during automation testing. Playwright provides powerful and easy-to-use assertion methods that allow testers to validate UI elements, page titles, URLs, and text content efficiently.

By using proper validations, automation engineers can ensure that their tests not only perform actions but also verify that the application is working as expected.

In the next tutorial, we will explore Advanced Playwright Features such as Screenshots, Video Recording, and Network Interception.

Next Topic

No comments:

Post a Comment

Few More

Paraller Excution in PlayWright

Parallel Execution in Playwright with Python Automation test suites can become very large as the project grows. Runni...