Configuring If-Else Statements in Katalon Studio

Configuring If-Else Statements in Katalon Studio

26 February 2025 Stephan Petzl Leave a comment Katalon Issues

If-else statements are crucial for controlling the flow of your test scripts in Katalon Studio. They help determine which actions to execute based on specific conditions. However, issues can arise when the script gets stuck on the if statement, failing to proceed to the else block. In this guide, we’ll explore a solution to this common problem.

Understanding the Problem

Let’s consider a scenario where your script is supposed to check if a particular text is present on the page. If the text is found, it executes a set of actions. If not, it should proceed to an alternative set of actions. Here’s a simplified version of the script:


WebUI.openBrowser('')
WebUI.navigateToUrl('123/account/login?ReturnUrl=%2F')
WebUI.maximizeWindow()
WebUI.setText(findTestObject('123/Page_Log in/input_ _Username'), 'admin')
WebUI.setEncryptedText(findTestObject('123/Page_Log in/input_ _Password'), 'admin')
WebUI.click(findTestObject('123/Page_Log in/span_Log in'))
if (WebUI.verifyTextPresent("Operator already has active session", true)) {
    WebUI.click(findTestObject('if-else/Page_Operator/button_Clear'))
    WebUI.click(findTestObject('if-else/Page_Operator/button_To activate session sta'))
    WebUI.waitForPageLoad(5)
    WebUI.click(findTestObject('123/Page_Operator/click_phone'))
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement Table = driver.findElement(By.xpath('//div[@id=\\'missedCallsContainer\\']'))
    List rows_table = Table.findElements(By.xpath('//tr[@class=\\'dl-menu\\']'))
    int rows_count = rows_table.size()
    println('No. of rows: ' + rows_count)
} else {
    WebUI.click(findTestObject('123/Page_Operator/click_phone'))
}
    

Solution: Using Failure Handling

The script above might fail to proceed to the else block if the verification step throws an exception. To handle this, you can enhance the if statement by adding a failure handling parameter. This ensures that the script continues execution even if the condition isn’t met.

Modify the if statement as follows:


if (WebUI.verifyTextPresent("Operator already has active session", true, FailureHandling.OPTIONAL)) {
    // Your existing actions here
}
    

This adjustment allows your script to handle failures gracefully, moving to the else block when the text isn’t present.

Enhancing Your Test Automation with Repeato

When dealing with complex test scenarios, using a tool that simplifies the process can be invaluable. Repeato, a no-code test automation tool, offers a robust solution for automating iOS, Android, and web app tests. By leveraging computer vision and AI, Repeato allows you to create, run, and maintain automated tests with ease.

Unlike other tools like Katalon Studio, Repeato supports multiple scripting languages and open-source modifications, providing flexibility and community-driven enhancements. For those looking to streamline their test automation processes, Repeato’s documentation provides comprehensive guidance on getting started.

Explore how Repeato can transform your testing workflow by visiting our blog for more insights and updates.

Like this article? there’s more where that came from!