Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Common Condition and Control Statements With Katalon Studio

DZone's Guide to

Common Condition and Control Statements With Katalon Studio

Learn about using conditional and control statements when writing tests in Katalon Studio to dictate the flow of test execution.

· DevOps Zone
Free Resource

Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market. 

Katalon Studio provides a capability to dictate the logical flow of execution by supporting control statements such as if/else, for/while and try/catch which are very common concepts in programming languages. This tutorial will explain in details how to use control statements in Katalon Studio along with examples for each case.

The following control statements are supported in Katalon Studio:

Note: once a test step is added as any of the control statements, it will not be allowed to change into another keyword.

Decision-Making Statements

In the Manual View

Open a test case in the Manual view, then navigate to Decision-making Statements from the command toolbar.

navigate to Decision-making Statements from command toolbar

Refer to the following table for the usage of each statement:

Statement Description
If This statement requires a boolean condition as the input value. Katalon Studio will execute all the steps within once the condition is triggered.
Else If Using Else If after If, you can create a combination of conditions where the steps within the first satisfied condition will be executed.
Else This statement serves as the conclusion of the If – Else If – Else structure. The steps within this statement will be executed if all the conditions above it are not triggered.

you can create a combination of conditions

Statement Description
Switch This statement requires an expression, which is often referred to as the control expression (or control variable), as the input value.
Case The Cases indicate the assumed value for the control expression, with the corresponding steps to be executed when a match occurs. Each Case will have a Break by default which should be positioned at the end of the Case block to mark the end of it.
Default This statement is included automatically within every Switch statement. In situations where there is no case matched, all the steps under default will be executed.

Common Condition and Control statements with Katalon Studio

In the Script View

The Script view of test cases allows you to programmatically define and handle If-ElseIf-Else or Switch-Case structure easily using either Groovy or Java language. Refer to conditional structure in Groovy for more details.

For example:

Decision-making statement Screenshot
If – Else If – Else

Here is an example on how to use if-else if-else in the Script View where the Click action will be carried out based on the condition

Script View Katalon Studio

Switch – Case

Here is an example on how to use switch-case in the Script View where varB is calculated based on the passing value of varA.

switch-case in the Script View

Looping Statements

In the Manual View

Open a test case in Manual view, then navigate to Looping Statements from the command toolbar.

Katalon Studio looping statement

Refer to the following table for the usage of each statement:

Statement Description
For This statement accepts a range, list or array as input value so that Katalon Studio knows how many times to execute all steps within the For structure.

how many times to execute all steps within the For structure

Statement Description
While This statement requires a boolean condition as input value so that Katalon Studio will keep executing all steps within until the condition fails.

 Katalon Studio will keep executing all steps within until the condition fails

In the Script View

The Script View of test cases allows you to programmatically define and handle For or While structure easily using either Groovy or Java language. Refer to looping structures in Groovy for more details.

For example:

Looping statement Screenshot
For

Here is an example on how to use For in the Script View where the acceptAlert keyword will be executed 10 times

how to use For in the Script View

While

Here is an example on how to use For in the Script View where the value of varA is true

how to use For in the Script View


Branching Statements

In the Manual View

Open a test case in the Manual view, then navigate to Branching Statements from the command toolbar.

Katalon Studio Branching Statements

Refer to the following table for the usage of each statement:

Statement Description
Break Katalon Studio exits the current code block and continues to the next code block/test step.

the next code block/test step

Statement Description
Continue Katalon Studio will skip the remainder of the current loop and continue with the next iteration of the loop.

 the next iteration of the loop.

Statement Description
Return Katalon exits from the current method/step, and the flow control is returned to where the method/step is previously invoked.

the flow control is returned

In the Script View

The Script view of test cases allows you to programmatically define and handle Break, Continue, and Return easily using either Groovy or Java.

For example:

Decision-making statement Screenshot
Break

Here is an example on how to use Break in the Scripts View in order to exit current code block and move to next code block.

Break-in-the-Scripts-Katalon-Studio
Continue Here is an example on how to use Continue in the Script View in order to skip the remainder of the current loop and continue with the next iteration of the loop. how to use Continue in the Script View
Return

Here is an example on how to use Return in the Script View in order to exit from the current method, and the flow control is returned to where the method was invoked.

how to use Return in the Script View


Exception Handling Block

In the Manual View

Open a test case in Manual view, then navigate to Exception Handling Statements from the command toolbar.

Katalon Studio Exception Handling Statements

Refer to the following table for the usage of each statement:

Statement Description
Try This statement indicates that all steps within it will be monitored by exception handlers.
Throw Before you can catch an exception, some code must throw it. Regardless of what throws the exception, it’s always involved with the Throw statement.
Catch Katalon Studio executes all steps within the Catch block when there is any issue occurring during execution of the Try block.
Finally This is the last part of the Try-Catch-Finally structure and all steps within this block will be executed regardless of what exceptions and whether they are handled in the Catch block.

the usage of each statement

In the Script View

The Script view of test cases allows you to programmatically define and handle exception easily using either Groovy or Java. Refer to this guide for more details about exception handling in Groovy.

For example:

Katalon Studio Script View

Find out more about how Scalyr built a proprietary database that does not use text indexing for their log management tool, allowing customers to search 1TB of data in under a second. 

Topics:
automated testing ,test automation ,software testing ,manual testing ,katalon studio

Published at DZone with permission of Oliver Howard. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}