Unable to verify IF element present or not


#1

I want to click a button if the button is present. if it is not available move to the next step and proceed. Tried the below step

if (WebUI.verifyElementPresent(findTestObject(‘Object Repository/btn_AddNew’), 10)==true) {
WebUI.click(findTestObject(‘Object Repository/btn_AddNew’))
}

Script fails at the if conditional statement

2019-01-29 16:12:15.537 e[34mINFO e[0;39m e[36mc.k.k.c.webui.common.WebUiCommonHelper   -e[0;39m e[39mUnable to find the element located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']'. Please recheck the objects properties to make sure the desired element is located. e[0;39m
2019-01-29 16:12:16.439 e[1;31mERRORe[0;39m e[36mc.k.k.core.keyword.internal.KeywordMain  -e[0;39m e[31m❌ com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/btn_AddNew' located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']' not founde[0;39m
2019-01-29 16:12:16.951 e[1;31mERRORe[0;39m e[36mc.k.k.core.keyword.internal.KeywordMain  -e[0;39m e[31m❌ Unable to verify object 'Object Repository/btn_AddNew' is present (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/TAS/Page_Transportation Asset System -/btn_AddNew' located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']' not found)e[0;39m
2019-01-29 16:12:17.000 e[1;31mERRORe[0;39m e[36mc.k.k.core.main.RawTestScriptExecutor    -e[0;39m e[31m❌ if (verifyElementPresent(findTestObject("Object Repository/btn_AddNew"), 10) == true) FAILED.e[0;39m
e[31mReason:e[0;39m
e[31mcom.kms.katalon.core.exception.StepFailedException: Unable to verify object 'Object Repository/btn_AddNew' is present (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/btn_AddNew' located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']' not found)e[0;39m
e[31m	at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:36)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:65)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:27)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.verifyElementPresent(VerifyElementPresentKeyword.groovy:92)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.execute(VerifyElementPresentKeyword.groovy:68)e[0;39m
e[31m	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:53)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(WebUiBuiltInKeywords.groovy:1434)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementPresent.call(Unknown Source)e[0;39m

#2

The verifyElementPresent() method acts as an assertion, which means that if the method returns false, the test will stop, and the if statement won’t even be evaluated. The solution is to add a FailureHandling argument to the verifyElementPresent() method call. See the solution to a similar topic here:


#3

Hi everyone,

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