Forum Discussion

jpruddick's avatar
jpruddick
Occasional Contributor
6 years ago

Adding method to "testobj"

I'm looking to create a method in javascript which can be called from an object variable in the same manner as common-to-all method- for example, "testobj.Find()". The method would be called Locate, ...
  • tristaanogre's avatar
    6 years ago

    I don't think it can be done, even with a script extension, at least, not simply  You're looking at creating a method on an object that is served up by TestComplete's object identification system.  These objects don't lend themself to prototyping and other tricks available in JavaScript so you can't just "attach" another method to them.   So, there's no real way to say something like

     

    var testObj = Aliases.MyApp.MyForm

    testObj.Locate()

     

    Now, what you COULD do is write a bit of code to do something like this

     

    var testObj = new myWrapper()

    testObj.Consume(Aliases.Myapp.MyForm)

    testObj.Locate()

     

    Basically, the "myWrapper" class would contain code that would go through ALL the methods and ALL the properties of the Aliased object, replicate them within the testObj instance, and then tack on your Locate method and any other methods you would want to add.  But, honestly, that feels like re-inventing the wheel.  The solution you've come up with (passing the object as a parameter) is probably the best as it is minimal extra code and doesn't end up with a confusion of whether or not you're calling the wrapped version of the object or the actual object itself.  Just seems like a lot of work for very little benefit.