Assertions in Playwright Python
Assertion is verifying whether the result we expected is the actual result. For example, we might be looking for a value to be present inside a webpage and we extract that value (actual) and compare it with our expectation.
The assertion only helps to fail a test but doesn’t have anything to do with passing the test
Why do we need assertion?
When we run automation, we will be doing multiple operations like clicks, setting values, and drag-dropping the items in the webpage but if any of the action fails, then the test case fails. This is called failure of action and works as a kind of indirect assertion.
In reality, along with this indirect assertion we have to verify directly. That means if there is a calculation that adds two numbers, let’s say 3+5 and we expect a result to be 8, and to ensure this thing we write a statement, and that ensuring is nothing but an assertion.
Python Assertion
We are currently writing all our scripts using Playwright and Python programming languages, whenever there is a programming language involved, the programming language itself provides few assertions. Especially with Python, we use only one assertion mostly that is assert
.
assert condition_results_boolen
When we are using the assert
keyword should be followed with a condition that results in a boolean value.
def test_addition():
assert 3 > 5
# the test fails and output is
The test fails and the output is
test_math.py::test_addition FAILED [100%]
3 != 5
Expected :5
Actual :3
def test_addition():
> assert 3 > 5
E assert 3 > 5
test_math.py:2: AssertionError
Playwright Assertions
There are assertions provided by the Playwright. We can use these assertions to verify the values/items present on the webpage. These assertions or present in other automation tools as well, but the different names.
Locator Assertions
Locator assertions are nothing, but the assertions that can be used on the elements present in the webpage. There are more than 50 locators-related assertions but we are going to see the most used assertions in this article.
The syntax for the locator assertion is expect(locator).condition()
.
expect(page.locator("#ch")).to_be_checked()
# check whether a checkbox checked or not
expect the function that will hold the locator and we will be using the condition function to verify whether the condition matches or not
In the below example, we will learn how to use playwright locator assertion and just use assert
keyword from Python programming language serving the same purpose.
Purpose: fail test case if the check box is not checked.
Navigate to https://demo.testercoder.com/waits.html and look for the below element. If you click the button (Check Checkbox) the checkbox will be checked after 10 seconds. Our expectation is the checkbox should be checked after 10 seconds.
We are using the to_be_checked()
assertion to verify whether a checkbox is checked or not.
from playwright.sync_api import sync_playwright, expect
playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://demo.testercoder.com/waits.html")
page.locator("#checkbox").click()
expect(page.locator("#ch")).to_be_checked()
Now the same thing can be achieved using the Python assert keyword. First, we store whether the checkbox is checked or not. If the checkbox is checked then is_tick becomes true and the assert becomes pass otherwise assert fails the test case.
page.locator("#checkbox").click()
is_tick = page.locator("#ch").is_checked()
assert is_tick
Now if you have truly executed the above code, you could notice both ways failed, because we are not overriding default timeout but the checkbox will be checked only after 10 seconds, so the test fails.
Influence Assertions with timeout:
We saw both ways failed because of timeout, we were expecting the checkbox to be checked within 5 seconds, in fact, we had to wait 10 seconds for the checkbox to check itself. So the test fails. Now let’s increase the timeout to 15000 ms (15 seconds)
expect(page.locator("#ch")).to_be_checked(timeout=15000)
or
is_checked = page.locator("#ch").is_checked(timeout=15000)
assert is_checked
The above solution solves the problem but the timeout only works for that particular line. In reality, you will have lots of expect statements, so we have to set the timeout for every expect statement.
Note: If the condition is met before the given timeout then, the execution moves to the next statement.
We can do that by using expect.set_options(timeout=15000)
, Now every expect statement will wait for 15 seconds by default.
pw = sync_playwright().start()
page = pw.chromium.launch(headless=False).new_page()
page.goto("https://demo.testercoder.com/waits.html")
page.locator("#checkbox").click()
expect.set_options(timeout=15000)
expect(page.locator("#ch")).to_be_checked()
Now, you have a situation where you might be testing some negative scenario. For which you have to use timeout only for that line, but already you have timeout globally using expect.set_options(timeout=15000)
, In such cases just write timeout on the statement itself, the statement level timeout has more priority than the global level timeout.
expect.set_options(timeout=15000)
expect(page.locator("#ch")).to_be_checked(7000) # Priority
Enabled and Disabled: We can assert whether an element is enabled or disabled using to_be_enabled()
and to_be_disabled()
. For example, Most of the sites will not be able to login button unless the user fills username and password.
# element enabled or not
expect(locator).to_be_enabled()
# whther element diabled
expect(locator).to_be_disabled()
Editable or not: Check whether an input box is editable or not, meaning whether the field accepts user input or not. We can use to_be_editable()
to whether the element is enabled or not.
Visible or hidden: Check whether the element is visible to the user or not. Use to_be_visible()
if you expect an element visible otherwise use to_be_hidden()
.
Important Note: Irrespective of whether the element is visible or not hidden, the element should be present on the page. Hidden does not mean the element is not present on the page, it is just that you cannot see it.
No text is present on Input: You can assert whether the Input field is empty or not. If empty test will move on otherwise test will fail.
pw = sync_playwright().start()
page = pw.chromium.launch(headless=False).new_page()
page.goto("https://demo.testercoder.com/waits.html")
expect(page.locator("#with-input")).to_be_empty()
I have written more locator assertions bottom of this article.
Page Assertions
Page assertion follows the same pattern as locator assertion but instead of an element, the assertion will be applied on the page.
URL assertion:
If you like to assert whether the current URL matches your expectations.
#expect(page).to_have_url('url_expected')
from playwright.sync_api import sync_playwright, expect
pw = sync_playwright().start()
page = pw.chromium.launch(headless=False).new_page()
page.goto("https://demo.testercoder.com/waits.html")
expect(page).to_have_url("https://demo.testercoder.com/waits.html")
expect(page).to_have_title("Waits Practice Page")
Similarly, you can assert whether the title of the page matches or not.
expect(page).to_have_title("Waits Practice Page")
You can also check the response of the goto()
function.
from playwright.sync_api import sync_playwright, expect
pw = sync_playwright().start()
page = pw.chromium.launch(headless=False).new_page()
resp = page.goto("https://demo.testercoder.com/waits.html")
expect(resp).to_be_ok()
Less used Assertions:
- to_be_checked(): Verify element is checked
- not_to_be_checked(): Verify element is not checked
- to_be_disabled(): Verify element is disabled
- not_to_be_disabled(): Verify element is not disabled
- to_be_enabled(): Verify element is enabled
- not_to_be_enabled(): Verify element is not enabled
- to_be_editable(): Verify text input field is editable
- not_to_be_editable(): Verify text input field is not editable
- to_have_text(): Verify element has text
- not_to_have_text(): verify element doesn’t have text
- to_contain_text(): verify element contains at least partial text
- not_to_contain_text(): Verify element doesn’t contain text
- to_have_value(): Verify given dropdown have the value
- not_to_have_value(): Verify given dropdown doesn’t have the value
- to_have_values(): Verify given dropdown have the values
- not_to_have_values(): Verify given dropdown doesn’t have the values
- to_be_empty(): Verify the text input bar is empty
- not_to_be_empty(): Verify text input bar is not empty
- to_be_hidden(): Verify element is hidden
- not_to_be_hidden(): Verify element not hidden
- to_be_in_viewport(): Verify element is present in the viewing area of the screen
- not_to_be_in_viewport(): Verify element is present in the viewing area of the screen
- to_have_count(): Verify element matches the count
- not_to_have_count(): Verify element doesn’t match the count
- to_have_role(): Verify element has the attribute ‘role’ with the given value
- not_to_have_role(): Verify element doesn’t have the attribute ‘role’ with the given value
- to_be_attached(): Verify element is attached
- not_to_be_attached(): Verify element is not attached
- to_be_focused(): Verify the current focus is on the element
- not_to_be_focused(): Verify the current focus is not on the element
- to_be_visible(): Verify element to be visible
- not_to_be_visible(): Verify element is not visible
- to_have_attribute(): Verify element has the attribute
- not_to_have_attribute(): Verify element doesn’t have the attribute
- to_have_class(): Verify element has the given class
- not_to_have_class(): Verify element does not have the given class
- to_have_id(): Verify element has the given id attribute
- not_to_have_id(): Verify element does not have the given id attribute
- to_have_css(): Verify element has the CSS
- not_to_have_css(): Verify element does not have the CSS
- to_have_js_property():
- not_to_have_js_property():
- to_have_accessible_description():
- not_to_have_accessible_description():
- to_have_accessible_name():
- not_to_have_accessible_name():
Posts You Might Like
- Why playwright is better than selenium webdriver, is it?
- Handle dropdowns in Playwright Python
- Open the browser and Close in Playwright Python
- Handle checkbox in Playwright Python
- Handle IFrames in Playwright Python
- Element Operations in Playwright Python
- Page level commands in Playwright Python
- Element State with Playwright Python