How to write a loop while in Robot Framework
Robot Framework does not have a while loop. You must use the FOR-loop and "exit for loop if" keywords to exit. It will run in a finite time, but if you select a large enough number in range, it is close enough for practical purposes.
*** Test Cases ***
For Test
FOR ${i} IN RANGE 999999
Exit For Loop If ${i} == 9
Log ${i}
END
Log Exited
This are other kinds of FOR Loops in Robot Framework, I have this on my own notes and its very helpfull.
FOR Loop with Upper Bounds Range
[Documentation] This gives us a 0 based range
FOR ${Index} IN RANGE 5
Do Something ${Index}
${RANDOM_STRING} = Generate Random String ${Index}
Log ${RANDOM_STRING}
END
FOR Loop with Start and Finish Range
[Documentation] No longer a 0 based range because I provided start
FOR ${Index} IN RANGE 1 4
Do Something ${Index}
${RANDOM_STRING} = Generate Random String ${Index}
Log ${RANDOM_STRING}
END
FOR Loop with Start, Finish, and Step Range
[Documentation] The counter will jump by 2 each time ("step" value = 2)
FOR ${Index} IN RANGE 1 10 2
Do Something ${Index}
${RANDOM_STRING} = Generate Random String ${Index}
Log ${RANDOM_STRING}
END
#index for elements in for
${index} = Set Variable 0
FOR ${col} IN @{cols}
${colum} Format String css:div[class='v-widget v-has-caption v-caption-on-top'] table[aria-rowcount='{0}'] tbody tr:nth-of-type({1}) td:nth-of-type(10) ${r_count} ${col}
Click element ${colum}
press Keys none ${net_config.broadcast}
Press Keys none TAB
Press Keys none ${net_config.${index}}
${index}= Evaluate ${index} + 1
END
#-------
FOR Loop with List
@{ITEMS} = Create List Item 1 Item 2 Item 3
FOR ${MyItem} IN @{ITEMS}
Log ${MyItem}
END
Exit a FOR Loop
@{ITEMS} = Create List Item 1 Item 2 Item 3 Item 4
FOR ${MyItem} IN @{ITEMS}
Log ${MyItem}
Run Keyword If "${MyItem}" == "Item 3" Exit For Loop
Log Didn't exit yet
END
Log Now we're out of the loop
You might be looking for the Wait Until Keyword Succeeds
keyword, which enables you to do a similar construction to a while loop. It is much more readable than FOR
cycles with conditional exiting.
You then use your custom keyword, which fails when you need to end the "loop".