VerifyElementPresent and VerifyElementNotPresent usage in 'if, else if' condition

I’ve to execute certain codes if the element is present and certain codes if the element is not present. So, i’ve used if, else if condition below
if (WebUI.verifyElementPresent(findTestObject(‘Element A’),
0)) {
---- Code A -----
}
else if (WebUI.verifyElementNotPresent(findTestObject(‘Element A’),
0)) {
---- Code B -----
}
When execute indicates that all steps in the test case is passed but Test Execution status as failed stating either the element mentioned in if or else if unable to find.

Can someone help with above? I need test execution to be showing as Passed with above condition. In My Project Settings i’ve configured as ‘Continue on Failure’

This is the Failure response i got after execution
=============== ROOT CAUSE =====================
Caused by: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Nutramigen/Element Text verification/Page_Nutramigen Enfamil/Verify Why tooltip label’ located by ‘//div[@class=‘form-control form-control–birth-date’]//button’ not found

For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshooting.html

06-21-2022 06:12:36 PM Test Cases/Sample Request/Styling on page

Elapsed time: 1m - 53.861s

Test Cases/Sample Request/Styling on page FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Nutramigen/Element Text verification/Page_Nutramigen Enfamil/Verify Why tooltip label’ located by ‘//div[@class=‘form-control form-control–birth-date’]//button’ not found (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Nutramigen/Element Text verification/Page_Nutramigen Enfamil/Verify Why tooltip label’ located by ‘//div[@class=‘form-control form-control–birth-date’]//button’ 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:74)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(WebUiBuiltInKeywords.groovy:1464)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementPresent$3.call(Unknown Source)
at Styling on page.run(Styling on page:352)
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:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1655815344632.run(TempTestCase1655815344632.groovy:25)
)
at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:39)
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:74)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(WebUiBuiltInKeywords.groovy:1464)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementPresent$3.call(Unknown Source)
at Styling on page.run(Styling on page:352)
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:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1655815344632.run(TempTestCase1655815344632.groovy:25)
Caused by: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Nutramigen/Element Text verification/Page_Nutramigen Enfamil/Verify Why tooltip label’ located by ‘//div[@class=‘form-control form-control–birth-date’]//button’ not found
… 21 more

Try this pattern:

if(WebUI.verifyElementPresent(findTestObject('Element A'), 10, FailureHandling.OPTIONAL)) {
---- Code A -----
} else {
---- Code B -----
}
1 Like

verifyElementPresent() emits a verbose StackTrace message when the condition is FALSE even when you specify FailureHandling.OPTIONAL.

Possibly you do not want to see the StatckTrace, do you?

So you would prefer waitForElementPresent() keyword to verifyElementPresent(). The waitForElementPresent() will emit no stack trace.

2 Likes

Thanks for the suggestion @Russ_Thomas. It worked for me

Thanks for the suggesstion @kazurayam . It worked for me but just a clarification. You said about stack track if even VerifyElement fails. Is this viewable only in console like below?
2022-06-22 19:25:33.927 INFO c.k.k.c.webui.common.WebUiCommonHelper - Unable to find the element located by ‘By.xpath: //span[@class=‘DayPicker-NavButton DayPicker-NavButton–next’]’. Please recheck the objects properties to make sure the desired element is located.
2022-06-22 19:25:34.126 WARN c.k.k.core.keyword.internal.KeywordMain - com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Nutramigen/Calendar/Verify Next Month button’ located by ‘//span[@class=‘DayPicker-NavButton DayPicker-NavButton–next’]’ not found (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Nutramigen/Calendar/Verify Next Month button’ located by ‘//span[@class=‘DayPicker-NavButton DayPicker-NavButton–next’]’ 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:74)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(WebUiBuiltInKeywords.groovy:1448)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementPresent$7.call(Unknown Source)
at Date Range.run(Date Range:75)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

I do not understand your question. As you see …

Do you want to view StackTraces somewhere else as well?

Nope. I actually got to know about StackTraces after you shared about those. After that i checked and noticed in console so only asked if they are shown only in console.

Hi
Anyone can sort out this issue

Condition is not working,
test case must be failed
because on site I m getting “This site is unsafe” which is storing in current_status1

current_status1 = WebUI.getText(findTestObject(‘Google/No unsafe content found’))
System.out.println(current_status1)
if (current_status1 == ‘No unsafe content found’) {
System.out.println(“if Condition”)
WebUI.delay(2)
} else {
(current_status1 == ‘No available data’)
System.out.println(“Else Condition”)
}

Hi @Noor_Ahmed_Khan
Please try with verifymatch instead of if, else condition. In if, else condition for sure one of them will pass then test case will be considered passed.

What are you trying to do with the above? Are you trying to make a “second” if condition. If so, then that’s not the way (currently it doesn’t do anything. Yes it equates to true but nothing is done with it). See how to do an “else if” condition below:

if(condition) {   
   statement #1   
   statement #2   
   ...   
} else if(condition) {   
   statement #3   
   statement #4   
} else {   
   statement #5   
   statement #6   
}  

Groovy Decision Making - Javatpoint

Edit: Note that you do not need a final “else” condition. If you don’t have one, or don’t need one, then don’t add it.