A Try Catch example in Katalon Studio

Since Katalon has the markError and markFailed commands, I don’t know if the Try Catch example is really needed. But, I did make use of it when trying to determine if a page loaded or generated an error.

After I click the link to load the page, I set the Try to the WebUI.verifyTextPresent(“Month To Date Sales Detail”, false). I need to determine if the text, Month To Date Sales Detail appears on the page. If it doesn’t, it means the page has an error and not only will the tests fail, but the data used for the page is invalid and needs manual investigation.

The Try is set to the text on the page. If that generates an error, the Catch statement then reads the Title of the page. This is written to the Warning log and I use the Throw command to break out of the test.

//Look for the text, Month To Date Sales Detail on the Page.
//If it's not there, there is a problem loading the page and the test should exit as the rest of the steps will fail
WebUI.click(findTestObject('Page_/Sales Dashboard/Monthly Details/Monthly Sales Accordion'))
try {
 elementPresent=WebUI.verifyTextPresent("Month To Date Sales Detail", false)
}
catch (Exception e) {
 title = WebUI.getWindowTitle()
 log.logWarning('ERROR: The title of the Sales Detail Page is:=' + title)
 throw new AssertionError('ERROR: The Sales Detail Page did not load correctly', e)
}

While this works and handles the error, I’m not sure if it’s the best method I could have come up with. Would it have been better and more consistent to use the GetText command with the CONTINUE_ON_FAILURE and mark the test as failed if the text didn’t appear? This would be more in line with how I check to see which tabs are available on a customer profile. This test was written quite awhile before my tabs example. This was more a brute force validation method, whereas the other seems a little more elegant and dynamic.

Either way, here is a very simple Try Catch example to determine of a page loaded correctly.

That's my story and I'm sticking to it.

Author Signature for Posts

0