Forum Discussion

Krishna_Kumar's avatar
Krishna_Kumar
Contributor
6 years ago

Project Variable

Hi Team,

i am trying to Create a object at run time and pass the same object for all other script.

for example,

in Method1 - i have created an excel object(Excel.Applilcation)

in Method2 - i want to access the object which is created in Method1 without creating the object again.

 

is it possible to store the created object in project variables and access them?

 

please help me out.

 

Kind Regards,

Krishna

  • Mr_Bro's avatar
    Mr_Bro
    6 years ago

    Hi Krishna_Kumar ,

     

    initialize all your required objects in the OnStartTest Event handler and assign the same objects to a project variable in the same time, 

    and after that were ever you want you van use the required object by assigning a new temperory variable,

     

    please refer the below code snippet for your reference and let me know if your problem is solved.

    //Event Handler
    function GeneralEvents_OnStartTest(Sender){
        if(Project.Variables.VariableExists("MyObject"))
           Project.Variables.RemoveVariable("MyObject") 
        Project.Variables.AddVariable("MyObject", "Object");
        Project.Variables.MyObject = Sys.OleObject("Excel.Application")
    }
    
    //Accessing the Project Variable in different script Routine
    function testingprojectVariable()
    {
      excelApp = Project.Variables.VariableByName("MyObject")
      excelApp.Workbooks.Open("C:\\Users\\sathishkumark\\Desktop\\Source.xlsx")
      excelApp.DisplayAlerts = true
    ////Your Excel Operations...
    .............. }
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Yes.  Create a project variable of the "Object" type.  That should allow you to store the object.

      • Mr_Bro's avatar
        Mr_Bro
        Frequent Contributor

        Hi Krishna_Kumar ,

         

        initialize all your required objects in the OnStartTest Event handler and assign the same objects to a project variable in the same time, 

        and after that were ever you want you van use the required object by assigning a new temperory variable,

         

        please refer the below code snippet for your reference and let me know if your problem is solved.

        //Event Handler
        function GeneralEvents_OnStartTest(Sender){
            if(Project.Variables.VariableExists("MyObject"))
               Project.Variables.RemoveVariable("MyObject") 
            Project.Variables.AddVariable("MyObject", "Object");
            Project.Variables.MyObject = Sys.OleObject("Excel.Application")
        }
        
        //Accessing the Project Variable in different script Routine
        function testingprojectVariable()
        {
          excelApp = Project.Variables.VariableByName("MyObject")
          excelApp.Workbooks.Open("C:\\Users\\sathishkumark\\Desktop\\Source.xlsx")
          excelApp.DisplayAlerts = true
        ////Your Excel Operations...
        .............. }