Ask a Question

Adding method to "testobj"

Occasional Contributor

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!

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



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


var testObj = new myWrapper()




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.

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
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!

Showing results for 
Search instead for 
Did you mean: