Forum Discussion

k_de_boer03's avatar
k_de_boer03
Contributor
9 years ago
Solved

Throwing exceptions in script extensions

Hi, 

 

I am learning to write a script extension in JScript. And I can't figure out how to throw an exception in the extension, and catch it in TestComplete. For example the following code in the script extension:

 

 

function GetError(){
throw new Error("Something went wrong"); }

When I call this code in TestComplete:

 

 

 

function TestExtension(){
  try{
    MyExtension.GetError();
  } catch(error){
    //Do something with the error
  }
}  

 

I expected error.description to return "Something went wrong". However, the error object I am getting is an empty object with no properties or methods at all.

 

Can anyone help me get this working?

 

I have read this article: http://support.smartbear.com/viewarticle/57510/

 

I am not really fond of using Log.Error instead because it may trigger the OnLogError event handler when you don't want to. 

 

I also dislike using the "last_status" approach. I want my code to fail when I get an uncaught exception in my code, not silently continue when I forget to add a status code check for every function call.

 

Regards,

Kevin 

 

 

 

  • There is a restriction with passing exceptions across units:

     

    In TestComplete, if a script calls an object method that is declared in another unit, and this method throws an exception, the exception cannot be caught and handled by the try {} block in the main script. Keep this in mind when developing object-oriented test scripts using native JScript functionality.

     

    mentioned in this article  Supported Scripting Languages - Specifics of Usage

3 Replies

  • There is a restriction with passing exceptions across units:

     

    In TestComplete, if a script calls an object method that is declared in another unit, and this method throws an exception, the exception cannot be caught and handled by the try {} block in the main script. Keep this in mind when developing object-oriented test scripts using native JScript functionality.

     

    mentioned in this article  Supported Scripting Languages - Specifics of Usage

    • k_de_boer03's avatar
      k_de_boer03
      Contributor

      Hmm that's actually very disapointing. I knew about extending objects through prototype in another unit didn't work (for some reason). But now exceptions as well :(

       

      I will try to think of some kind of workaround or design pattern as an alternative.

       

      Thanks for your answer,

      • bbi's avatar
        bbi
        Contributor

        Hello,

         

         

        "I am not really fond of using Log.Error instead because it may trigger the OnLogError event handler when you don't want to"

         

        For this u can use anyway the Log.Error approach and manage unwanted log error event by using an event handler which will skip unwanted errors. The event handler must be in the main source code because events are fired by the testitem played.

         

        Below here an example of a complete handler. It does 2 things, it manage multiple same errors raises and publish only one log entry and it skip unwanted errors raised.

         

        The advantage is to fully centralized the error management.

         

        /* ---------------------------------------------------------------------------
           Gérer les erreurs répetées via gestion des events
        --------------------------------------------------------------------------- */
        function GeneralEvents_OnLogError(Sender, LogParams) {
          if (LogParams.MessageText == LASTERRORLOG) {
            SAMEERRORCOUNT++;
            LogParams.Locked = true;
          }
          else {
            // Pour éviter un test rouge 
            // - par l'erreur aléatoire de IT_Launch à sa sortie, 
            // - sur une déconnexion de la VM 
            // - ou sur une VM non visible
            var skip = aqObject.CompareProperty(LogParams.MessageText, cmpContains, 'exception was encountered while terminating', false, lmNone);
            if (!skip) {
              skip = aqObject.CompareProperty(LogParams.MessageText, cmpContains, 'is invisible', false, lmNone); 
            }
            if (!skip) { 
              skip = aqObject.CompareProperty(LogParams.MessageText, cmpContains, 'because the user session is disconnected', false, lmNone);
            }  
            if (skip) { 
              Log.Message(LogParams.MessageText);
              LogParams.Locked = true;
            }  
            else {	  
              if (SAMEERRORCOUNT > 1) {
                Log.Warning('Erreur "' + LASTERRORLOG + '" répétée ' + SAMEERRORCOUNT.toString() + ' fois !');
              }  
              LASTERRORLOG = LogParams.MessageText;
              SAMEERRORCOUNT = 0;
            }
          }
        }

         

        Of course u need to initialize the variables used, in script extension init routine for example.

         

        var LASTERRORLOG = '';
        
        var SAMEERRORCOUNT = 0;