6 Using Control Structures in Steps

Learn to use different control structures in steps in Oracle Communications Solution Test Automation Platform (STAP).

Topics in this chapter:

Overview

You can use control structures like if, for, and while for steps in the Behavior-Driven Development (BDD) language. They are the building blocks within each test case and determine the flow of execution for each step based on specific conditions. Steps dictate the flow within test cases, while scenario execution flow governs the execution of the entire test scenario.

Scenario Execution Flow

The Scenario Execution Flow relies on the outcomes of the steps in the scenario. If a step within a test case fails, it impacts the flow by skipping remaining steps and potentially other test cases within the scenario.

Figure 6-1 shows the detailed flow of a scenario execution.

Figure 6-1 Scenario Execution Flow



If the scenario execution is successful:

  • Test Scenario Execution: If all the test cases within a scenario are run successfully, the entire scenario is considered passed.
  • Test Case Execution: When all test steps within a test case are run without any errors, the test case is considered passed.

If the scenario execution fails:

  • Test Scenario Execution: If a test case within a scenario fails, all subsequent test cases in that scenario are skipped, and the entire scenario is marked as failed.
  • Test Case Execution: If any test step within a test case fails, the remaining test steps in that test case are skipped, and the test case is marked as failed.

This detailed flow ensures that the execution process is efficient and that any failures are quickly identified and addressed, preventing unnecessary execution of subsequent steps or cases.

Action Execution

There are two types of Action Executions:
  • Static Action (Default)
  • Controlled Step

Static Action (Default)

Performs the action once (in sequence).

Figure 6-2 shows the detailed flow of a static action.

Controlled Step: Dynamic Action

Controlled execution of Step

  • Condition: Conditional Execution (IF)
    • Perform action when the condition is PASSED.
  • repeatTimes (FOR)
    • Repeat number of times.
  • repeatUntil (UNTIL)
    • Repeat until the condition is PASSED.
  • repeatWhile (WHILE)
    • Repeat while the condition is true.

Conditional Execution

  • Perform Action when the condition is successful.
  • Support multiple conditions using 'Condition'.
Figure 6-3 shows the detailed flow of a conditional execution.

Figure 6-3 Conditional Execution



For example,

# This block of code checks if the category is Platinum, if yes, then it changes to Gold through the action file request 
When change category, for changing customer category
Condition:
| ${category} | Platinum |
Validate:
| $status | 201 |
| category | Gold |
Save:
| name | name |
| category | category |
RepeatTimes

Repeat Times

Repeatedly perform the action given number of times.

Figure 6-4 shows the flow of repeat times.

The success of the step depends on each action. If any iteration fails – the step fails even then continues.

The following are the various ways through which you can specify the repeat number of times:
  • n : integer: number of times
  • ${variable}: integer variable of times
  • ${array}: array of integers. Action is repeated for array length number of times
  • ${index}: index value of iteration. Values: 1-n
  • ${nextValue} gives next array value.
  • $breakOnFailure: YES breaks the loop, Default: NO

Example

Case: RepeatTimesAction
When set variable, create bills list
Save:
| $ARRAY{bills} | 25.213 |
| $ARRAY{bills} | 30.456 | 
Then get mock response, repeatedly to send payment reminders of bills
# executes this block for variable times - size of array 
RepeatTimes:
| $times | $ARRAY{bills} | 
Data: 
| id | getdata | 
| index | ${nextValue} |
Validate:
| $status | 200 |
| bills[${index}] | $ARRAY{bills[${index}]} |
Then get mock response, repeatedly to send payment reminders of bills
#executes this block of code for predefined number of times 
RepeatTimes:
| $times | 2 | 
Data: 
| id | getdata | 
| index | ${nextValue} |
Validate:
| $status | 200 |
| bills[${index}] | $ARRAY{bills[${index}]} |
RepeatUntil

Repeat Until

  • Repeatedly perform the action until the given condition is true.
  • At least one Condition is mandatory. (?)
  • $breakOnFailure : YES breaks the loop on action validation failure. Default: NO.

When set variable, create bills list
Save:
| $ARRAY{bills} | 25.213 |
| $ARRAY{bills} | 30.456 | 
| $ARRAY{bills} | 28.712 |
| $ARRAY{bills} | 26.389 |
| $ARRAY{bills} | 31.243 |
# executes this block of code until all the conditions are true 
Then get mock response, sending notifications until customer bill equals a value
RepeatUntil:
| $ARRAY{bills[${index}]} | 30.456 |
Data:
| id | getdata | 
| index | ${nextValue} |
Validate:
| $status | 200 |
Repeat Until with time durations and frequency interval

Repeat Until with Time Durations and Frequency Interval

Figure 6-5 shows the flow of Repeat Until with Time Durations and Frequency Interval.

Figure 6-5 Repeat Until with Time Durations and Frequency Interval



$startAfter : Optional. Start executing action after this duration of time. By default, starts immediately.The duration is in seconds.
$endAfter : Mandatory. Break after the completion of this time duration.
$interval: Optional. interval duration to run the action. By default, executes continuously.
Specify duration in Seconds.
Breaks if the condition is true even before $endAfter.
$breakOnFailure : YES will breaks loop on action validation failure, Default: NO.
Example scenario:

example

