I have a carousel which I want do loop through (clicking next on the carousel navigation button) until a specific product is visible. The elements I want to use are both present in the dom and should be visible (hence why I went with verifyElementVisible).
After playing with this a bit and doing some research, Iâve come up with the below. Issues with thisâŚ
It seems to loop through 4 times regardless (at which point it always finds the product on the last slide) - this might be some other issue I havenât figured outâŚ
It never seems to catch the exception when it is unable to locate the element - this is my first time working with that, what am I doing wrong?
Other suggestions? Iâm psyched itâs mostly working but perplexed what Iâm missing here.
Thanks!
Morgan
import com.kms.katalon.core.exception.StepFailedException
<and others>
for (int i=0; i<4; i++) {
try {
if (WebUI.verifyElementVisible((sponsoredPrd), FailureHandling.CONTINUE_ON_FAILURE)) {
KeywordUtil.markPassed("Correct Product Located")
break;
} else {
KeywordUtil.logInfo("Product not located on current slide")
WebUI.click(findTestObject(btnNextSlide))
WebUI.delay(1) }
} catch (com.kms.katalon.core.exception.StepFailedException e)
{
this.println("Unable to locate correct sponsored product but it's cool...")
}
}
using FailureHandling.CONTINUE_ON_FAILURE the StepFailedException is not thrown, so it cannot be caught. Remove this argument and then you may be able to catch the exception if any appears.
I removed that and it does seem to now hit the catch statement (yay!) however it now wonât hit the else when it should Any ideas what might be going on there?
Hereâs where Iâm at - this works GREAT when the element is visible in the stupid carousel. If it is not, however, the test still fails and the exception is not caught. Greatly appreciate the help on this!
try {
if (WebUI.verifyElementVisible((sponsoredPrd))) {
KeywordUtil.markPassed("Correct product located")
break
}
} catch (StepFailedException e) {
if(e.message.contains("is NOT visible")) {
KeywordUtil.logInfo("Unable to locate correct product immediately but it's cool...")
WebUI.click(findTestObject(btnNextSlide))
WebUI.delay(2);
}
}
}
Errors loggedâŚ
2019-05-09 14:13:21.651 ERROR c.k.k.core.keyword.internal.KeywordMain - â Object 'Object Repository/Card Template/GiantEagle/btnCartAdd' is NOT visible (Root cause: com.kms.katalon.core.exception.StepFailedException: Object 'Object Repository/Card Template/GiantEagle/btnCartAdd' is NOT visible
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.doCall(VerifyElementVisibleKeyword.groovy:83)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.call(VerifyElementVisibleKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.verifyElementVisible(VerifyElementVisibleKeyword.groovy:97)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.execute(VerifyElementVisibleKeyword.groovy:67)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:56)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementVisible(WebUiBuiltInKeywords.groovy:372)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementVisible$4.call(Unknown Source)
at Verify Cart Page Add to Cart Increments Campaign Spends.run(Verify Cart Page Add to Cart Increments Campaign Spends:93)
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:336)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:327)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:306)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:232)
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 TempTestCase1557425556018.run(TempTestCase1557425556018.groovy:21)
)
2019-05-09 14:13:22.049 ERROR c.k.k.core.keyword.internal.KeywordMain - â Unable to verify object 'Object Repository/Card Template/RETAILER/btnCartAdd' is visible (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to verify object 'Object Repository/Card Template/RETAILER/btnCartAdd' is visible
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.VerifyElementVisibleKeyword.verifyElementVisible(VerifyElementVisibleKeyword.groovy:97)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword.execute(VerifyElementVisibleKeyword.groovy:67)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:56)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementVisible(WebUiBuiltInKeywords.groovy:372)
at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementVisible$4.call(Unknown Source)
at Verify Cart Page Add to Cart Increments Campaign Spends.run(Verify Cart Page Add to Cart Increments Campaign Spends:93)
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:336)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:327)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:306)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:232)
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 TempTestCase1557425556018.run(TempTestCase1557425556018.groovy:21)
Caused by: com.kms.katalon.core.exception.StepFailedException: Object 'Object Repository/Card Template/RETAILER/btnCartAdd' is NOT visible
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.doCall(VerifyElementVisibleKeyword.groovy:83)
at com.kms.katalon.core.webui.keyword.builtin.VerifyElementVisibleKeyword$_verifyElementVisible_closure1.call(VerifyElementVisibleKeyword.groovy)
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)
... 17 more
)
Okay, this is starting to look buggy - which is crazy, these APIs are in use all over and they must work (youâd think).
Letâs try this, just as a test. Create a new test case - real simple, no for loop.. Add this:
try {
if (WebUI.verifyElementVisible(something-NON-existent) {
println "should NOT see this message"
}
} catch (Exception e) {
println "SHOULD see this message"
}
âBut Russ, why didnât you try this yourself?â
Because I donât use the WebUI APIs and I donât use Test Objects
If the docs are correct, verifyElementVisible should throw the very exception youâre trying to catch:
@Marek_Melocik Can you take another look? Seems to me, sheâll never get a false from verifyElementVisible if she includes FailureHandling, but without it, sheâs not getting the catch. Am I missing something?
it is problem âFailureHandlingâ. It will not throw any exception so what you can try below piece of code by throwing explicit exception
boolean flag ;
for (int i=0; i<4; i++) {
flag = true
try {
if(WebUI.verifyElementVisible((sponsoredPrd),FailureHandling.CONTINUE_ON_FAILURE)) {
KeywordUtil.markPassed("Correct Product Located")
break;
} else {
KeywordUtil.logInfo("Product not located on current slide")
WebUI.click(findTestObject(btnNextSlide))
flag = false
WebUI.delay(1) }
if(!flag){
throw new Exception("Element is not visbile after delay of 4 "+i+" seconds")
}
} catch (com.kms.katalon.core.exception.StepFailedException e){
this.println("Unable to locate correct sponsored product but it's cool...")
}
}
As I said earlier, this is sounding buggy. The reason why was posted my @Marek_Melocik in his bug report above. You basically need a different approach, Morgan.
Hello Morgan,
Just now i looked into detail and used the below code(using my repo), and it worked fine. I also observed using katalon that âSometimes when xpath is not correct or object is not correct that time it will skip all the further steps and directly go to catch statement (if written). Actually this is kind of bug in katalon and you can observe the same during debuggingâ. So please check your xpath again and use the below code. If error comes please share the screenshot once.
boolean flag = false
for (int i=0; i<4; i++) {
try {
flag =WebUI.verifyElementVisible((sponsoredPrd),FailureHandling.CONTINUE_ON_FAILURE)
if(flag == true) {
KeywordUtil.markPassed(âCorrect Product Locatedâ)
break;
} else {
KeywordUtil.logInfo(âProduct not located on current slideâ)
WebUI.click(findTestObject(btnNextSlide))
Thread.sleep(1000) }
} catch (com.kms.katalon.core.exception.StepFailedException e){
this.println(âUnable to locate correct sponsored product but itâs coolâŚâ)
}
}
}