Catch block not executed correctly

Hey again! :slight_smile:

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…

  1. 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…
  2. 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...")
		}
	}

Hi Morgan,

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 :thinking: Any ideas what might be going on there?

You probably want to ditch the else altogether. In effect, you now have a true/fail test, not a true/false test:

try {
  if(verifyElementVisible(...)) {
    markPassed(...)
  }
} catch(Exception e) {
  if(e.message.contains("stuff") {
    //do something
  } else {
    //do something else
  }
}

very true! I’ll give it a shot - thanks!

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
)

Sorry, I got super busy…

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 :stuck_out_tongue:

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?

Maybe this is the answer.

I had similar issue few months ago and it looks like the exception is consumed and handled on other layer.

1 Like

hmmm… I’ll give it a shot and report back. Thanks!

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...")
		}
	}

So, using this, it hits the first if statement, logs the following error…

ERROR c.k.k.core.keyword.internal.KeywordMain  - ❌ Object 'Object Repository/Card Template/RETAILER/btnCartAdd' is NOT visible (Root cause: com.kms.katalon.core.exception.StepFailedException: Object 'Object Repository/Card Template/RETAILER/btnCartAdd' is NOT visible

… does not hit the else statement but then hits the catch / println,…?

2019-05-13 10:27:07.965 DEBUG e Add to Cart Increments Campaign Spends - 3: catch (com.kms.katalon.core.exception.StepFailedException e)
2019-05-13 10:27:07.967 DEBUG e Add to Cart Increments Campaign Spends - 1: println("Unable to locate correct sponsored product but it's cool...")
Unable to locate correct sponsored product but it's cool...

Weird that it’s both still reporting an error and not hitting the else. :thinking:

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.

1 Like

Thank you, moving on :wink:

@Morgan told you trying may be catch-ing :))

@devalex88 any updates on the issue to catch step-failed in testcase?

as far as i remember it was a long discussion on the matter if is a bug or not …

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…”)
}
}
}