• Common Condition and Control statements with Katalon Studio

    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 command toolbar.

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

    STATEMENTDESCRIPTION
    IfThis statement requires a boolean condition as input value. Katalon Studio will execute all the steps within once the condition is triggered.
    Else IfUsing Else If after If, you can create a combination of conditions where the steps within the first satisfied condition will be executed.
    ElseThis 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.
    STATEMENTDESCRIPTION
    SwitchThis statement requires an expression, which is often referred to as the control expression (or control variable), as input value.
    CaseThe 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.
    DefaultThis statement is included automatically within every Switch statement. In situations where there is no case matched, all the steps under default will be executed.

    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 statementDescription
    If – Else If – ElseHere 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

    if (varA == true){
    WebUiBuiltInKeywords.click(ObjectRepository.findTestObject(null), FailureHandling.STOP_ON_FAILURE)
    } else if(varB == true) {
    WebUiBuiltInKeywords.click(ObjectRepository.findTestObject(null), FailureHandling.STOP_ON_FAILURE)
    } else {
    WebUiBuiltInKeywords.click(ObjectRepository.findTestObject(null), FailureHandling.STOP_ON_FAILURE)
    }
    Switch – CaseHere 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 (varA) {
    case 1:
    varB = 2*varA
    break
    case 2:
    varB = 3*varA
    break
    case 3:
    varB = 4*varA
    break
    default:
    varB = 0
    break
    }

    Looping statements

    In the Manual view

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

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

    STATEMENTDESCRIPTION
    ForThis 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.
    STATEMENTDESCRIPTION
    While This statement requires a boolean condition as input value so that 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 statementDescription
    For
    Here is an example on how to use For in the Script View where the acceptAlert keyword will be executed 10 times

    for (def index : (0..9) {
    WebUiBuiltInKeywords.acceptAlert(FailureHandling.STOP_ON_FAILURE)
    }
    WhileHere is an example on how to use For in the Script View where the value of varA is true

    while (varA == true) {
    WebUiBuiltInKeywords.acceptAlert(FailureHandling.STOP_ON_FAILURE)
    }

    Branching statements

    In the Manual view

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

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

    Statement
    Description
    Break Katalon Studio exits the current code block and continue to 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.
    Statement
    Description
    Return Katalon exits from the current method/step, and the flow control is returned to where the method/step is previously invoked.

    In the Script view

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

    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.

    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.
    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.

    Exception handling block

    In the Manual view

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

    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.

    In the Script view

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

    For example:

    Sharing is caring.

    Share on facebook
    Facebook
    Share on twitter
    Twitter
    Share on linkedin
    LinkedIn

    Like This Post?

    We have a lot more where that came from?

    We only send really good stuff occasionally, promise.

    Rajkumar SM

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Scroll to Top
    API Testing eBook

    DOWNLOAD FOR FREE