Statements

In Katalon Studio, you can add statements as test steps. There are seven types of statements:

  • Decision-making Statements
  • Looping Statements
  • Branching Statements
  • Exception Handling Statements
  • Binary Statement
  • Assert Statement
  • Method Call Statement

This article guides you through how to add these statements to your test case and define a method in both manual and script views.

Decision-making Statements

Once a test step is added as any of the control statements, it is not allowed to change into another keyword.

In Manual view

Open a test case in Manual view. Click on the drop-down icon of the Add button, then choose Decision-making Statements.

add statements

To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

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

  • If - Else If - Else:

    if-else statements
Statement Description
If This statement requires a boolean condition as an input value. Once the condition is triggered, Katalon Studio starts executing all steps within.
Else If Using Else If after If, you can create a combination of conditions where the steps within the first satisfied condition start being executed.
Else This statement serves as the conclusion of the If - Else If - Else structure. If all the conditions above it are not triggered, test steps within this statement start being executed.
  • Switch - Case:

    switch statement
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 corresponding steps to be executed when a match occurs.

Each Case Statement has a Break Statement at the end of it by default to mark the end of the statement.

Default This statement is included automatically within every Switch Statement. If no Case value matches, the steps within Default is taken.

In Script view

The Script view of test cases allows you to programmatically define and handle If-ElseIf-Else or Switch-Case structure using Groovy or Java language. For more details about the conditional structure in Groovy, refer to this Groovy documentation: Control structures.

For example:

  • If - Else If - Else:

    if (true) {
        WebUI.click(findTestObject('Page_CuraAppointment/chk_Medicaid'), FailureHandling.STOP_ON_FAILURE)
    } else if (true) {
        WebUI.click(findTestObject('Page_CuraAppointment/chk_Medicare'), FailureHandling.STOP_ON_FAILURE)
    } else {
        WebUI.click(findTestObject('Page_CuraAppointment/chk_None'), FailureHandling.STOP_ON_FAILURE)
    }
    
  • Switch - Case:

    switch (true) {
        case true:
            WebUI.click(findTestObject('Page_CuraAppointment/chk_Medicaid'), FailureHandling.STOP_ON_FAILURE)
    
            break
        case true:
            WebUI.click(findTestObject('Page_CuraAppointment/chk_Medicare'), FailureHandling.STOP_ON_FAILURE)
    
            break
        default:
            WebUI.click(findTestObject('Page_CuraAppointment/chk_None'), FailureHandling.STOP_ON_FAILURE)
    
            break
    }
    

Looping Statements

Once a test step is added as any of the control statements, it is not allowed to change into another keyword.

In Manual view 

Open a test case in the Manual view. Click on the drop-down icon of the Add button, then choose Looping Statements.

To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

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 an input value so that Katalon Studio knows how many times to execute all steps within the For structure.
While This statement requires a boolean condition as the input value so that Katalon Studio keeps executing all steps within until the condition fails.

In Script view

The Script view of test cases allows you to programmatically define and handle For or While structure using Groovy or Java language. For more details about looping structures in Groovy, refer to this Groovy documentation: Looping Structures.

For example:

  • For:
for (def index : (0..5)) {
    WebUI.acceptAlert(FailureHandling.STOP_ON_FAILURE)
}
  • While:
while (varA == true) {
    WebUI.acceptAlert(FailureHandling.STOP_ON_FAILURE)
}

Branching Statements

Once a test step is added as any of the control statements, it is not allowed to change into another keyword.

In Manual view 

Open a test case in Manual view. Click on the drop-down icon of the Add button, then choose Branching Statements.

branching statement

To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

branching statement in manual view

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.
Continue Katalon Studio skips the remainder of the current loop and continues with the next iteration of the loop.
Return Katalon exits from the current method/step, and the flow control is returned to where the method/step was invoked.

In Script view

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

For example:

  • Break:
for (int i = 0; i < max; i++) {
	// interested only in p's
	if (searchMe.charAt(i) != 'p') {
		break;
	}
	
	// process p's
	numPs++;
}
  • Continue:
for (int i = 0; i < max; i++) {
	// interested only in p's
	if (searchMe.charAt(i) != 'p') {
		continue;
	}
	
	// process p's
	numPs++;
}
  • Return:
for (int i = 0; i < max; i++) {
	// interested only in p's
	if (searchMe.charAt(i) != 'p') {
		return true;
	}
	
	// process p's
	numPs++;
}

Exception Handling statements

Once a test step is added as any of the control statements, it is not allowed to change into another keyword.

In Manual view 

Open a test case in Manual view. Click on the drop-down icon of the Add button, then choose Exception Handling Statements.

exception handling statement

To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

exception handling statements in manual view

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

Statement Description
Try This statement indicates that all steps within is monitored by exception handlers.
Throw Some code must throw an exception before you can Catch an exception. Regardless of what code throws the exception, it always involves the Throw statement.
Catch When there is any issue occurred during the execution of the Try block, Katalon Studio executes all steps within.
Finally This is the last part of the Try-Catch-Finally structure. All steps within this are executed regardless of any exception.

In Script view

The Script view of test cases allows you to programmatically define and handle exceptions using Groovy or Java language. For more details about exception handling in Groovy, refer to this Groovy documentation: Try - Catch - Finally.

