Skip to main content

Assertions

Assertions are used to check expected behaviors or outcomes in tests. At Qualiti, you can add an assertion to an existing Step in a Test Case, or create a new Step -- meaning that you can have as many assertions as you want.

Since we ultimately generate Playwright tests, we can leverage their powerful expect() implementation which includes many generic matchers like to_be_okto_contain_textto_have_url, or specific assertions for the current webpage or a specific element.

info

For more info regarding assertions, visit Playwright Assertions

Add an Assertion

To add an assertion, open a Test Case and switch to the Steps tab to see the list of steps. Then either click the Edit or Add icon on any step to open the Test Step Modal.

  1. Observe that there are the Locate, Act, and Assert textboxes
  2. Within the Assert textbox, you can add any expect() condition (👀 more details below)
  3. Once you've filled out these textboxes (if needed) and added your assert, click Save

Writing Assertions

Within each Test Step, there are two main objects you have access to:

  • page - represents the context of the current webpage
  • element - represents the element that was found in the Locate textbox

Once you've identified what you want to assert, then edit an existing step or add a new step to automate it! Let's use some examples to illustrate this. We will be using OrangeHRM as the example so you can follow along!

Example: Assert login was successful using the URL

Here's simple Gherkin to describe the behavior we're checking:

GIVEN I enter valid admin credentials
WHEN I attempt to login
THEN I should login successfully with the URL ending in /dashboard/index

Qualiti auto-generated this Test Case but didn't add the exact assertion I was looking for. I can open the Test Case in the Qualiti Portal and add a new Test Step!

Current Steps:

  1. Enter 'Admin' into username field
  2. Enter 'admin123' into password field
  3. Click Login button

At this point, I could either add my assertion into Step 3 or I could create a new Test Step just for this assertion. In this scenario, I think a new Test Step makes more sense, so I click the Add icon on Step 3 to open the Test Step Modal and enter the following info:

  • Select the option to add the new step after this step

  • Step Name: Admin should land on the /dashboard/index page

  • Locate: [leave blank] -- we don't need to find an element for this check

  • Act: [leave blank] -- we don't need to perform any other actions for this check

  • Assert: expect(page).to_have_url(re.compile("/dashboard/index$"))

    tip

    .to_have_url() can take a string or regex expression

Once I save the new Test Step, my list of steps should look like this:

  1. Enter 'Admin' into username field

  2. Enter 'admin123' into password field

  3. Click Login button

  4. Admin should land on the /dashboard/index page

    tip

    Words matter! You may find it more readable to use the word Validate or Assert at the start of the Step Name instead.

Example: Assert login was successful using an element

I'll use simple Gherkin to describe the behavior we're checking:

GIVEN I enter valid admin credentials
WHEN I attempt to login
THEN I should login successfully with my username in the Account Dropdown

Qualiti auto-generated this Test Case but didn't add the exact assertion I was looking for. I can open the Test Case in the Qualiti Portal and add a new Test Step!

Current Steps:

  1. Enter 'Admin' into username field
  2. Enter 'admin123' into password field
  3. Click Login button

At this point, I could either add my assertion into Step 3 or I could create a new Test Step just for this assertion. In this scenario, I think a new Test Step makes more sense -- especially since we are looking for a specific element -- so I click the Add icon on Step 3 to open the Test Step Modal and enter the following info:

  • Select the option to add the new step after this step

  • Step Name: Admin's username is in the Account Dropdown

  • Locate: page.locator(".oxd-userdropdown")

  • Act: [leave blank] -- we don't need to perform any other actions for this check

  • Assert: expect(element).to_be_visible()

    tip

    Remember, the element object refers to the element we found in the Locate textbox

Once I save the new Test Step, my list of steps should look like this:

  1. Enter 'Admin' into username field
  2. Enter 'admin123' into password field
  3. Click Login button
  4. Admin's username is in the Account Dropdown