Forum Discussion

shaifali_pandya's avatar
shaifali_pandya
Occasional Contributor
12 years ago

Global variable

Need help understand why a global array vailable declared in unit1.sj outside of any function is not available in another script file.



Unit1.sj

//USEUNIT ErrorReporter


var vectErrMsgs = new Array();

function Trial()

{

if (!ErrorReporter.ReportError("Unable to understand", "Global array not populated as expected.", "this.function", false))

  Log.Error("Problem");

}



ErrorReporter.sj



function ReportError(strErrCategory, strErrText, strErrOrigin, bCapDesktop)

{

  var myDesktopPic = null;

  if ((bCapDesktop ==null) || (bCapDesktop!= false))

    myDesktopPic = Sys.Desktop.Picture(); //Take picture of the desktop

   

    var strMessageText = strErrCategory + " - " + strErrText + "\n\n" + strErrOrigin;

    vectErrMsgs.push([myDesktopPic, strMessageText]); //push err message text into an array.

   

    return true;

}


 


5 Replies

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Shef, Script Units can only access variables of other Script Units if a USEUNIT is included.


     


    In Script Unit ErrorReported.sj, you do not have a USEUNIT for Unit1.sj, which has the global Array, therefore it cannot access it.


     


    The problem you will have though is that JScript does not allow circular references, i.e. because Unit1.sj contains //USEUNIT ErrorReporter, ErrorReporter,sj CANNOT contain //USEUNIT Unit1


     


    In your case, the only option is to move var vectErrMsgs = new Array(); into ErrorReporter.sj


     


    ErrorReporter.sj


     


    var vectErrMsgs = new Array();


     


    function ReportError(strErrCategory, strErrText, strErrOrigin, bCapDesktop)


    {


      var myDesktopPic = null;


      if ((bCapDesktop ==null) || (bCapDesktop!= false))


        myDesktopPic = Sys.Desktop.Picture(); //Take picture of the desktop


       


        var strMessageText = strErrCategory + " - " + strErrText + "\n\n" + strErrOrigin;


        vectErrMsgs.push([myDesktopPic, strMessageText]); //push err message text into an array.


       


        return true;


    }


     


    but this may not be what you are wanting to acheive.


     


    Regards,


    Phil Baird

  • Shef, have you thought about using project variables?  You can set them to be either persistent (value remains across project executions) or temporary (reset with each project execution).



    Double click on the project node in your Project Explorer, and click on the Variables tab.



    You can then access your variables in code using Project.Variables.VariableName



  • shaifali_pandya's avatar
    shaifali_pandya
    Occasional Contributor
    Phil,



    I want to have a global array delcared such that it is accessible from all the test script files and library functions. If I declare it in ErrorReporter then it will be declared every time any function from ErrorReporter is called.

    What we need is something equivalent of  "appstate" of SilkTest

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Shef, that is not how Test Complete works


     


    When a test is run, every Script Unit in the Project is compiled and a singleton object created for each, meaning that ErrorReporter::vectErrMsgs will only be created once.


     


    Consider the following Script Units:


     


    Unit1.sj


    var vectErrMsgs = new Array();


     


    function test001() {


      vectErrMsgs.push( "Unit 1" );


    }


     


    Unit2.sj


    //USEUNIT Unit1


     


    function test002() {


      Unit1.test001();


      Unit1.vectErrMsgs.push( "Unit 2" );


    }


     


    Unit3.sj



    Unit3.sj


    //USEUNIT Unit1


    //USEUNIT Unit2


     


    function test003() {


      Unit1.test001();


      Unit2.test002();


      Unit1.vectErrMsgs.push( "Unit 3" );


      


      for( var j = 0; j < Unit1.vectErrMsgs.length; j++ ) {


        Log.Message( Unit1.vectErrMsgs[ j ] );


      }


    }



     


    When Unit3.sj is run, it will output


     


    Unit 1


    Unit 1


    Unit 2


    Unit 3


     


    proving that Unit1::vectErrMsgs is only created once.


     


    Phil

  • karkadil's avatar
    karkadil
    Valued Contributor
    Hi Shef,



    Appstate in SilkTest is used to prepare tested application for further testing (perform pre- and post-testing actions).



    In TestComplete you can use Events Handling for this purpose (namely events OnStartTest and OnStopTest).