Forum Discussion

pmahesha's avatar
pmahesha
Occasional Contributor
13 years ago

Storing Object references in global variables

Hello,

I have one issue with storing object references globally. I used a class and object to store certain parameters to test series of text boxes. All parameters are retained properly except the reference to the object. I wanted to store the reference to the

text box object inside my object variable so that I can write scripts to test all the text fields in a loop.





Here is my program:





// Step1: Define global Object for storing properties of a text box.



  var w;



  // Open the Window



  Sys.Process("java").SwingObject("ImageViewerDisplay", "Intra*", 0, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("JPanel", "", 0).SwingObject("JButton", "", 0).ClickButton();



  // Create New object ( which is Global to this file). Class "TextFieldClass" is already declared using visual method.



  PatientNumObj=ODT.Classes.New("TextFieldClass");



  // Get the reference to the text field and store it as FieldAddress



  w=Sys.Process("java").SwingObject("ct", "", -1, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("JScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("JTabbedPane", "PATIENT INFO", 0).SwingObject("JPanel", "** ADD PATIENT DETAILS **", 0).SwingObject("JTextField", "", 0);

  PatientNumObj.FieldAddress=w;

  PatientNumObj.FieldName="Patient ID";

  PatientNumObj.MinLength=1;

  PatientNumObj.MaxLength=20;

  PatientNumObj.MandatoryTrueOrFalse="TRUE";

  //PatientNumObj.ClickCoordinate="26,8";

  PatientNumObj.FieldType="NUMERIC";

  PatientNumObj.FirstChr="NUMERIC"

  PatientNumObj.Case="BOTH"







// Then I define new function in the same file at the end.





          function test()

              {

                             

              Log.Message(PatientNumObj.MaxLength);    // This prints correctly.

              Log.Message(PatientNumObj.FieldType);     //This prints the values correctly.

              var mykeys="Mahesha";



              // Key in the name

        PatientNumObj.FieldAddress.Keys(mykeys);

                           

              }





When I run, I get the results as:





The button was clicked with the left mouse button.

20

NUMERIC

Keyboard input.





Note that "Mahesha" is keyed in properly in the respective text box.









But If I close the window and reload, this does not work. We get error message





  var w;

  // Open the Window



  Sys.Process("java").SwingObject("ImageViewerDisplay", "Intra SKAN*", 0, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("JPanel", "", 0).SwingObject("JButton", "", 0).ClickButton();



  // Create New object



  PatientNumObj=ODT.Classes.New("TextFieldClass");



  // Get the reference to the text field

  w=Sys.Process("java").SwingObject("ct", "", -1, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("JScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("JTabbedPane", "PATIENT INFO", 0).SwingObject("JPanel", "** ADD PATIENT DETAILS **", 0).SwingObject("JTextField", "", 0);

  PatientNumObj.FieldAddress=w;

  PatientNumObj.FieldName="Patient ID";

  PatientNumObj.MinLength=1;

  PatientNumObj.MaxLength=20;

  PatientNumObj.MandatoryTrueOrFalse="TRUE";

  //PatientNumObj.ClickCoordinate="26,8";

  PatientNumObj.FieldType="NUMERIC";

  PatientNumObj.FirstChr="NUMERIC"

  PatientNumObj.Case="BOTH"



  // Close the window

  Sys.Process("java").SwingObject("ct", "", -1, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("MetalTitlePane", "", 0).SwingObject("JButton", "Close", 0).ClickButton();







     function test()

              {

              // Again Open the Window



            Sys.Process("java").SwingObject("ImageViewerDisplay", "Intra*", 0, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("JPanel", "", 0).SwingObject("JButton", "", 0).ClickButton();               

              Log.Message(PatientNumObj.MaxLength);

              Log.Message(PatientNumObj.FieldType);

              var mykeys="Mahesha";

        // Key in the name

              PatientNumObj.FieldAddress.Keys(mykeys);

                           

              }





Result:



The button was clicked with the left mouse button.

The button was clicked with the left mouse button.

The button was clicked with the left mouse button.

20

NuMERIC

The '' window is invisible and cannot be activated.





The last line in the result is error. Also "Mahesha" is not typed in the text field referenced by PatientNumObj.FieldAddress.





My second question:

How do I store the reference to an object without really opening the corresponding window? In the above code, we can see that window was opened just to ensure that we do not get error message for following.



w=Sys.Process("java").SwingObject("ct", "", -1, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("JScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("JTabbedPane", "PATIENT INFO", 0).SwingObject("JPanel", "** ADD PATIENT DETAILS **", 0).SwingObject("JTextField", "", 0);

  PatientNumObj.FieldAddress=w;



Is there a method to store the object reference, without opeining the respective window?



I hope someone will be able to help me in this regard,

6 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Rather than constructing complex objects and variables like that, consider investigating and using NameMapping.  It not only allows you to reference and map on screen objects to your own set of aliases, it can store a number of properties and such for your object that you can then use with the Code Completion function to populate your scripts.  Read http://smartbear.com/support/viewarticle/12456/ to find out more.



    I use this and it's a life saver.  Makes it so that, while working with my AUT, I don't have to have it open all the time to access certain properties.
  • pmahesha's avatar
    pmahesha
    Occasional Contributor
    Dear Mr. Martin,

    Thank you for the info. I am new to TC and yet to learn many of the internals.

    But I am wondering why this behavior? Once the reference is stored, how closing and opening of window affects?

    Thanks and Regards,



    Mahesha
  • Hi,



    A variable stores a reference to the target object. If the object is destroyed (you closed the window you keep in your variable), a reference to it gets broken. If you reopen the window, it is actually another object with another reference which has nothing to do with the previous one, and you need to reobtain it. As Robert said, using Name Mapping instead of global variables is a more robust approach.
  • pmahesha's avatar
    pmahesha
    Occasional Contributor
    Thank you for the good support. Let me go through Name mapping.
  • pmahesha's avatar
    pmahesha
    Occasional Contributor
    I started using the name mapping feature. Now I have new problem.

    Sometimes Objects are getting recognized only if I launch the application under test afresh for working with even child windows. If directly open a child window and run the scripts, I get the error message :The window is Invisible and cannot be activated. But if I navigate to the same child window after launching application from script itself, things work fine. So I ended up in modifying many scripts to close and reopen the application afresh.

    Is there any dependency or settings we need to make to work with name mapping without restarting the application?



    I think I am in soup now. I have scripts with mapping and without mapping (older one). Few of the scripts without name mapping does not work. Can both coexists or I will have to change all of the older scripts to consider name mapping?


  • Hi,



    This looks like a timing issue. Try waiting until the window you're having troubles with appears and becomes visible (see the Waiting for a Process or Window Activation help topic). Depending on how you address your objects ('NameMapping...obj' or 'Aliases...obj'), you need to wait using either the WaitNamedChild, or the WaitAliasChild method.