Forum Discussion

DenisMedvedev's avatar
DenisMedvedev
Contributor
9 years ago
Solved

How get object reference?

The bellow code works fine with system SetText(string) method

 

ODT.Data.CustomerTextField.GetObject().SetText("test")

GetObject method reterns me NameMapping.Sys.Orders.OrderForm.Group.Customer object

 

I have my own SetValue method wih bellow code

function SetValue(text){
HereShouldBeObject .Keys(text + "[Enter]")
}

The question is how to get object in SetValue method, to do bellow code works?

ODT.Data.CustomerTextField.GetObject().SetValue("test")

 

Will be glad to have any help.

Thanks in advance, Denis  

 

 

  • ghuff2's avatar
    ghuff2
    9 years ago

    You're basically asking to add user-defined functions (SetValue) onto TestComplete defined objects (Aliases.blah...). As far as I'm aware that's not possible.

7 Replies

  • You could pass the object into the function as a parameter. So it would be:

     

    function SetValue(obj, text){
    obj.Keys(text + "[Enter]")
    }
    
    //Call the function
    SetValue(ODT.Data.CustomerTextField.GetObject(), "test")

    I don't know how else you'd have your function recognize which object you want it to use. Hope this helps.

    • ghuff2's avatar
      ghuff2
      Contributor

      You could also feasibly create a wrapper class to wrap around the TestComplete Object that you want to be able to call SetValue on. I think it's overkill compared to just passing the object into the function, but here's how you could do it in python (I don't know JScript enough to translate it, but I'd assume you can do the same thing):

       

      class SetValueWrapper:
          def __init__(self, obj):
              self.obj = obj
      
          def SetValue(self, text):
              self.obj.Keys(text + '[Enter]')
              
      def test():
        wrapped_obj = SetValueWrapper(Aliases.CenterAdmin.LoginForm.pnlFlowLayout.pnlUsernameAndPassword.txtUsername)
        wrapped_obj.SetValue('text')

      Obviously if you try it on an object that doesn't have the SetText method you will get a runtime error.

  •  Hi Denis,

     

    We have a simialr process to write values in to fields in our scripts. As ghuff2 said, the best way would be to pass the object and value to write in the argument.

     

    Additionally, we use "tab" instead of "Enter", that works better for us,

     

    Object.Keys(text + "[Tab]")

     

     

     

     

    • DenisMedvedev's avatar
      DenisMedvedev
      Contributor

      yes, i agree with it, i'm using obj in signature, but i was wondering  how does it work with system SetText() method? 

       

      Currently I also use [Tab], it really better works for me too. 

       

      Thanks every one!!!

       

      • ghuff2's avatar
        ghuff2
        Contributor

        I'm not sure I understand what you're asking. Are you asking why obj can call SetText()? obj is just an object reference variable for storing the object that is returned when you call GetObject(), or whichever method you use to grab the object (namemapping, etc.).

         

        If you pass in an object to obj that doesn't have the SetText() method then it will give you a runtime error. It's probably possible to have the method figure out whether obj holds an object with the SetText() method and only then execute SetText(), but I'd suggest just making sure you only pass in objects that have the SetText() method. Or you could wrap in a try\catch block and just catch the runtime error if the object doesn't have a SetText() method.