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.

Other articles of interest:

2 Responses to A Try Catch example in Katalon Studio

  • Lucas says:

    Thanks for sharing a lot of Katalon tips and tricks. The Katalon team highly appreciate your support and feedback. To make Katalon Studio better and be the best free automation solution for testing community, we wish to coordinate with the expert users who love to write and share their knowledge and real practices of Katalon Studio for testing community.
    We are waiting for your response.
    The Katalon Team.

    • Don Pedro says:

      Hi Lucas. Thank you for the kind comments. I would be happy to help. Let me know how I could be of assistance.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments