Conditional Statements – IF .. ELSE IF in Katalon Studio

One of the big advantages of using Katalon Studio is the ability to use the underlying Groovy subsystem. And with that comes the means to do conditional branching, the ubiquitous IF and IF .. ELSE statement. This allows you to perform all sorts of actions based on what values are read from the site.

In my case, if a value is less than 0, it could mean the data import didn’t succeed. That can easily be expressed as:

if (salesPlan <= 0) {

    log.logWarning('ERROR: The Sales Plan Value is $0 or less. A problem may have occurred with the import')

}

The IF statement can be entered directly into the Script editor, but it can also be constructed using the Manual view. If you click the down arrow next to Add, there is an option for Decision-making Statements, and within there is IF, Else, and Else If.

if-then

This is very handy to get started the first couple of times, but the construct will become second nature pretty quickly. Since the above is a simple IF statement, the next step is the IF ELSE statement which look like:

if (salesPlan <= 0) {

    log.logWarning('ERROR: The Sales Plan Value is $0 or less. A problem may have occurred with the import')

} else {

 log.logWarning('SUCCESS: The import appears to have been successful')

}

That covers the idea of something being a success, or something being in error. It’s also possible to string statements together to cover multiple conditions. Below is a snippet of code that determines the first of a string. If it’s a vowel, the word, “AN” should be used instead of the letter, “A”.

if (prepWord==("A")) {

  if (adjWord.substring(0,1)=="A") {

  prepWord="An"

  }

  else if (adjWord.substring(0,1)=="E") {

  prepWord="An"

  }

  else if (adjWord.substring(0,1)=="I") {

  prepWord="An"

  }

  else if (adjWord.substring(0,1)=="O") {

  prepWord="An"

  }

  else if (adjWord.substring(0,1)=="U") {

  prepWord="An"

  }

}

A CASE statement probably would handled this better, but that’s an example for another day.

IF statements can also be nested. In this example, if the values don’t match, determine the margin of error. For my site, it’s not uncommon for figures to be by $1 or $2 due to rounding. This isn’t actually a problem and with an IF statement, it can be accounted for.

if (dailyGrossProfitPlan != siteDailyGrossProfit) {

 log.logWarning('The site and calculated values DO NOT match!')

 calcDifference=(dailyGrossProfitPlan-siteDailyGrossProfit)

 if (calcDifference<=5){

 log.logWarning('The difference is less than $5, most likely a rounding difference and is acceptable')

 } else {

 KeywordUtil.markFailed('ERROR: The difference between the site and the calculated value is greater than $5. Investigation is needed')

 }

}

The IF statement can also be used to determine if elements are visible on the page, and act accordingly. In a test case I recently put together, the Customer Profile can have several tabs listed – Contacts, Notes, Tasks, Quotes and Plan. However, the tabs display under certain conditions and may not always be visible. To start, I made an object with the XPATH for each tab. I then checked to see if the tab was visible. If it was, code related to that tab would run. If the tab wasn’t visible, the test would be skipped.

The code for that is listed below.

//Determine which of the tabs is visible and run the appropriate test

elementVisible=WebUI.verifyElementPresent(findTestObject('Page_/Customer Profile/tab-Contacts'), 10) - determine if the tab is visible on the page. The variable elementVisible will be TRUE if it exists or FALSE if it's not.

if (elementVisible==true){ - If the tab exists, execute the steps between the {}

log.logWarning('--- Contacts tab is available, running test ---')

WebUI.callTestCase(findTestCase('Customer Profile/Contacts'), [:], FailureHandling.STOP_ON_FAILURE) - Call out to another test case to verify items on the tab.

} - End of the IF statement

Finally, the operators used in an IF statement are as follows:

== equal

!= different (or not equal)

< less than

<= less than or equal

> greater than

>= greater than or equal

The IF statement is an incredibly powerful tool to interact with and adapt to the values read from a site. It can be used to determine if a value is too high or low, whether an element exists or not, can be used as part of string manipulation and nearly an endless variety of tests. This was pretty limited within the Selenium IDE and needed the SelBlocks plugin to work. Now, the full power of Groovy can be used to make tests infinitely flexible.

Handcrafted with care just for you.

Author Signature for Posts

1 thought on “Conditional Statements – IF .. ELSE IF in Katalon Studio

    • Author gravatar

      I used this : elementVisible=WebUI.verifyElementPresent…
      but it failed because
      “FAILED because (of) Unable to verify object ‘Object Repository/Homepage/b_vaolophoc’ is present (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Homepage/b_vaolophoc’ located by ‘By.xpath: //b[text()=’VÀO LỚP HỌC’]’ not found)”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.