How to iterate through a block of code until a condition is met
In the solution shown below, you will see that multiple navigations are needed while calculating the Age of a person by selecting the birth date in "Birthday to Age Calculator" from the HICC application. Here, you will need to navigate to previous years where the Navigation arrow should be clicked multiple times until the required year is visible and selected. To achieve this, Looping commands can be used to perform the same action repeatedly.
Approach #1: Using for-endFor commands
# | Command | Target | Value |
---|---|---|---|
Verify and access the Birthday to Age Calculator | |||
1 | assertVisible | css=#birthdayToAgeCalculator > img | |
2 | click | css=#birthdayToAgeCalculator > img | |
Verify the text present on the popup | |||
3 | assertText | id=birthdayToAgeCalculatorHeaderText | Birthday To Age Calculator |
Click on the date input field | |||
4 | click | id=mobileDatepicker | |
Verify the calendar opened | |||
5 | assertElementPresent | //datepicker/div/div/div/div[2] |
|
Access Year | |||
6 | click | css=a > span.ng-binding | |
Start For loop to navigate to previous years | |||
7 | for | i=0;i<=8;i++ | |
8 | setTimeout | 10000 |
|
store the visibility of required Year | |||
9 | storeVisible | link=1974 | YearVisibility |
10 | setTimeout | 120000 | |
If year visibility is true break the loop | |||
11 | if | "${YearVisibility}"=="true" | |
12 | break | ||
else click on the left arrow to navigate to previous years | |||
13 | else | ||
14 | click | //div[2]/a/b/img |
|
End if and for blocks | |||
15 | endIf | ||
16 | endFor | ||
Select the year | |||
17 | click | link=1974 |
Approach #2: Using while-endWhile commands
# | Command | Target | Value |
---|---|---|---|
Store the visibility of required Year | |||
10 | setTimeout | 15000 | |
11 | storeVisible | link=1974 | YearVisibility |
12 | setTimeout | 120000 | |
If the year visibility is true click on the Year | |||
13 | if | "${YearVisibility}"=="true" | |
If year visibility is true break the loop | |||
14 | click | link=1974 | |
else start WHILE loop and access left arrow to navigate to previous years | |||
15 | else | ||
16 | while | "${Date}"!="true" | |
17 | click | //div[2]/a/b/img | |
Store the visibility of required Year | |||
18 | setTimeout | 15000 | |
19 | storeVisible | link=1974 | Date |
20 | setTimeout | 120000 | |
End WHILE block | |||
21 | endWhile | ||
Select the required year | |||
22 | click | link=1974 | |
End IF block | |||
23 | endIf |
Tips, Tricks, Gotchas & Best Practices:
- Tip #1: Looping command blocks can be dragged and dropped within the test script along with the instructions present in the block
- Tip #2: Multiple instructions within a test script can be moved to Looping commands block by selecting them using the ctrl key and dragging them to the required block. Please find the following gif for reference
- As a best practice, it is not suggested to use looping commands to iterate multiple sets of test data instead use Data-Driven concept
Representative Usage Scenarios for this Example:
Use Case #1:
For any application that has search and the click results in multiple records where data validation should be performed. Then, you can implement validation of records' data using the looping statements.