Forum Discussion

frank_vanderstr's avatar
frank_vanderstr
Contributor
9 years ago

Catching Exception for Object Click

I am trying to add some error handling and tear down functions into my current test scripts. I can't seem to understand how to properly use the try catch block to initiate tear down functions when a web object cannot be found.

 

Here is an example of what I am trying to do

 

try {
    var browser = Sys.Browser("*");
    var Page1 = browser.Page("*");
    props = new Array("contentText", "className", "Visible", "VisibleOnScreen");
    values = new Array(categoryname, "categoryButton", true, true);
    var category = Page1.FindChild(props, values, 30);
    category.Click();
 
    Page1.Wait();
  }
  catch (error) {
    Log.Error("Category group error occured");
    ClickMyAccount();

    Logoff();
  }

 

 

I found out that if an object is not found, TestComplete does not throw an exception which can be caught, so I am unsure how to proceed from here.

 

I also have debug agent enabled for my projects.

 

Any suggestions would be appreciated.

8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The problem is that the "does not exist" is not a JavaScript code exception so it's not going to fail as a code exception. This is why the "catch" block isn't catching it.

     

    The other problem is that you don't have any checking in your code to detect whether or not you actually found the item you are looking for before you click it.  If that's your desire, try this (altered/added code in bold):

    try {
        var browser = Sys.Browser("*");
        var Page1 = browser.Page("*");
        props = new Array("contentText", "className", "Visible", "VisibleOnScreen");
        values = new Array(categoryname, "categoryButton", true, true);
        var category = Page1.FindChild(props, values, 30);
        if (!category.Exists) throw Error("Category " + categoryname + " not found");
        category.Click();
     
        Page1.Wait();
      }
      catch (error) {
        Log.Error("Category group error occured: " + error.message);
        ClickMyAccount();
        Logoff();
      }

     

    • frank_vanderstr's avatar
      frank_vanderstr
      Contributor

      Thanks, I think that is what I am looking for.

       

      My next question is how to best exit the current script and move to the next one, since so far even if the exception is caught, my script attempts to perform later steps.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Well, the function you posted is handling it's exception and then doing stuff... you need to determine, then, if, when it exits, if the next function should execute.  You need the code to return some sort of indicator that, yes, we can keep going... So... that said... you might want to try something like:


        try {
            var successful = false;
            var browser = Sys.Browser("*");
            var Page1 = browser.Page("*");
            props = new Array("contentText", "className", "Visible", "VisibleOnScreen");
            values = new Array(categoryname, "categoryButton", true, true);
            var category = Page1.FindChild(props, values, 30);
            if (!category.Exists) throw Error("Category " + categoryname + " not found");
            category.Click();
         
            Page1.Wait();
            successful = true;
          }
          catch (error) {
            Log.Error("Category group error occured: " + error.message);
            ClickMyAccount();
            Logoff();
          }
        finally {
            return successful;
        }

        Then, in whatever code is calling that function, do a check like

         

        if (!myFunction()) throw Error('blah');

        Or something else... basically, tell TestComplete exactly what you want to do if the function does not return successful.  This is actually a pretty fundamental key to a well constructed test suite/framework... that you build this kind of detection in to determine if each integral step of a test case is successful and, if it's not, mark the case as failed and "clean up" and then move on, either to the next test case or end the project.