If/Else Logic
Using If/Else
Cycle has the ability to use If / Else / ElsIf structures. The basic syntax of an if statement is shown in the following diagram. The only required elements of the if structure are the If and EndIf keywords. The use of Else and ElsIf elements are optional.
The snippet below demonstrates a Feature File that prompts the user for a number and If/Elsif/Else to determine if the number entered was 1, 2, 3, or none of those.
Feature: If_Else
Scenario: Simple If/Else
Given I prompt "Please enter a number" and assign user response to variable "Number"
If I verify number $Number is equal to 1
Then I echo "User entered 1"
And I wait 1 seconds
Elsif I verify number $Number is equal to 2
Then I echo "User entered 2"
And I wait 2 seconds
Elsif I verify number $Number is equal to 3
Then I echo "User entered 3"
And I wait 3 seconds
Else I echo "User did not enter 1, 2, or 3"
And I wait 4 seconds
EndIf
Combining If/Else with "I execute Scenario..."
In some cases it may be helpful to call an imported Scenario inside of an if statement, as in the example below which would wait .5 seconds in the middle of each loop.
Scenario: If I execute
Given I assign value 0 to unassigned variable "loop_count"
While I verify number $loop_count is less than 5
If I execute scenario "Looping Scenario"
Then I echo "Looping Scenario ran"
Else I echo "Looping scenario failed"
EndIf
And I increase variable "loop_count" by 1
EndWhile
@wip
Scenario: Looping Scenario
Then I wait .5 seconds
The benefit of using the if/else here is that the overall Feature can keep running even in the event of a failure of the inner looping Scenario.
Note: Though a failure in an inner Scenario that is called from an if/else would cause an error to display in the output panel, the top level feature would still report a success.