Forum Discussion

enriquebravo's avatar
enriquebravo
Contributor
3 years ago

Implement Error Handling

Hi,   I am trying to implement a solution as a described on https://community.smartbear.com/t5/TestComplete-Questions/All-type-of-error-handling-in-one-unit/m-p/57651#M96232   I am using TC versi...
  • AlexKaras's avatar
    3 years ago

    Hi,

     

    > //Checks if the object exists.

    >  (Aliases.browser.pageSignInToYourMicrosoftAccount.textboxTestAccount.exist);

    This is the key point which is not always clear to those who start using TestComplete (TC).

    There are two ways how TestComplete addresses objects in test code. I call them 'explicit' and 'implicit' ones.

    Explicit way is when test code directly addresses objects like in your case. E.g.:

    Aliases.browser.pageSignInToYourMicrosoftAccount.textboxTestAccount

    Such line of code tells TC that you expect that browser, pageSignInToYourMicrosoftAccount and textboxTestAccount objects must exist. As this is expected, TC throws an error if any object along the referenced path does not exist. This is logical, because your explicit expectation was not met, so this means an error which must be reported to test log.

     

    There are cases when you expect that some object may not exist. This may be expected and you know how to handle this. In this case you must not reference objects explicitly but resort to implicit approach.

    With implicit approach, test code tries to find the required object via relevant .FindXXX() or .WaitXXX() methods provided by TC and proceed according to the search result.

    In your case, code might be like this:

    var obj = Aliases.browser.pageSignInToYourMicrosoftAccount.WaitAliasChild("textboxTestAccount", 500);

    if (obj.Exists)

      ...

    else

      ...

     

    Note, however, that the above code snippet will not work if pageSignInToYourMicrosoftAccount object is absent. This is because pageSignInToYourMicrosoftAccount object is referenced explicitly and thus TC expects that this object must exist.

    In this case your code might be like this:

    var obj = Aliases.browser.WaitAliasChild("pageSignInToYourMicrosoftAccount", 500);

    if (obj.Exists) {

      var oTextBox = obj.textboxTestAccount;

      oTextBox.Keys(...);

      ...

    }

    else

      ...

     

    I.e.: you must try to search for the first object that may be absent, but not the last one.

    Note, that the line like

    Aliases.browser.WaitAliasChild("pageSignInToYourMicrosoftAccount", 500).WaitAliasChild("textboxTestAccount", 500);

    will not work either if pageSignInToYourMicrosoftAccount object does not exist.

    This is because you will get an empty object as a result of searching for the pageSignInToYourMicrosoftAccount and then will try to call non-existing .WaitAliasChild() method for this empty object.

    So chaining methods calls do not work when you need to check the existence of more than one object along the objects chain and your test code must explicitly verify the existence of every object.

     

    Additional note should be done for try/catch statements that are also sometimes misinterpreted in the world of TestComplete.

    By design, try/catch handles runtime errors. Like, for example, division by zero or direct reference of some non-existing object's property.

    When some object does not exist within the chain of explicitly referenced objects (like in yours Aliases.browser.pageSignInToYourMicrosoftAccount.textboxTestAccount), this is rather not a runtime error, but the logical issue in test code which is handled by TestComplete. And thus it does not trigger exception and thus you will not get into the catch block of your code.

     

    Hope that above will make things more clear to you.