Forum Discussion

enriquebravo's avatar
enriquebravo
Contributor
2 years ago
Solved

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 version 15.40. I am new to the tool and I am still trying to find my way in it. I understand that Events are an option when it comes to handling action at start/end of a test. What I am trying to do is that in the case an object is not found the test execution is not interrupted and the error is handled. From what I have read so far, seems like the only option is to control these type of situations via conditional statements.

 

Is there an option similar to what was used in UFT called, recovery scenarios?

 

I have changed my Error Handling Playback project settings to Continue running for all type of errors, but the exception handling does not kick in.

 

The steps that I am running are related to signing in to a Microsoft account.

There are two scenarios:

 

Scenario 1: A user is logging in for the first time, so credentials need to be entered.

Scenario 2: The user has previously logged in and the account is listed.

 

The most common scenario is that the account is listed already, however, I would like the script to handle the situation in which the account is not listed. In that case, the script should click the "Other account" option to enter user credentials.

 

I force the error to occur and TC reports that the expect account does not exist, however, even though the object is not visible it can still be highlighted from the namemapping. I created a script to "handle" the situation. I am also new to JavaScript so I think I am missing maybe parameters or something else to catch the error and continue the execution. For some reason my catch block is never executed.

//Error handling function

function test(){

try {

//Checks if the object exists.

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

//Perform error handling steps

} catch (exception) {
KeywordTests.Enter_Microsoft_Credentials;
     }
}

 

Seems like converting a keyword test that includes try, catch blocks cannot be converted into scripts.

 

Any help is appreciated, thanks.

  • 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.

     

3 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    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.

     

    • enriquebravo's avatar
      enriquebravo
      Contributor

      Thank you. Using the Find option worked better. It was a mix of organizing my logic better, but your explanation definitely came in handy. I am liking this tool better as I work with it.

       

      Regards,

       

      Enrique.

    • jana_kiran1991's avatar
      jana_kiran1991
      New Contributor

      Hi AlexKaras,

       

      Thank you for the reply and the solution you explained has worked for me.