Case: RepeatUntilAction
When set variable, create bills list
Save:
| $ARRAY{bills} | 25.213 |
| $ARRAY{bills} | 30.456 | 
| $ARRAY{bills} | 28.712 |
| $ARRAY{bills} | 26.389 |
| $ARRAY{bills} | 31.243 |
# executes this block of code until all the conditions are true 
Then get mock response, sending notifications until customer bill equals a value
RepeatUntil:
| $ARRAY{bills[${index}]} | 30.456 |
# start execution after 1 second
| $startAfter | 1 |
# 1 second interval for every execution
| $interval  | 1 |
# stop execution after 5 seconds
| $endAfter | 5 |
Data:
| id | getdata | 
| index | ${nextValue} |
Validate:
| $status | 200 |
RepeatWhile
When set variable, setting customer bill Amount
Save:
| billAmount | 30 | 
# All the conditions must hold true -> While executes the condition first
Then get mock response, sending notifications of pending bills while amount is under a threshold
RepeatWhile:
# The variable used here must be defined already
| ${billAmount} | %GREATER_THAN(25) |
Data:
| id | getcust | 
| index | ${nextValue} |
Validate:
| $status | 200 |
| extractedNotice | 'Your subscription is expiring soon' | 
RepeatWhile with time durations and interval
Repeat While
  • Repeatedly perform the action while the given condition is true.
  • $breakOnFailure: YES will break loop on action validation failure, Default: NO.

Repeat While

  • Repeatedly perform the action while the given condition is true.
  • $breakOnFailure: YES will break loop on action validation failure, Default: NO.

Figure 6-6 shows the flow of 1st iteration.

Figure 6-6 Repeat While 1st Iteration



Figure 6-7 shows the flow of other iterations.

Figure 6-7 Repeat While Other Iterations



Case: RepeatWhileAction
When set variable, setting customer bill Amount
Save:
| billAmount | 30 | 
# All the conditions must hold true -> While executes the condition first
Then get mock response, sending notifications of pending bills while amount is under a threshold
RepeatWhile:
# The variable used here must be defined already
| ${billAmount} | %GREATER_THAN(25) |
# starts execution after 1 second
| $startAfter | 1 |
# 1 second interval for every execution
| $interval  | 1 |
# stop execution after 5 seconds
| $endAfter | 5 |
Data:
| id | getcust | 
| index | ${nextValue} |
Validate:
| $status | 200 |
| extractedNotice | 'Your subscription is expiring soon' | 

Repeat While: Examples of Time Durations and Interval

$startAfter : Optional. Start executing action after this duration of time. By default, starts immediately.
$endAfter : Mandatory. Break after the completion of this time duration.
$interval: Optional. interval duration to run the action. By default, executes continuously.
Specify duration in Seconds.
Breaks if the condition is true even before $endAfter.
$breakOnFailure : YES breaks a loop on action validation failure. Default: NO.
Case: RepeatWhileAction
When set variable, setting customer bill Amount
Save:
| billAmount | 30 | 
# All the conditions must hold true -> While executes the condition first
Then get mock response, sending notifications of pending bills while amount is under a threshold
RepeatWhile:
# The variable used here must be defined already
| ${billAmount} | %GREATER_THAN(25) |
# starts execution after 1 second
| $startAfter | 1 |
# 1 second interval for every execution
| $interval  | 1 |
# stop execution after 5 seconds
| $endAfter | 5 |
Data:
| id | getcust | 
| index | ${nextValue} |
Validate:
| $status | 200 |
| extractedNotice | 'Your subscription is expiring soon' | 

Using multiple test data files in control actions

Action using multiple data
When create product offering, with multiple data sets 
repeatTimes: 2 
Data: 
| $request | $FILE(productOffering_${index}.json) |
(data/product Offering_0.json
data/productOffering_1.json)
| variable | I Value${index}-${UlD} |
Validate: 
| statusCode | 201 | 
Save : 
| $ARRAY{productOfferingId} | id |
(data/productOffering_O.json 
data/productOffering_1.json 
productQtferingJd Array) 

 

Action using multiple data sets 
When Dummy, save some values 
Save: 
| $ARRAY{poNames} | VoicePO | 
| $ARRAY{poNames} | SMSPO | 
| $ARRAY{poNames} I VolPPO | 
When create product offering, with multiple data sets 
Repeat Times: 
| $times | $ARRAY{poNames} I 
Data: 
| $request |$FILE(productOffering_${nextValue}.json |
(data/productOffering_VoicePO.json
data/productPffering_SMSPO.json
data/productOffering_VolPPO.json)
| variable | Values${nectValue}-${index}-${UID} | 
Validate: 
| statusCode | 201 | 
Save : 
| $Array{productOfferinfId} | id |

Using Conditional Cases

Cases to run are mentioned in the scenario.config file. With conditional case execution, you can specify a set of conditions, and only the cases which satisfy all specified conditions are run.

Note:

  • You mention the conditions after the case name within curly brackets, separated by a comma. For example, sampleCase {condition1, condition2}.
  • If the condition value or condition variable is from a saved variable in any of the previous cases run, they are to be specified within ${ }.
  • Only = or Equals to operation is supported for condition evaluation.

The following are the configurations to set to run conditional cases.

The syntax for scenario.config configuration file:
Header.info
Data.case
MockAction.case
MockAction.case{${executeMockAction}=${value}}
#MockAction.case{${executeMockAction}=true}
MockAction.case{${executeMockAction}=true,${day}=wednesday}
The following is the syntax for data.case file:
Case: Data creation for conditional execution
 
When dummy
Save:
| value | true |
| executeMockAction | true |
| day | wednesday |
The following is the syntax for MockAction.case file:
Case: Mock action test
 
When execute mock action, creating a task
Data:
| id | WeekdayTask-${UID} |
| name | WeekdayTask-${UID} |
Save:
| taskId | id |
| taskName | name |
 
When execute mock action, reading the task
Data:
| $requestString | {"id":"id"} |
| id | ${taskId} |