Execute if/else when Element exists

Hello, Do you know the if statement code for mobile testing?

To whom it may concern,

In Katalon Studio 7.0.0, we have already fixed the bug that caused the verifyElementPresent() and verifyElementNotPresent() keywords to throw an exception instead of returning false when the verified elements do not exist.

Please first check out the release note and click here to download if you want to try version 7.0.0 (beta) in advance.

Happy Testing!

Jass

1 Like

works for me, thank’s

1 Like

Thank you very much. It works for me. :blush:

I want to verify that click a button to open in a new tab for Dev environment but there is no button in Prod environment.

Thread.sleep(2000)
if (WebUI.verifyElementPresent(findTestObject('Object_Frontend/button'), 1, FailureHandling.OPTIONAL)){
    Thread.sleep(2000)
    WebUI.click(findTestObject('Object_Frontend/button'))
    WebUI.getWindowTitle() == 'Finally you got  a new tab'
    WebUI.closeBrowser()
} else {
    WebUI.closeBrowser()
}

thanks. its work.

Issue is still present on 7.4.3. I tried to manage this using try/catch but it seems impossible to handle it.

  	public boolean verifyWorkFlowNavCurrentSectionAndDevice() throws Exception
	{
		String path = 'Home/DevicesTab/Devices/TopNoNameTab/'

		try {
			WebUI.verifyElementPresent(findTestObject(path + 'ul_NavBar'), Main.DELAY_004, FailureHandling.OPTIONAL)	
		} catch (Exception e) {
			Main.writeToLog(true, WebUI.concatenate('[INFO] --> Exception detected 1'))
		} catch (RuntimeException re) {
			Main.writeToLog(true, WebUI.concatenate('[INFO] --> Exception detected 2'))
		} catch (WebElementNotFoundException we) {
			Main.writeToLog(true, WebUI.concatenate('[INFO] --> Exception detected 3'))
		} finally {
			Main.writeToLog(true, WebUI.concatenate('[INFO] --> Exception detected 4'))
		}

		return false
	}

and the result is always the same…

2020-04-30 15:32:41.968 DEBUG rity Feeds - Read Current Security Feeds - 4: if (!(dev.verifyWorkFlowNavCurrentSectionAndDevice("Security Feeds")))
2020-04-30 15:32:46.202 INFO  c.k.k.c.webui.common.WebUiCommonHelper   - Unable to find the element located by 'By.xpath: //ul[@class='nav nav-pills workflow_nav']'. Please recheck the objects properties to make sure the desired element is located. 
2020-04-30 15:32:47.008 WARN  c.k.k.core.keyword.internal.KeywordMain  - com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/Home/DevicesTab/Devices/TopNoNameTab/ul_NavBar' located by 'By.xpath: //ul[@class='nav nav-pills workflow_nav']' not found (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/Home/DevicesTab/Devices/TopNoNameTab/ul_NavBar' located by 'By.xpath: //ul[@class='nav nav-pills workflow_nav']' not found
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword$_verifyElementPresent_closure1.doCall(VerifyElementPresentKeyword.groovy:85)
	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword$_verifyElementPresent_closure1.call(VerifyElementPresentKeyword.groovy)
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.verifyElementPresent(VerifyElementPresentKeyword.groovy:92)
	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.execute(VerifyElementPresentKeyword.groovy:68)
	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:72)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(WebUiBuiltInKeywords.groovy:1444)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementPresent$8.call(Unknown Source)
	at oneConfig.Device.verifyWorkFlowNavCurrentSectionAndDevice(Device.groovy:2557)
	at oneConfig.Device$verifyWorkFlowNavCurrentSectionAndDevice$2.call(Unknown Source)
	at TC - Security Feeds - Read Current Security Feeds.run(TC - Security Feeds - Read Current Security Feeds:28)
	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.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:169)
	at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:142)
	at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:91)
	at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
	at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
	at TempTestSuite1588224720185.run(TempTestSuite1588224720185.groovy:37)
)
2020-04-30 15:32:47.017 INFO  com.kms.katalon.core.util.KeywordUtil    - 2020-04-30 15:32:47 --> [INFO] --> Exception detected 4

Hello, this is a if/else statement for Mobile testing. I don’t know the reason why the script will give me an error stating

Failed to check for element ‘Object Repository/Android-GoogleFit/GoogleFit-text’ exist (Root cause: com.kms.katalon.core.exception.StepFailedException: Element ‘Object Repository/Android-GoogleFit/GoogleFit-text’ not found

This is my condition logic:

if ( Mobile.verifyElementExist(findTestObject(‘Object Repository/Android-GoogleFit/GoogleFit-text’), GlobalVariable.G_Timeout_long)) {
Mobile.tap(findTestObject(‘Object Repository/Android-GoogleFit/GoogleFit - nextBtn’), GlobalVariable.G_Timeout_long)
Mobile.delay(GlobalVariable.G_Delay_long)
Mobile.tap(findTestObject(‘Object Repository/Android-GoogleFit/Google-emailAdd’), GlobalVariable.G_Timeout_long)
} else {
Mobile.verifyElementExist(findTestObject(‘Object Repository/Android_HOME/Home - TOP_nav’), GlobalVariable.G_Timeout_long)
}

I’m expecting that if the first condition if not existed then it will go to else.

Thank you for the help.

We can check the existence of the web element with selenium findwebelements list size as follows,
if (WebUiCommonHelper.findWebElements(findTestObject(‘Object Repository/abc/button_got_it’), 5).size() > 0) {
WebUI.click(findTestObject(‘Object Repository/abc/button_got_it’))
}

Someone can help me. I need that when the loop is fulfilled the test continues with the other commands, someone can guide me how I can achieve

Issue still persists in version 8.2.0 with the verifyElementChecked() keyword.

I’m using v.8.2.5, and the verifyElementPresent() is throwing an exception instead of returning “false”.

Same here. It doesn’t execute the else at all. If the element doesn’t exist it fails the test.

This helps! Thank you so much!!

Is it worked?

Im having same issue

What product and environment are you using? This thread has Mobile, Web and Katalon Recorder. Which one are you asking about?

Im working on web
Scenario 1: when the text is visible it will enter into if block and execute the block of code

Ex : success msg will appear when we save the element successful

Tried WebUI.VerifyElementPresent as well as getText()
Positive scenario is working fine but negative scenario not executed

scenario 2: when the text is not visible it is not entering into the else block

Ex: when we get any error it will show the error message like “Not able to save successfully”

Okay. I may make a few assumptions.

"save and verifyTextPresent"
WebUI.click(findTestObject('myPage/a_Save'))
WebUI.waitForPageLoad(10)

// do we need a delay here for text to show?
if (WebUI.verifyTextPresent("The Save was Successful", false, FailureHandling.OPTIONAL)) {
    WebUI.comment("Positive scenario has occurred")
} else {
    WebUI.comment("Negative scenario has occurred")
    WebUI.comment("Not able to save successfully")
}

Note that I put in the FailureHanding option to the verifyTextPresent statement. I use this in my if statements as I think it adds stability. You should be able to use CONTINUE_ON_FAILURE also. Obviously, don’t use STOP_ON_FAILURE.

Thank you for your quick reply. I will try this logic.

Yeah. I think you should be good if you add the FailureHandling option. Based upon the response due to the different FailureHandling options, I would go with the OPTIONAL one.

image

image

1 Like