Forum Discussion

jpruddick's avatar
jpruddick
Occasional Contributor
5 years ago
Solved

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, and would perform operations upon the object. I've already written a working function which accomplishes the same thing by taking the object being manipulated as a parameter in the form of "Locate(Object ancestor, Variant propNames, Variant propValues, Integer depth)". However, I'd much rather be able to call the method in the form of "ancestor.Locate (propNames, propValues, depth)".

 

From what I've been able to find so far, it seems like accomplishing this would take a script extension. Is this correct, or is there some way to do this in an ordinary script unit? If a script extension is necessary, would anyone be willing to give me a few pointers on how to get started on it? I currently have no experience with that particular functionality.

 

I can post the code I currently have for the Locate function if necessary; I don't think it should be though.

 

Thanks in advance for any help you can offer!

  • 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.

2 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    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.

    • jpruddick's avatar
      jpruddick
      Occasional Contributor

      Ouch, that's unfortunate. Locate performs some of the same functions as Find, so I was hoping to have a nice readable "Parent.Find(ChildProps).Find(ChildProps)" structure to my code rather than the current confusing "Locate(Locate(Parent, ChildProps), ChildProps)" format. Still, if that's what it takes, that's what I'll do.

       

      Thank you very much for your help!