For example:

    try {
        WebUI.openBrowser('')

        WebUI.navigateToUrl('katalon.com')

        if (WebUI.getText(findTestObject('Object Repository/txt_singin')).length() < 0) {
            throw new com.kms.katalon.core.exception.StepFailedException('Value required')
        }
    }
    catch (StepErrorException e) {
        this.println(e)
    } 
    catch (Exception e) {
        this.println("General issue occurs.")
    } 
    finally { 
        this.println("Navigate to a page.")
    }

Binary Statements

A Binary statement represents a dual expression consisting of two single expressions (variables, strings, numbers, methods...) and an operator, for example: +, -, *, <, <=, !, etc.). For more details about using operators in Groovy, refer to this Groovy documentation: Operators.

In Manual view

  1. Open a test case in Manual view. Click on the drop-down icon of the Add button, then choose Binary Statements.

    To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

    binary statement
  2. A test step representing a binary statement is added to the test case.

    add binary statement
  3. Double-click on the Input cell to edit those required components.

    Binary statements are normally used to assign either values to test objects

    or test objects to variables to take the next steps.

  4. Save the test case when you're done.

    binary statement

    Once a test step is added as any of the Binary Statement, it is not allowed to change into another keyword.

In Script view

The Script view of a test case allows you to programmatically define and handle binary statements using Groovy or Java languages.

For example:

  • To assign a value to a test object

    myText = 'Welcome to Katalon Studio'
    
  • To assign a test object to a variable

    myObject = findTestObject('my object')
    WebUI.setText(myObject, 'Welcome to Katalon Studio')
    WebUI.verifyTextPresent('Welcome to Katalon Studio', false)
    

Method Call Statements

Method Call statement allows you to call other methods provided by the built-in libraries of Katalon Studio.

In Manual view

  1. Open a test case in Manual view. Click on the drop-down icon of the Add button, then choose Method Call Statements.

    To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

    method call statement
  2. A test step representing a method call is added to the test case.

    method call statement
  3. Double-click on the Input cell to edit the called method.

    method call statement input
  4. Save the test case when you're done.

    Once a test step is added as Method Call Statement, it is not allowed to change into another keyword.

In Script view

The Script view of a test case allows you to programmatically define and handle method calls using Groovy or Java language.

For example:

WebUiBuiltInKeywords.openBrowser('', FailureHandling.STOP_ON_FAILURE)
WebUiBuiltInKeywords.navigateToUrl(GlobalVariable.global_Gmail_Url, FailureHandling.STOP_ON_FAILURE)
WebUiBuiltInKeywords.setText(ObjectRepository.findTestObject(null), 'varA'.toString(), FailureHandling.STOP_ON_FAILURE)
Integer.notifyAll()

Assert Statements

An assert statement contains a boolean expression where this condition must hold true for the test execution to continue. Thus, execution of the assertion causes evaluation of the boolean expression, and an error is reported if the expression evaluates as false.

In Manual view

  1. Open a test case in Manual view. Click on the drop-down icon of the Add button, then choose Assert Statements.

    To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

    assert statement
  2. A test step represents assert expression is added to the test case.

    assert value
  3. Double-click on the Input cell to edit the assertion.

    input

    Once a test step is added as Assert Statement, it is not allowed to change into another keyword.

In Script view

The Script view of test cases allows you to programmatically define and handle assertions using either Groovy or Java language. For more details regarding assertions in Groovy, refer to this Groovy documentation: Core Testing Guide.html.

For example:

WebUiBuiltInKeywords.openBrowser('', FailureHandling.STOP_ON_FAILURE)
WebUiBuiltInKeywords.navigateToUrl(GlobalVariable.global_Gmail_Url, FailureHandling.STOP_ON_FAILURE)
assert varA == true

Define Method

A method consists of instructions to perform a specific task. Defined methods can be called to use later. For more details regarding how to call a defined method, refer to Method Call Statements.

In Manual view

  1. Open a test case in Manual view. Click on the drop-down icon of the Add button, then choose Method.

    To add a keyword under a statement, select that statement, then click Add. A test step is created under that statement.

    method statement
  2. The Method builder dialog appears.

    method builder

    Specify the required information for your method as follows:

Field Description
Name The name of the method.
Return type

The object type that this method returns after its execution.

Setup, Teardown options Select any checkbox to indicate whether it should be a setUp or tearDown method. Refer to SetUp() and TearDown() in Manual view for more details.
Parameter list

Any parameter needed to pass into the method.

By clicking on the Insert button, a row will be appended into the grid. You can then change the type and name of the parameter by double-clicking and editing them.

After configuring the method details, click OK.

  1. A test step representing the recently defined method is added to the test case. You can switch to the script view to define content for the method.

    setUp and tearDown in manual view
  2. When you're done, save the test case.

    Once a test step is defined as Method, it is not allowed to change into another keyword.

In Script view

The Script view of a test case allows you to programmatically define and handle methods using Groovy or Java language. For details about methods in Groovy, refer to Groovy documentation: Methods.

For example:

  • Method declaration:

    Integer myMethod(def param1, def param2) {
        return param1 + param2
    }
    
  • Method call:

    myVar = myMethod1(varA, varB)
    

Feedback