Forum Discussion

RUDOLF_BOTHMA's avatar
RUDOLF_BOTHMA
Community Hero
8 years ago

Is it possible to create a temporary Alias/Name Mapping?

Good Day folks,

 

I'm trying to achieve something I am not sure can be done in a Web application.  The question is:

 

"Can I take an object I found by ID, Name, etc. and Alias it, then remove the Alias when I'm done ? "

 

This isn't the only way to do what I'm aiming for, but it would be the most convenient.  Here is how it currently works:

 

I have a web control.  As everybody knows, their ID can be all over the place.  They can also be on multiple pages or be on a page multiple times.  This causes a ridiculous amount of mappings, so I'm trying to write a script that will take a parameter for the object and a parameter for the value and set it accordingly without needing to map so many objects.  I'm throwing it into script that takes the Parent's alias then has methods for setting values.  I can extract the ID for the controls from the Parent Alias' name etc. by using a .Find() of the textbox's name, which I will now know what it will be.  Et voila, I have the textbox I would like to work with.  Problem is, this is a regular object (textbox, panel,checkbox etc.), not an Aliased object.  This is an important distinction, because I can use WaitAliasChild if is Aliased - I have a script that does some try-catches, retries and error logging that wraps the builtin WaitAliasChild - not to mention the fact that I have lots of other fancy footwork and scripts that depend on the Alias rather than the object. 

 

EDIT:

 

I have marked the correct response, but be sure to check the reply by me at the end of the thread that has a gotcha you need to be aware of when using the proposed solution

 

Any thought or suggestions would be welcomed.

  • If I understand you correctly, you would just like to be able to dynamically change a search value for a mapped object?

     

    Namemapping values can be tied to a Project Variable which you could then set the value of at run time. Does this accomplish what you need?

  • cunderw's avatar
    cunderw
    Community Hero

    If I understand you correctly, you would just like to be able to dynamically change a search value for a mapped object?

     

    Namemapping values can be tied to a Project Variable which you could then set the value of at run time. Does this accomplish what you need?

    • RUDOLF_BOTHMA's avatar
      RUDOLF_BOTHMA
      Community Hero

      Having played around with this for a while, there's one gotcha you have to be aware of when using this method.  TC caches.
      If you have a Keyword test that calls a script, which sets the project variable, followed by a second keywords test that sets the variable, it will work fine.  If you have one script that changes the variable in the middle, it wont work.  I sent details to support, so just copying and pasting:

      function SetComboText(ObjectIdentifierString, Text) {
        Project.Variables.MetaObjectIdentifier=ObjectIdentifierString;
        browser.MetaCombobox.SetText(Text);
      }
      My keyword test calls the script with the ObjectIdentifier for Combobox1 and it sets the text for ComboBox1 correctly.
      My second keyword test calls the script with ObjectIdentifier for ComboBox2 and it set the text for ComboBox2correctly.
      Now, for keyword test 3 I use the following script:
      function SetComboboxestext(Combo1Text,Combo2Text)
      {
        SetComboText(Combobox1ObjectIdentifierString,Combo1Text); //I know what the ObjectIdentifier is and it's static.  This sets the text of combobox1
        SetComboText(Combobox2ObjectIdentifierString,Combo1Text); //I also know what the ObjectIdentifier is and it's static.  This ALSO sets the text for combobox 1
      }
      In other words, if I set the project variable a second time in the same script it doesn't pick this up.  I have put a breakpoint in after setting the project variable for the second time.  If I find the actual combobox2 in object browser, it has the MappedName of browser.MetaCombobox and combobox1 has a blank as MappedName, so obviously it picked up on the change.  My script watch however tells me that Aliases.browser.MetaCombobox.ObjectIdentifier is Combobox1ObjectIdentifierString.  What happens then is that the text of Combobox1 gets set again with combo2text....


      Response from Support, which I tested and works :

      -----------------------------------------------------------------------------

      This happens because TC holds a cached reference to the first object. Since the object's state doesn't change, TC doesn't try to reobtain it and keeps using the first combo box. Going to the Object Browser refreshes the object tree and resets cached objects. Call Refresh in the SetComboText  function:

      function SetComboText(ObjectIdentifierString, Text)
      {
        Project.Variables.MetaObjectIdentifier=ObjectIdentifierString;
        Aliases.browser.Refresh();
        Aliases.browser.MetaCombobox.RefreshMappingInfo();
        Aliases.browser.MetaCombobox.SetText(Text);
      }
      -----------------------------------------------------------------------------------------------

       

    • RUDOLF_BOTHMA's avatar
      RUDOLF_BOTHMA
      Community Hero

      I'm not familiar enough about how it works yet to give you anything more than a "Maybe".
       
      Here's how I understand what you are saying:
       
      I have a pseudo script
      function SetText(AliasedObjectParent,AliasedObjectChild,Text)
      {
       AliasedObjectParent.WaitAliasChild(AliasedObjectChild.MappedName,1); //Want to know it's there first
       AliasedObjectChild.SetText(Text);
      }
       
      I have a panel with two labels/textboxes/comboboxes on.
       
      The panel is Aliased as, say, Browser.LoginPage
      I then add the first label mapped as Browser.LoginPage.TopTextBox
       
      Now if I want to set the text of Browser.LoginPage.TopTextBox I go
      SetText(Browser.LoginPage,Browser.LoginPage.TopTextBox,"Some Text");
       
      If I understand you correctly I can go something like
      Aliases.Browser.LoginPage.TopTextBox.ID = [bottomtextbox's ID] (unclear on this part, but basically change the ID to the ID of the bottom textbox)
       
      Now if I call
      SetText(Browser.LoginPage,Browser.LoginPage.TopTextBox,"Some Text");
      it will actually set the bottom textbox because I have now changed the mapping and all the properties are going to come from the bottom text box?  Not entirely sure why the project variable would come into play.  I recall reading something to that effect in help files, but it's a bit fuzzy still.

      • cunderw's avatar
        cunderw
        Community Hero

        So, the ID property of the mapped object cannot be set like that (it's read only), which is why I was mentioning using a project variable.

         

        What you would want to do is create a project variable called something like, textBoxID.

         

        Then in the name mapping for that object add the ID property and for the value change the Mode from 'Constant' to 'Project Variable' and select the newly created textBoxID.

         

        Then for your script when you want to change which object you're point to, you would do Project.Variables.texBoxID = <newID> and then call your set text.