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.

Other articles of interest:

Leave a Reply

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

Recent Comments
  • How To Disable the Quicken Registration Prompt (15)
    • Charlie Fellenbaum: Yay, thanks, and that worked for Quicken 2006 as well.
    • Ken: This didn’t work for me. I have 2006. I installed on a new laptop. I must have registered before, don’t remember. I tried to register this time, but got an error message. I’d rather not registered, though. I use...
    • Neil Murphy: I discovered that early on. I’m using 2001.
    • Jeremy: Thank you so so so much. I am still using Quicken 2010 because Intuit didn’t want to properly support users abroad anymore (never mind that they only link to US banks for transaction downloads; they didn’t want my...
    • Richard: I have Quicken 2016 Home & Business and when I use Shift + CTRL I can’t see any Tools option; All I can see is the original menu items of File & About. Might you be able to offer any advice?
  • Quasi Performance/Load Testing with Katalon Studio (1)
    • Anthony: I have done this with 100 plus machines at the same time and it worked well and provided some useful feedback.
  • A Try Catch example in Katalon Studio (3)
    • Venkatesh: Hi Don Pedro Can you share me sample project ….which consist of basic to advanced stuff…If your github or gitlab url aso present please share… As I am beginer to this I need to learn lot
    • Don Pedro: Hi Lucas. Thank you for the kind comments. I would be happy to help. Let me know how I could be of assistance.
    • Lucas: Thanks for sharing a lot of Katalon tips and tricks. The Katalon team highly appreciate your support and feedback. To make Katalon Studio better and be the best free automation solution for testing community, we wish to coordinate...
  • Securely storing passwords and login details with Set Encrypted Text in Katalon Studio (1)
    • Agus: Hi Don, Is there a way to set the encrypted text to be used in the ‘WebUI.authenticate̵ 7; method? Basically I am trying to encrypt the password that is passed to the ‘WebUI.authenticate̵ 7; method. Thank you.
  • Katalon Studio – Manual View – The start of a test script (2)
    • ben: Hello Don Great Website. Keep up the work. I learnt a lot. cheers Ben.
    • Lucas: Hi Don Pedro, Thanks for sharing a lot of great articles related to Katalon Studio and Selenium. I am Lucas from Katalon team. From your point of view, we’ve learnt alot to improve our product and add more valuable features...
  • Another success with Katalon Studio (1)
    • Mahesh Joshi: Hi Don, I just started following your blog in regards to katalon. I want to introduce katalon studio at my work we do manual testing at work at the moment. What approach I should use for proof of concept with katalon for...
  • What went wrong with Crime City? (34)
    • G D: One thing I have noticed is the awkwardness of items and the attack points and how you can earn them. I was pretty pathetic with attack points and then I played that limited time boss and got weapons that had attack points in the...
  • Disable the Quicken 2012 Registration Prompt (1)
    • Scott: Thanks this worked great and is better then having to upgrade to a subscription!