Forum Discussion

SarahEdwards's avatar
SarahEdwards
Contributor
8 years ago

Calling a script from an Event Handler

So, I just learned about Event Handlers. What amazing things!

 

However, I'd like to send an email from the OnLogError event.

My handler script is as follows:

 

function GeneralEvents_OnLogError(Sender, LogParams)
{
Log.Picture(Sys.Desktop.Picture(), "Image of the whole screen", "", pmHigher);
PackResults(); //This is the name of my email function.
}

 

When my tests hit an error, I get a javascript error:

 

ReferenceError
PackResults is not defined
Error location:
Unit: "SmokeTest\SmokeTest\Script\SmokeTestScript"
Line: 4 Column: 3.
 
I'd imagine this is just a syntax and/or scope issue. How do I call my email function, PackResults()?

 

  • PackResults I'm assuming is in a different script unit than your event handler.  So... this is a two-step thingie

    1) Add at the top of the unit for the event handler the following

     

    //USEUNIT MyScriptUnit

    where "MyScriptUnit" is the code unit containing PackResults

     

    2) Change your code to:

    function GeneralEvents_OnLogError(Sender, LogParams)
    {
    Log.Picture(Sys.Desktop.Picture(), "Image of the whole screen", "", pmHigher);
    MyScriptUnit.PackResults(); //This is the name of my email function.
    }

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    PackResults I'm assuming is in a different script unit than your event handler.  So... this is a two-step thingie

    1) Add at the top of the unit for the event handler the following

     

    //USEUNIT MyScriptUnit

    where "MyScriptUnit" is the code unit containing PackResults

     

    2) Change your code to:

    function GeneralEvents_OnLogError(Sender, LogParams)
    {
    Log.Picture(Sys.Desktop.Picture(), "Image of the whole screen", "", pmHigher);
    MyScriptUnit.PackResults(); //This is the name of my email function.
    }

     

    • SarahEdwards's avatar
      SarahEdwards
      Contributor

      UseUnit wasn't working for me, but it did lead me to this article (titled "USEUNIT Statement -- About" in the index help files):

      https://support.smartbear.com/testcomplete/docs/scripting/calling-routines/declared-in-another-unit/from-gui.html which accomplishes the same thing. My event handler script now looks like this:

       

       

      var EmailResults = require("EmailResults"); //EmailResults is the parent script that holds the PackResults() function.
      
      function GeneralEvents_OnLogError(Sender, LogParams)
      {
      Log.Picture(Sys.Desktop.Picture(), "Image of the whole screen", "", pmHigher);
      EmailResults.PackResults(); //This is the name of my email function.
      }

       

      Now I just need to ensure that the error that stops everything is included in the log that's emailed.

       

      Thank you so much, Tristaanogre!

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        USEUNIT should work but note that it needs to 

         

        a) start with the two slashes // and 

        b) be in all caps.  Otherwise, it won't work.

         

        However, the method you used is just fine as is.  Glad you got it working!