Provar Automation Advanced: Control Statements
(New)
Course Contributor: Ravish Rana
Course Designer: Caroline Smith
The purpose of this course is to explain control statements in Automation.
We want to make your University of Provar experience as accessible as possible. Please note
that you can navigate directly to lesson content and skip sidebar navigation if you are tabbing
through our course with a screen reader. You can also use these keyboard shortcuts for
alternative navigation.
NOTE: Provar Automation Advanced courses are best completed in conjunction with the use
of our Automation product. Please contact sales@[Link] to learn more about
becoming a customer.
INTR ODUC TION
Getting Started
Control Statements
C ONDITIONAL LOGIC S TEPS
Introduction to Conditional Logic
If Statement
Switch
LOOPING
Introduction to Looping
Wait For
While
For Each
FINALLY
Finally
WR AP UP
Quiz
Summary & Badge
Course Transcript
Tell us what you think!
Lesson 1 of 14
Getting Started
By the end of this course, you will be able to. . .
explain the purpose of advanced control statements
identify opportunities for advanced control statements in test cases
This course should take about 60 minutes, and you will earn an Advanced Control Statements
badge upon completion.
Lesson 2 of 14
Control Statements
Control Statements in Automation
Control statements make your testing more dynamic by adding structure and conditions to your
testing. With Automation, these steps are easy to access in the Test Palette from either Provar
Studio or Test Builder. For this course, we'll cover the control statements with advanced
functionality (indicated with red boxes). Information on the other steps can be found in our Provar
Automation Essentials certification or in our documentation.
Provar Studio
When you are working in Provar Studio, you can access control statements in the Test Palette, which is the
tab next to the Navigator.
Test Builder
As you are building your tests, you can also access the Test Palette and its control statements in Test
Builder.
Lesson 3 of 14
Introduction to Conditional Logic
What is conditional logic?
Conditional logic helps with complex testing scenarios. Writing your tests with conditional logic means the
path the test takes will change depending on the information received. In other words, a conditional
statement checks if certain conditions are met to determine which actions to execute next. We'll cover two
control statements that use conditional logic in this section:
If statements
Switch statements
Source
Lesson 4 of 14
If Statement
What is an If statement?
An If statement evaluates a condition to determine which sub-steps will be executed if the
boolean condition returns as true. It is usually coupled with an Else statement, which contains the
different set of steps to execute if the boolean condition is false. Simply put, you can define
different sets of steps for each Boolean result of the statement. You might think of it like a flow
chart written into your test case.
Now let's think about it in a test case. In this scenario, we will evaluate if the test step returns the
text This is a test when executed. If the text is present, then the condition will return as TRUE. This
means that the next step will execute Then to assert the values under that test step.
If the text is not present, then the condition will return as FALSE. This means that the the next step
will not execute Then. Instead, it will execute Else, which will assert the values for that test step.
Click through the numbers below to see how this would look in flow chart form.
If Statement
Evaluate if the test step returns the text when executed.
True
This is a test is present, so condition returns as TRUE.
Execute If step
Because the condition is true, the If step will execute Then to assert the values under that step.
False
This is a test is not present, so the condition returns as FALSE.
Execute Else step
Because the condition is false, the test will execute the Else step to assert the values under that test step.
Now let's replace those flow charts with an actual test case using the same scenario described
above.
If Statement
Evaluate if the test step returns the text flag when executed
True
This is a test is present, so condition returns as TRUE.
Execute If step
Because the condition is true, the If step will execute Then to assert the values under that step.
False
This is a test is not present, so the condition returns as FALSE.
Execute Else step
Because the condition is false, the test will execute the Else step to assert the values under that test step.
Use Multiple If statements
One limitation of If statements is that you can only have one condition with one Else clause.
However, you can nest these If statements within your Then or Else clauses in Automation. This
means you can still accomplish multiple conditional checks within one If statement. The image
below shows an example where we can check for three different types of accounts and perform
a different set of steps based on the account type.
Note that nesting too many If statements can create maintenance issues in your test cases. It is
recommended that you limit nesting to 2-3 levels to avoid overly complex scenarios. A Switch step
(covered later in this course) is better suited for scenarios requiring more than three conditions.
Add an If statement to a test case
With the Test Palette, you can easily add this logic to your test cases. Click through the slides
below to see this simple drag-and-drop functionality in Automation.
Add an If Statement
Step 1
Add the test step
Drag the If test step from the Test Palette into your test case.
Note: Click to enlarge the pictures.
Step 2
Update the parameters
Condition: Provide the condition in the If statement. Based on that, you will get the Boolean
result.
Then: This is a grouping of sub-steps which should be executed only when the If condition
evaluates to TRUE. This grouping is required and is added automatically under the If step.
Else: This is an optional grouping of sub-steps which should be executed only when the If
condition evaluates to FALSE. To add an Else clause to the If statement, right-click on the If test
step and select the Add an Else Clause option
Step 3
If you want to use Contains operator in the If statement
If you want to use Contains as a logic in the If statement, you can use the ~ operator. Note that
the Containing string needs be written between .* and .* as shown in the image above.
Lesson 5 of 14
Switch
What is a Switch statement?
The Switch Case statement is a control structure used to perform different actions based on the
value of a variable or expression. Once the condition is matched, the respective steps are
executed.
Let's consider an example with sports, specifically football and cricket. Similar to the different
rules and equipment we have for each sport, we'll also have different steps we want to execute
for each sport in our test case. If Football is returned, then we want only the values for Football to
be asserted. If Cricket is returned, then we only want the values for Cricket to be asserted. These
are our added Case clauses.
Another option you have in Automation is to include a Default case. This serves as a catch-all
group step that will run if none of your expected conditions are met. For example, if Soccer or
Basketball are unexpectedly returned, then the Default clause would execute those edge cases.
It's a recommended practice to put Default clauses at the end of the Switch block.
Take a look at this in a flow chart. You'll see that even though we've outlined two sports, there are
three options depending on the value returned because we also added the Default clause.
Now let's look at that same scenario written into a test case. Notice the Break statements after
each Case statement. Without them, all Case and Default clauses would execute every time,
defeating the purpose of the Switch statement. To add the Break statement to your test case,
simply drag it from the Test Palette.
A Switch statement is a way to streamline a series of If-Else
statements and create more complex logic without nested
statements.
Add a Switch statement to a test case
Automation makes it easy to add this logic to your test cases. Click through the slides below to
learn more.
Add a Switch statement
Step 1
Add the test step and set the value
Drag Switch from the Test Palette into your test case. Then, provide the value in the
Value parameter.
Note: Click to enlarge the pictures.
Step 2
Add or default a case
Switch statements come with one case by default. To add a case or default a case, right-click on
the Switch test step and select the option to Add a Case/Default Clause.
Note that you can add multiple case clauses in the test case based on your requirements.
Summary
That's it! Once you save the test case, your Switch step will be ready to go.
Automation will automatically add one Case clause with the Switch step. You can add another Case clause
or a Default clause by right-clicking on the Switch statement and selecting Add a Case Clause or Add a
Default Clause. You can have as many Case clauses as you want in your test case, but you can only have
one Default clause.
Lesson 6 of 14
Introduction to Looping
What is looping?
Looping repeats actions based on conditions that are set. It creates an iterative process in your testing, so
you can think of it as creating loops that run through and repeat a set of actions. Looping helps testing
because it saves time and reduces errors. We'll cover three statements that use looping:
While statement
Wait For statement
For Each statement
Source
Lesson 7 of 14
Wait For
What is a Wait For step?
The Wait For step is used to wait for a specific condition before proceeding further in the test.
There are different scenarios where you might want to add one to your test cases. Click the +
symbol to learn more about a few of them.
Wait for an element/field to become visible in the UI
–
If you've updated a record on a page, the page may not reflect the changes immediately. You can read some
fields' values prior to the loop, using either a UI Assert, an API call, or set parameter step, and compare it to
the value you are expecting it to be in the condition parameter of the Wait For step.
Test the response of a web service callout, such as a REST API
–
You can execute an API call, SOQL, or database query and wait for the response of said callout to match the
expected Wait For condition. By making the callout inside of the Wait For step with a small Sleep Timer
between each call, you can ping any endpoint/system and validate a specific field/value/parameter on the
backend before continuing your test case.
Wait on an email message to arrive in an inbox
–
Set up a connection to and poll an email inbox. Then, utilize a Wait For step to wait for the message to arrive
in the specific inbox with the correct information before continuing to test.
Let's dig into an example. Assume we have a dynamic element on the page, but the time that it
takes to show up on the page is inconsistent. We can use a Wait For step to help us here.
Wait for a Dynamic Element to Appear
Step 1
Read the element visibility
Note: Click to enlarge images.
Step 2
Use the result of the visibility as a Wait For condition
Step 3
Read the visibility of the element again
This will update the status of the visibility. The variable name will be the same as the first reading
of the element visibility.
Add a Wait For step to a test case
You can add a Wait For loop by dragging it from the Test Palette into your test case. Click the +
icons below to learn more about the different parameters you can set for it.
Condition
This is the condition that needs to be reached for the test to continue. You can define it using the build-in editors.
Test First
Check this box to test the condition first
Max Attempts
The maximum number of attempts to be made
Sleep For
The time in seconds that the sleep time should last. Note that this can exceed 60 seconds.
Continue On Failure
Check this box if the wait-for time should continue after failure
The Wait For step will exit under three conditions:
1 The maximum number of attempts is exceeded (e.g., the maximum
number is set to 5, so the loop will exit before a 6th attempt is made).
The conditional parameter evaluates to the boolean value of true.
2
3 One of the iterations encounters an error, and the Continue on Failure box
is unchecked.
Lesson 8 of 14
While
What is a While loop?
With a While loop, a block of code is executed repeatedly until a specific condition is met. It
consists of a condition and a block of code. The condition is evaluated before each iteration, and
if it's true, the code inside the loop is executed. If the condition becomes false, the loop
terminates, and the program continues with the next statement after the loop. There are several
scenarios where you might use a While loop.
You are iterating through a list and you want to specify an end value as a
conditional exit for the loop.
You want to perform a set of actions a certain number of times without a
list variable to utilize.
You have some more advanced logic and want to update the condition
inside the loop by using the counter as an indexer for a list, etc.
You want to loop indefinitely and perform some group of actions that are
not conditionally based.
Let's look at a While loop in a test case. In this scenario, we want to create five account records,
so we used a While loop will run up to five times depending on if or when the condition is met.
Click to enlarge the image.
Add a While loop to a test case
Drag and drop the While step from the Test Palette to your test case. Then, specify the
parameters. Click the + icons to learn more about them.
Condition
This is the condition to be reached for the test to continue. You can define this using the inbuilt editors also.
Counter Name
The user-defined name of the loop counter
Start Value
The starting value of the counter
End Value
This is the end value of the counter. Looping will automatically end when the loop reaches this value. If the value is
less than the Start Value, then the counter will decrease with each execution instead of increasing.
Continue on Failure
Check this box if the test should continue after failure.
There are three exit conditions for the While loop.
1 The conditional parameter evaluates to the boolean value of true.
2 One of the iterations encounters an error, and Continue on Failure box is
not checked.
The Loop Counter has reached the specified end value.
3
Lesson 9 of 14
For Each
What is a For Each loop?
Imagine you have a data file (csv, excel, JSON) that contains multiple rows of data, and you'd like
to perform the same actions on the multiple records of data. You could write the same steps over
and over for each row of data, or you could let Automation's For Each loop run through all the rows
with just one step.
A For Each loop allows you to iterate over elements of a collection, such as an array, list, or any
other iterable data structure. For example, in the spreadsheet below it would iterate through the
data on rows 2 through 11.
The benefit of a For Each loop is that you do not have to specify the loop's initialization, condition,
and increment explicitly. Instead, you provide a way to access each element of the collection, and
the loop automatically iterates through all the elements.
With the For Each loop, you can easily iterate over a list by
performing a repeatable action using the data in each row
or index of the loop.
Let's consider an example in a test case. In this scenario, we want to create multiple opportunity
records using the data provided in an Excel sheet. We can accomplish this in two simple steps.
Read the Excel sheet data
Read the data into your test case by adding a new parameter value source (covered in Provar Automation
Essentials: Data-Driven Testing).
Use the For Each loop to iterate through the data
With the data read into the test case, the For Each loop will iterate through the data to repeat the steps to
create the opportunity.
Add a For Each loop to your test case
Adding a For Each loop is as easy and dragging and dropping it from the Test Palette. There are a
few important parameters to update. Click on the icons below to learn more about each one.
List
The item collection list (e.g., 1, 2, 3, 4, 5) or Excel read data list
From Item
A particular value from the collection (e.g., 1) that indicates where to begin the first iteration of the loop
Value Name
A reference variable name to store the data for each iteration
Continue on Failure
If the For Each loop should continue after failure of any iteration within the loop, check this box.
The For Each loop will exit in two conditions. The first is when the index of the list/array set in the
List parameter has reached the end of its values. The second is when one of the iterations
encounters an error (failed test step or execution), and the Continue on Failure box is not
checked.
Lesson 10 of 14
Finally
What is a Finally statement?
When a scenario fails on a step in a normal execution flow, the default setup execution flow is either shifted
to the next scenario for batch execution or terminated for single scenario. In some instances, though, you
may want to execute some steps at the end of the scenario before the control shifts to the next one or
execution is terminated. This is where a Finally statement comes in.
The Finally statement creates a grouping of test steps that are always executed even if prior test steps have
failed. There are several scenarios where this is helpful:
Source
Include a Finally step to run reporting steps, such as updating the test
result into a spreadsheet.
If you want to clean up the data created during your test execution, you
can include those clean-up steps inside the Finally statement.
Use a Finally step to update the test case outcome in apps such as Test
Rail or JIRA regardless.
Let's look more closely at that last example. On a successful execution, Pass should be updated,
and on a failure Failed status should also be updated. We can use a Finally statement to update
that status after the run, but we also need two different sets of steps depending on the test
passing or failing. Sound familiar? We'll need to use an If Else statement to branch within the
Finally step. Check it out below.
Add a Finally statement to a test case
You can quickly and easily add a Finally statement to your test case by dragging it from the Test
Palette. Once it is in there, provide a label for it in the Description box and then save your case.
Lesson 11 of 14
Quiz
Question
01/05
Which symbol indicates the Contains logic in an If statement?
~
Question
02/05
How can you add multiple Case clauses to a Switch block?
Automation will automatically add the number of Case clauses you need
You can only have one Case clause in a Switch block
Drag them from the Test Palette
Right-click the Switch case and click Add a Case Clause
Question
03/05
True or False: The steps under a Finally statement will be executed even if there is a prior
failure.
True
False
Question
04/05
True or False: You can set a sleep time for more than 60 seconds with a Wait For loop.
True
False
Question
05/05
What happens if the condition evaluated in a While loop is true?
The code inside the loop is executed
The loop terminates
The loop runs again
The loop creates another loop
Lesson 12 of 14
Summary & Badge
Congratulations on completing the Advanced Control Statements course.
You should now be able to. . .
explain the purpose of advanced control statements
identify opportunities for advanced control statements in test cases
You've also earned a Advanced Control Statements badge. You can view your badge under “My
Badges” on UP.
Lesson 13 of 14
Course Transcript
File Attachment Block
No file added
Lesson 14 of 14
Tell us what you think!
Discover Provar's latest product update: Spring '24 Release 1
([Link]
([Link]