Sometimes it is not possible to determine in advance how many times a loop should be repeated. Here is an example:
A user queries a Check Status page multiple times until the server completes a slow, asynchronous transaction and redirects to a Result page. When recording such a test case, it is unknown how many times during the load test each virtual user should query the Check Status page. Therefore, it is necessary to use a Do...While (Conditional Loop) configure such a scenario. Conditional loops have an exit condition checked at the end of every loop iteration to determine if the loop should continue or exit. You can configure a loop to exit when the condition returns true or false.
Creating a while loop
- Go to the Build Test Case node.
- Select the starting object.
- Right-click to open the context menu and click Create Container -> Create Do...While.
- A new object dialog will appear.
- Select the last object in the loop
- Click Create Do...While .
Quick create
Another way to create a loop is to select multiple objects in the test case tree -> right-click -> select Create Do...While. This will generate a Do...While around the selected objects.
Exit condition types
There are 4 exit condition types:
- Compare expressions
- Search response
- Match extractor (deprecated)
- Match scriptable variable (deprecated)
Note:
Starting from v5.4, the preferred exit condition type is Compare expressions. Match extractor and Match scriptable variable have been deprecated.
Compare expressions
This type compares 2 expressions using a specified operand to determine whether to continue the loop. An expression is a combination of text and/or variables.
- In the Condition type (a) property, select Compare expressions
- Enter expressions to compare in the Left expression (b) and Right expression (d) properties. To insert a variable, hit the down arrow in the property grid to bring up the variable picker.
- In the Compare operator (c) property, select the operator to compare left and right expressions. The available operators are:
- =: Returns true if left and right expressions return the same value.
- >: Returns true if the left expression returns a value that is greater than the value returned by the right expression.
- >=: Returns true if the left expression returns a value that is greater than or equal to the value returned by the right expression.
- <: Returns true if the left expression returns a value that is less than the value returned by the right expression.
- <=: Returns true if the left expression returns a value that is less than or equal to the value returned by the right expression.
- Contains: Returns true if the left expression returns a value that is contained in the value returned by the right expression.
- Starts with: Returns true if the left expression returns a value that starts with the value returned by the right expression.
- Ends with: Returns true if the left expression returns a value that ends with the value returned by the right expression.
Exists: Returns true if the left expression's value is not empty and has a length greater than 0. Use this operand to test if an extractor is found.
Note
If looping until extractor is not found, then set the extractor's Override previous value when not found the property to Yes as noted here.
- In the Exit loop if compare condition is true? (e) property select Yes to exit the loop when the expression comparison evaluates to true and continue the loop when the expression comparison evaluates to true. Select No to exit the loop when the expression comparison evaluates to false, and continue the loop when the expression comparison evaluates to true.
Search response
The search response condition is based on finding a specified text or regular expressions in an HTTP response. For this condition type, specify the following:
- In the Condition type property, select Search response (10)
- In the Response to search (a), select from the dropdown the id whose HTTP response to analyze
- In the Search expression (b) property, specify an expression that will be searched in the HTTP response specified in response to the search property.
In the Search type (c) property, select Text if the Search string property is a text, or select RegEx if the Search expression property is a regular expression.
In the Exit loop if match found? (d) property select Yes exit the loop when the search text is found in the response, and continue the loop when the search text is not found in the response. Select No exit the loop when the search text is not found in the response, and continue the loop when the search text is found in the response.
Match Extractor (deprecated)
The match extractor condition is based on comparing an extractor value to a constant string.
- In the Condition type property, select Match Extractor (11)
- In the property Extractor to match (a), select an extractor of the drop-down to compare with the string. The extractor must be defined inside the Do...While object
- In the property Text-to-match (b), enter a string to compare with the extractor.
- In the Exit loop if match found? (c) property select Yes exit the loop when the extractor's value equals text-to-match and continue the loop when the extractor's value does not equal text-to-match. Select No exit the loop when the extractor's value equals text-to-match and continue the loop when the extractor's value does not equal text-to-match.
Match scriptable variable (deprecated)
The match scriptable variable condition is based on comparing a scriptable variable return value to a constant string.
- In the Condition type property, select Match scriptable variable (12)
- In the property Scriptable variable to match (a), select a scriptable variable from the drop-down to compare its value with the string.
- In the property Text-to-match (b), enter a string to compare with a value of the scriptable variable returned by the IExternalVariable.GetValue() method.
- In the Exit loop if match found? (c) property select Yes exit the loop when the external variable's value equals text-to-match and continue the loop when the external variable's value does not equal text-to-match. Select No exit the loop when the external variable's value equals text-to-match and continue the loop when the external variable's value does not equal text-to-match.
Other properties
- Number of Repeats (Max) (a) - A positive integer value of the maximum number of repeats cannot be exceeded. Make sure this value is higher than the maximum number of possible repeats.
- Delay before next Loop. (b) - A non-negative decimal number of seconds to wait after the previous iterations and the start of the next iteration.
Action on loop completion after max. repeats (c) - Specifies the action to execute when the loop completes the max number of repeats without exiting the condition. For example, if the test case fails to log in after a few attempts, it doesn't make sense to complete post-login transactions. The following options are possible:
Continue - To continue the iteration.
Abort iteration - To stop the current iteration and start a new one
Abort vu - To stop sending requests from this VU.
- To increment the index for iteration-bound datasets on every loop iteration, set the Increment the index for iteration-bound datasets? (d) property to Yes. For more information on databinding, see Databinding.