When testing an application, there are times when you may need to assert that an element is absent from the page. For example, after logging out, you might want to validate that a logged-in user profile is no longer present in the nav bar. Or after a delete action, you may want to confirm that the deleted item is no longer on the page.
Depending on what you're trying to validate, there are several ways to assert that an element is not present in the mabl Trainer:
- Work backwards with an element assertion
- Use a custom find assertion
- Assert on the element count
- Use a loop to wait until an element is absent
Asserting that an element is not present is not the same as asserting that an element is not visible:
- An "is not present" assertion passes if the target element doesn't exist in the HTML of the page.
- If the target element is present in the HTML but is hidden using an attribute like
display
,visibility
, oropacity
, an "is not present" assertion will fail. For browser tests, you can use a CSS assertion instead.
Work backwards with an element assertion
Create an "is not present" element assertion while the element is still on the page. When you create the assertion, it will run and fail. Then, move the assertion to the position in your test where you expect it to be absent.
See an example of this strategy in the following animation:
Working backwards to create an "is not present" assertion
For best results, add Configure Find to the "is not present" assertion:
- Select element attributes that are unlikely to change.
- Make sure that auto-heal is disabled. If an "is not present" assertion auto-heals to a different element that matches the Configure Find settings, the assertion will fail.
Use a custom find assertion
If the element doesn't appear during testing, but you can identify it with CSS or XPath, record a custom find assertion and select the "is not present" assertion type.
Assert on the count of elements
In addition to asserting that an element is not present, you can use custom find assertions to assert on the number of elements that match a given CSS or XPath query equals zero.
Asserting on the count of an element
Asserting on the count of elements is also useful when you want to assert on the number of elements before and after a change. For example, if you delete a record from a table, you can use a custom find assertion on the count of elements before and after the change to assert that the table shows one less record.
Use a while loop to wait until an element is absent
If your test case requires waiting until an element is absent from the page, you can create a loop that exits once the element is not present. To implement this workflow, take the following steps:
- Create a variable and assign it a number as a value.
- Create a flow that loops using the variable you just created.
- In the flow, add an IF step that asserts that the element is not present.
- In the IF step, add a variable step that creates a new variable with the same name as the looping variable, and set the value to 0.
- Add an ELSE step.
- In the ELSE step, add a static wait step.
In the following example, the variable loop_total
is initialized to 50. For a given loop iteration, if the toast tab is present, the test runs the ELSE scenario and waits 10 seconds. If the toast tab is absent, the test runs the IF scenario, which updates the value of loop_total
to 0 and causes the loop to exit early.
Waiting until an element is absent