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