Proper formatting of else-if statements for button elements

Hello! I’m quite new to Katalon Studio and I’m trying to figure out why my logic statement won’t execute:

if (true) {
WebUI.click(findTestObject(‘Object Repository/button_a’))
} else if (true) {
WebUI.click(findTestObject(‘Object Repository/button_b’))
} else {
WebUI.click(findTestObject(‘Object Repository/button_c’))
}

My code is trying to verify if button A is on a website, and it will click that value, and if it is not present, it will click repeat the process for button b or c. I’ve based my code on the Katalon Guide, however, I have an inkling that the problem relates to the condition input (currently value type is ‘condition’ and value is 'true) or the error handling (currently set on ‘stop on failure’). I’ve made sure the element clicking works on a single command, and the button is properly in the repository.

Thanks in advance!

The essence of your structure looks fine to me. What issue are you seeing?

Let me see the actual code and any errors you receive.

I can’t seem to find the description of the problem, besides that it ‘won’t execute’. I can see something about ‘true’ and ‘condition’ which in my mind were not related before. Would you mind clarifying a bit more ?

@ThanhTo

Yes, it’s vague, but they’re either having trouble with something like verifyElementVisible/Present or FailureHandling is tripping them up.

@jai.kowalik In future, make your question/issue clearer. Follow this advice:

Apologies about the vagueness of my comment! I will make sure to revise that article to make my question more direct!

1 Like
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable

WebUI.openBrowser('')

WebUI.navigateToUrl('https://www.website.com')

if (true) {
    'button_US 10\n'
    WebUI.click(findTestObject('Size Elements (MENS)/button_a'))
} else if (true) {
    'button_US M 10  W 115'
    WebUI.click(findTestObject('Size Element (WMNS)/button_b'))
} else {
    'button_US 11\n'
    WebUI.click(findTestObject('Size Elements (MENS)/button_c'))
}

11-25-2019 01:57:05 AM Test Cases/Finalised Scripts/Sizing - Else If Statement

Elapsed time: 1m - 7.294s

Test Cases/Finalised Scripts/Sizing - Else If Statement FAILED.

Reason:

com.kms.katalon.core.exception.StepFailedException: Unable to click on object 'Object Repository/Size Elements (MENS)/button_a'

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)

at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.click(ClickKeyword.groovy:99)

at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword.execute(ClickKeyword.groovy:43)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:60)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.click(WebUiBuiltInKeywords.groovy:616)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$click$1.call(Unknown Source)

at Sizing - Else If Statement.run(Sizing - Else If Statement:24)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1574609222203.run(TempTestCase1574609222203.groovy:23)

Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/Size Elements (MENS)/button_a' located by 'By.xpath: //button[(text() = 'US 10' or . = 'US 10')]' not found

at com.kms.katalon.core.webui.common.WebUiCommonHelper.findWebElement(WebUiCommonHelper.java:1113)

at com.kms.katalon.core.webui.keyword.internal.WebUIAbstractKeyword.findWebElement(WebUIAbstractKeyword.groovy:27)

at com.kms.katalon.core.webui.keyword.internal.WebUIAbstractKeyword.findWebElement(WebUIAbstractKeyword.groovy:26)

at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword$_click_closure1.doCall(ClickKeyword.groovy:68)

at com.kms.katalon.core.webui.keyword.builtin.ClickKeyword$_click_closure1.call(ClickKeyword.groovy)

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)

... 17 more

My code is above, and the error that returns is underneath it. I’m not sure if this is the information that is required to troubleshoot this problem. Please let me know if I am on the wrong path. :slight_smile:

Oh, I see…

I thought if(true) was a placeholder for a proper conditional. Your first if will ALWAYS be true - because true is always true :slight_smile:

Try something like:

if(verifyElementVisible(findTestObject('Size Elements (MENS)/button_a'), FailureHandling.CONTINUE_ON_FAILURE)) {
  WebUI.click(findTestObject('Size Elements (MENS)/button_a'))
  ...
if(verifyElementVisible(findTestObject('Size Elements (MENS)/button_a'), 20, FailureHandling.CONTINUE_ON_FAILURE)) {
  WebUI.click(findTestObject('Size Elements (MENS)/button_a'))
} else if (verifyElementVisible(findTestObject('Size Elements (WMNS)/button_b'), 20, FailureHandling.CONTINUE_ON_FAILURE)) {
    'button_US M 10  W 115'
    WebUI.click(findTestObject('Size Element (WMNS)/button_b'))
} else {
    'button_US 11'
    WebUI.click(findTestObject('Size Elements (MENS)/button_c'))
}

I’ve updated the code to your above recommendation and now it returns this error:

11-25-2019 02:40:13 AM Test Cases/Finalised Scripts/Sizing - Else If Statement

Elapsed time: 27.582s

Test Cases/Finalised Scripts/Sizing - Else If Statement FAILED.

Reason:

groovy.lang.MissingMethodException: No signature of method: Script1573900914844.verifyElementVisible() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.lang.Integer, com.kms.katalon.core.model.FailureHandling) values: [TestObject - 'Object Repository/Size Elements (MENS)/button_a', ...]

at Sizing - Else If Statement.run(Sizing - Else If Statement:22)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at TempTestCase1574611809527.run(TempTestCase1574611809527.groovy:23)

Sorry. My bad. I updated the post.

You might also try waitForElementVisible

1 Like

@jai.kowalik

verifyElementVisible is part of the WebUI API, so please use something like:

if(WebUI.verifyElementIVisible( .... ))

and remove the timeout (the second parameter I mean).

1 Like

Apologies for the belated reply, I managed to get the logic working with your help! Did a bit of research so now I understand the integration of the ‘Script’ tab with ‘Manual’. Appreciate the help! :slight_smile:

1 Like

Thank you for this clarification, I did a bit of background research to your comment and figured it out… now the code likes me know! Appreciate your help as well :slight_smile: