Pro-tip: More JScript vs. JavaScript -> Objects, iterations, and arrays
So, today, I went around in circles for a while looking for why my code wasn't working.
Context:
I'm building a framework utilizing a lot of Script Extension runtime objects to encapsulate as much of my code as possible. The advantage to this is that my framework is portable and there is minimal code that needs to be added to any single TestComplete project in order to execute a set of tests.
Part of the requirements for this is that we are going to be executing a series of scenarios generated as KeywordTests, each with a set of parameters. However, Script Extensions cannot call anything on the KeywordTests object. So, any interaction with KeywordTests needs to be handled in local project code.
Additionally, we chose JavaScript as the code base for the main projects. But script extensions can only be written in VBScript or JScript.
Proposed Solution:
Thanks to HKosova, I learned something new where you can pass in an array to a method and that array can be set to "explode" into the set of parameters. Additionally, because JavaScript supports either bracket or dot notation for object properties, I can do the following to run any keyword test.
var myTestName = 'TestCase1' var myParameters = ['Value1', 'Value2'] KeywordTests[myTestName].Run(...myParameters)
This works AMAZINGLY and really helped out a lot... except...
The Problem
That darn JScript. Arrays and objects defined in JScript extensions and then passed to the above JavaScript doesn't work. There are a lot of the exposed prototype methods and properties in JavaScript that JScript just doesn't expose. So, the following scenario DOES NOT work:
[JScript Extension] MyRTObject.parameterArray = ['value1'. 'value2']; MyRTObject.testName = 'TestCase1'; [JavaScript Code] KeywordTest[myRTObject.testName].Run(...MyRTObject.parameterArray) //This line blows up with stuff about things not being functions, etc.
The Actual Solution
JSON... It's how data and information is encapsulated on web pages to move stuff from place to place in a code independent fashion. So, I'm using some JSON. Now, my actual code is a lot more complex but, essentially, it looks like this:
[JScript Extension] MyRTObject.parameterArray = JSON.stringify(['value1'. 'value2']); MyRTObject.testName = 'TestCase1'; [JavaScript Code] var localArray = JSON.parse(MyRTObject.parameterArray) KeywordTest[myRTObject.testName].Run(...localArray)
This worked like a charm. My structure is actually a bit more complicated where I basically created a HashTable of objects, each representing a test case and each having a set of properties, one of which being an array of parameters. Without the JSON conversion, I couldn't do any of the fancy for(testcase in MyObjects) or the parameter "trick". But, by converting my object structure to JSON within the extension and passing it out to my JavaScript code, now it all works.
Again, just sharing something here to help those who come after me. This is a cool little trick to move stuff back and forth and allowed me to write some much more concise code.
Enjoy!