Article Overview:
This article will cover how you can achieve the following:
How to click on an element based on a condition.
Problem Statement:
In automated testing, executing test steps based on dynamic conditions is crucial for handling various scenarios that depend on variable states or specific criteria. However, users often face challenges in implementing conditional logic within test steps, which may lead to inefficient or incomplete test flows. Without flexible options for conditional execution, test cases may not adapt to real-world complexities, making it harder to create robust and reliable test scenarios.
This article provides solutions for implementing conditional execution in Virtuoso, allowing users to execute steps based on specific criteria, ensuring more adaptable and dynamic test flows.
Solution:
To allow you to create flexible and dynamic test scenarios, Virtuoso offers the flexibility to conditionally execute test steps based on specific criteria.
There are three options available for conditional execution:
- Always: This option ensures that the step is always executed during the test, regardless of any conditions.
-
If...: With this option you can define simple conditions using an intuitive and self-explanatory interface to determine whether the step should be executed. You start by specifying a variable using the syntax $variable. Then, there are six operator choices available for defining the condition:
- is defined: When selecting the is defined operator, the condition will evaluate to true if the variable is defined and has a value. By using the is defined operator, you can execute a step based on the presence of a specific variable.
- is not defined: The is not defined operator functions as the inverse of the is defined operator. It evaluates to true if the specified variable is not defined or does not have a value. This operator allows you to execute a step when a particular variable is absent or empty.
- is empty: The is empty operator will execute the step if a variable is defined, but has no value. Note that the step will fail if the variable is undefined.
- is not empty: The is not empty operator will execute the step if a variable is defined, and has any value. As above, the step will fail if the variable is undefined.
- equals: The equals operator allows you to compare the values of two variables, or the value of a variable against a fixed value. If the values are equal, the condition will evaluate to true, and the step will be executed.
- is not equal to: Similar to the equals operator, the is not equal to operator compares the values of two variables, or the value of a variable against a fixed value. If the values are not equal, the condition will evaluate to true, and the step will be executed.
Supported syntax:
To define a condition variable, you can optionally use the dot notation to access an object's property ($variable.prop1.prop2).
For using the bracket notation, please use the "Advanced" option explained below.
When using a fixed value as the condition's right operand, it has to be either a number (e.g. 42.5) or a string (e.g. English).
- Advanced: This option allows you to execute the step based on the evaluation of a JavaScript (JS) expression. If the JS expression evaluates to a true value, the step will be executed. Otherwise, it will be skipped.
Examples:
- $languageToggle!==English || $location===France
- $languageToggle===English && $location!==France
- $response.statusCode === 200 && $response.data.length === 23
- ($response.content.includes("Success") && $response.headers['Content-Type'] === 'application/json') || $response.headers['X-Custom-Header'] === 'valid'
Note:
These conditional execution options provide you with precise control over the flow and behaviour of your tests, allowing you to customize the execution based on variable values and their relationships.
For more detailed information and examples on working with variables, please consult the Variables Guide.
It's important to note that these conditions affect both live authoring and regular executions.
This means that you can test and validate the behaviour of your conditions in real-time during the authoring process and ensure they function as intended during execution.
To make it easier to identify steps with applied conditions, Virtuoso includes an indicator icon next to their names.
This visual cue helps you quickly identify which steps have conditional execution logic associated with them, making it easier to navigate and understand your test flow.
Additionally, the result of evaluating a condition will appear in the side effects tab.
This provides you with visibility into the outcome of the condition evaluation, allowing you to verify if the expected conditions are met or if any adjustments are needed.
The side effects tab displays the condition's evaluation result, providing insights into how the condition influenced the execution flow.
Comments
0 comments
Please sign in to leave a comment.