With conditionals, you can handle testing scenarios that sometimes require action, such as dismissing a consent dialog, completing a verification prompt, or skipping a step when a value is already set. This article explains how to author conditional steps in the mabl Trainer, including how to…
- Add a conditional step
- Check multiple conditions before taking action
- Take action when any one of several conditions is true
- Exit a loop when a condition is met
Add a conditional step
In the mabl Trainer, take the following steps to add a conditional step:
- Click on + (Add step) > Conditionals.
- Select a specific conditional step type: IF, ELSE IF, ELSE
- If you are creating an IF or ELSE IF step, choose what you want to base the condition on:
- An assertion, including elements, URL, cookie, variable, and visual assertions.
- Advanced find assertion using CSS or XPath
- Output from a JavaScript snippet
- Record the steps that you want to run if the condition evaluates to true.
Options for an IF step
When you add an IF step, the mabl Trainer adds an ELSE step and an END step by default.
- If you don’t need the ELSE step, you may delete it.
- The END step is required. If you delete it, the mabl Trainer adds a warning icon to the conditional step indicating that the statement has no END step.
Check multiple conditions before taking action
To ensure a set of circumstances is met before executing steps, use one of the following methods to implement IF… AND… logic:
JavaScript snippet
If your team is comfortable writing and maintaining JavaScript, create an IF step, and set the IF condition based on JavaScript. Write a snippet to evaluate whether X and Y are true.
Nested conditionals
Create an IF step to evaluate for the first scenario. Inside the IF step, add another IF step to evaluate for the second scenario. Inside the nested IF step, record the steps that you want to run if both scenarios are true.
In the following example, the test clicks on a search result if it matches the correct categories: “Creating tests” and “Performance tests.”
Using nested IF statements to implement IF… AND… logic
Take action when any one of several conditions is true
If your test needs to execute steps when one of several conditions is true, use one of the following methods to implement IF… OR… logic:
JavaScript snippet
If your team is comfortable writing and maintaining JavaScript, create an IF step, and set the IF condition based on JavaScript. Write a snippet to evaluate whether X or Y are true.
Combine conditionals
You can also implement IF… OR… logic by combining conditionals:
- Create a variable called
either_situationand assign it the value “false.” - Create an IF step to evaluate for the first scenario.
- Add an ELSE IF step to evaluate for the second scenario.
- Inside both the IF and ELSE IF blocks, create a variable called
either_situationand assign it the value “true.” This action updates the value ofeither_situationtotrue. - After the END step, add a new IF step that checks if the variable
either_situationequals “true.” - Inside the IF step, record the steps that you want to run if either scenario is true.
For example of IF… OR… logic, the following screenshot shows a test that clicks the “SUBMIT” button if the file upload is either records.csv or roster.csv.
Implementing IF…OR…logic with variables and conditionals
Exit a loop when a condition is met
You can create a de facto while loop that exits the loop when a specific condition is met. See the article on Using loops > How to exit a loop early for more information.