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ā¦ā)
}
}
}