Skip to main content

While Loops

Cycle has the ability to repeat groups of Steps using while loops. The basic syntax and flow of a while loop are shown in the diagram below.

While loop

Cycle will repeat all the Steps between the While and the EndWhile keywords until the conditional Step fails. When the conditional Step fails, execution will continue at the first Step after the EndWhile. The Feature File shown in the following snippet will continue to prompt the user for a value until the user replies with a value not equal to Y.

Feature: My Feature

Scenario: Prompt and Repeat
Given I assign "Y" to variable "Answer"
While I verify text $Answer is equal to "Y" ignoring case
Then I prompt "Continue Prompting? (Y/N)" and assign user response to variable $Answer
EndWhile

Any Step can be used as the conditional Step of a while statement. This functionality allows you to create while loops that are based on graphic images or even other Scenarios.

The snippet below demonstrates using a graphic image for a conditional Step. When executed, the script will stay in the while loop until the Cycle Labs Logo is no longer found when scanning the screen.

Feature: WhileLoop2

Scenario: Is Cycle Labs Website Open?
While I see image "Image:images/CycleLog.png" within 2 seconds
Then I echo "website is open"
EndWhile