Forum Discussion

kumar_perlaa's avatar
kumar_perlaa
Contributor
13 years ago

TestComplete Strange Behaviour-- Object/variable is not getting global scope when i tried to access in multiple units...

TestComplete Strange Behaviour

Can you please help me out regarding below issue: 

Problem Scenario: ( All methods are  defined  in same unit)



  1. The Main function is invoking IsRefSelection() and IsAlgorithm() methods.


  2. ContractToRef,TargetExp_IsRef_Alog_Tableà These variables are declared as global and holds the address of object.


  3. When I am accessing “TargetExp_IsRef_Alog_Table” variable in IsRefSelection()method I am getting global scope and everything is working as per the business scenario, but I am not getting global scope in IsAlgorithm() method. What is causing the problem.


  4. I request you to add sample example instead of suggesting TC articles, I had already went through those. It suppose to work as per any programming language syntax.(C++,Java,C#.Net)


Code Snippet:

var ContractToRef,TargetExp_IsRef_Alog_Table;à Global Variables

var ContrToRefSel = "TRUE";

 

function ConfMGR_TTE_VolatilityUpdates(/*ContrToRefSel,isRef,Algorithm,RefExpires*/)

{

TargetExp_IsRef_Alog_Table = ContractToRef.Panel(1).Panel(0).Table("ctl00_cphMain_gvTargetExpiries")

IsRefSelection();

 IsAlgorithm();

 

}



IsRefSelection();

{

 var strText = TargetExp_IsRef_Alog_Table.Cell(0,1).innerText; // Global variables are working fine in this method

-----

switch(isRef)

    {

   

     case  "TRUE":

            var isRefObj = TargetExp_IsRef_Alog_Table.FindChild(Array("ObjectType", "RowIndex", "ColumnIndex"),Array("Cell", "1", "1"), 1);

            var isRefChild = isRefObj.FindChild("ObjectIdentifier","ctl00_cphMain_gvTargetExpiries_ctl02_chbIsRef",1);

 

--

            if (!isRefChild.checked)

            {

              isRefChild.ClickChecked(true);

              bIsRefSelectionFalg = true

              break;

            }

}



IsAlgorithm()

{

var strText = TargetExp_IsRef_Alog_Table.Cell(0,2).innerText;-à Here Iam getting String name as “XYZ”, That mean I am getting global scope of TargetExp_IsRef_Alog_Table variable/object

....

Case “Manual”:

          var TotalRows = TargetExp_IsRef_Alog_Table.RowCount;-à Getting error message here and Global variable does not holding address of the object that’s the reason I am getting rows value as null.

----

          Log.Message(Total_Rows);

           for (var i = 1; i <= TotalRows-1; i++)

{

         ---------;

-----;

}

----;

}

 


  • Hi Kumar,


     


    Based on your example, if the TargetExp_IsRef_Alog_Table variable didn't contain the object you assigned to it in the ConfMGR_TTE_VolatilityUpdates function, the test would fail. I noticed that in IsRefSelection you get one cell value:


     


    var strText = TargetExp_IsRef_Alog_Table.Cell(0,1).innerText;


     


    but in IsAlgorithm - a different cell:


     


    var strText = TargetExp_IsRef_Alog_Table.Cell(0,2).innerText


     


    I think that's expected that you get different values after executing the code above. If I misunderstand something, post the expected and actual values you want to get.

  • Hi Tanya,



    Thanks for the reply. I fixed that issue .



    Please could you  take one example(very simple) and explain how global variables works in TestComplete. I went through all TC-Global varibles articles...but failed to implement as per TC documentation.



    I request you to provide code snippet on my query.



    Here is the scenario:



    1) we have 3 methods

    2)1st Method has accessing variables a,b, c and d( all avraibles are global and holding values(1,2,3,4 )



    3) 2nd method is performing the following actions( a = a+1,b=b+1;c=C+1;d=d+1)

    3) third method is doing the  same of Step 2.#



    Kumar

  • Hi Kumar,


     


    Look into the following example:




    //JScript


    var a=1;


    var b=2;


    var c=3;


    var d=4;


     


    function test1()


    {


      a = a + 1;


      b = b + 1;


      c = c + 1;


      d = d + 1; 


    }


    function test2()


    {


      a = a + 1;


      b = b + 1;


      c = c + 1;


      d = d + 1;


    }


     


    function test3()


    {


      a = a + 1;


      b = b + 1;


      c = c + 1;


      d = d + 1;


    }


     


    function main()


    {


      Log.Message("a=" + a + "; b=" + b + "; c=" + c +"; d=" + d);


      test1();


      test2();


      test3();


      Log.Message("a=" + a + "; b=" + b + "; c=" + c +"; d=" + d);


    }




     


    The Test log contains the following:




    a=1; b=2; c=3; d=4


    a=4; b=5; c=6; d=7



  • Hello Tanya,



    Thanks for the code snippet. That's the one way to access global variables in many methods. if all variables are declared in the same file (unit) with global scope.





    Let's take  a same example and assume that Test5() defined in file 2.



    Code Snippet for Test5()



    // Test5() declared in file2(Unit 2)  and All above methods are in file1(Unit1).

      if i include file1 in file2 ,it will work as per C++ syntaxt,but JScript is not allowing Cross reference. is there any work around solution for this...Please let me know?


    #include<file1.h>

    function
    Test5()



    {



    Log.Message("a=" + a + "; b=" + b + "; c=" + c +"; d=" + d);



    a = a + 1;



    b = b + 1;



    c = c + 1;



    d = d + 1;



    Log.Message("a=" + a + "; b=" + b + "; c=" + c +"; d=" + d);



     



    }


  • if i declare  those variables at project level,i can access them in multiple methods. Let me give a try and then I will update you on the same. Thank you.