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:

One Response to Conditional Statements – IF .. ELSE IF in Katalon Studio

  • Allyson says:

    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 *

Recent Comments
  • Working with Dates and Date Formatting in Katalon Studio (4)
    • Don Pedro: import groovy.time.TimeCategory currentDate = new Date() println currentDate use( TimeCategory ) { after30Mins = currentDate + 30.minutes } println after30Mins https://stackoverflow.com/ques tions/21166927/incrementing...
    • ricky julianto nugroho: hay can u gave me an example for plus a hour in the time ? example : now is 15 pm i want to print 16 in the text thank you
    • Don Pedro: formattedDate=mydate.format(&# 8220;MMM”) – Nov formattedDate=mydate.format(&# 8220;MMMM”) – November
    • Ann: This was a huge help. Thank you ! Do you have something for capturing the month spelled out? and abreiveiated? Example November Nov Thanks !
  • Boostnote for Code Snippets (1)
    • Manual: Hi Don, Regarding the code snippets and examples that you have created. Is there any GitHub repo or something that can be shared? Thanks
  • Using TypeIt4Me with Katalon Studio (3)
    • Manual: That’s great thanks for sharing.
    • Don Pedro: Added a screenshot to show some examples of how I have shortcuts configured for Groovy code. TypeIt4Me supports groups, so you can create shortcuts related to Groovy, Selenium, and other topics to keep them organized. You can...
    • Manual: Hi Don, I like the idea of using the TypeIt4Me for scripting test code. Could you please share how I can create. I also like the idea of creating code templates to use with code editors. Thanks, Manual
  • Another success with Katalon Studio (2)
    • Don Pedro: Since Katalon is free, a very practical demonstration of what it can would be to create the code needed to fill in forms. That was one of my first tasks as Katalon could fill in a lengthy form in seconds versus the minute or...
  • How To Disable the Quicken Registration Prompt (18)
    • Susan Long: I have Quicken personal 2012 which I only use to enter and retrieve data. It suits my needs perfect. Now they have blocked my access and want me to re register. I don’t want to upgrade as it I am comfortable with this...
    • Stewart: I have been using Quicken 200 basic since it was introduced but I now have a new PC using windows 10 and can’t get rid of the registration prompt. I have tried the above control/shift trick but to no avail. Any ideas
    • Dave: Works for Quicken 2007 too. Thanks, Gina!
    • Charlie Fellenbaum: Yay, thanks, and that worked for Quicken 2006 as well.
  • Conditional Statements – IF .. ELSE IF in Katalon Studio (1)
    • Allyson: I used this : elementVisible=WebUI.verifyEle mentPresent… but it failed because “FAILED because (of) Unable to verify object ‘Object Repository/Homepage/b_vaolopho c’ is present (Root cause:...
  • 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.