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
  • How To Disable the Quicken Registration Prompt (21)
    • Susan Long: I bought my quicken disc in the beginning and it came with a registration number. I rang the helpline and they gave me the code to put in and talked me through it. It you downloaded your version then you don’t own it...
    • Peter: You might consider running your old version of Quicken on an ancient computer. This is what I have done for years. The newer versions are fraught with problems- criminal, in my opinion. One version made mathmatical errors when...
    • Roslyn Chamberlain: Can I stop the countdown in quicken 2001 says only 8 sessions left. and what will happen after?
  • What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse (1)
    • Mahesh: Looking for more posts on katalon studio.your katalon stuffs are always exiting
  • Simple wildcard searches for pattern matching (2)
    • Don Pedro: For that scenario it seems .contains would be your choice. For example, variable.contains(‘amazo n.com’) to see if the url had amazon.com. In that case, www.amazon.com, forum.amazon.com, retail.amazon.com would all...
    • Jony: Hi, How can I use a wild card to assert a URL is the one I want. I just want to verify the domain ==expected but not anything after it. Tried * but not working and only works when I have full URL.
  • Create a Dynamic Object at Runtime (2)
    • Saish: How to add shadow root parent to this runtime object..
    • Jeremy Brien: I appreciate this! I saw this post on LinkedIn this morning and was able to find a use case for it! I found that defining my xpath with an iterable variable allows me to loop through and capture text from tables created...
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (2)
    • Don Pedro: That would be things like: log.logWarning(‘The import date listed on the site is: ‘ + importDate) log.logWarning(‘The value from the site is: ‘ + salesFigure) log.logWarning(‘Filter Results for ‘ +...
    • Prashant Pednekar: Thanks for the informative article but can we also put some variable values to make it more exciting.
  • Setting up a repeatable Search Method in Katalon Studio (1)
    • RJ: Thanks a lot! This is very helpful 🙂
  • Filling forms with random numbers in Katalon Studio (3)
    • Patrick Clough: We created a handy random string generator custom keyword. The method takes in a string for the type of string you want, and an integer for the length. Looks like this: static String RANDOM_ALPHANUM =...
    • Don Pedro: randomNumber is the variable that holds the result of generating a new random number. Depending on how things work on your site, you might need to convert the number to a String before it’s recognized correctly....
    • Seth: When you are setting up randomNumber is this a new keyword or a new variable? I am trying to randomly generate unique socials and can’t seem to connect my generator to my Set Text.
  • Working with Dates and Date Formatting in Katalon Studio (5)
    • Ann: THANK YOU ! That worked perfectly !