TestComplete: using undeclared variables in JScript
@ Su, 19 September 2010, 21:17JScript language allows using undeclared variables. Undoubtedly, this is bad, because we will get an error message “Microsoft JScript runtime error. VAR_NAME is undefined”, if we try to get the value of this variable first. Here, VAR_NAME is the name of the undeclared variable.
However, we still can use undeclared variables in the FOR statement. In this case variable will be created and initialized automatically. There will only one problem left: this variable won’t be destroyed after the FOR statement is finished.
Let’s take this example:
function UndeclaredVariable()
{
for (i = 1; i <= 3; i++)
{
Log.Message (i);
}
Log.Message (i);
}
This function prints in the loop value of the i variable (1, 2, 3), and then prints it again (4) after the loop has ended. Everything seems to be all right.
And now imagine, that function UndeclaredVariable() calls itself recursively. For example, we should go through all tree nodes not depending on its structure and find a certain node.

This is a first version of the function:
function TreeFindNode(tree, node)
{
var nodes = tree.Nodes.Count;
for(i = 0; i < nodes; i++)
{
Log.Message("Checking item '" + tree.Nodes.Item(i).Text + "'");
if(tree.Nodes.Item(i).Text.OleValue == node)
return tree.Nodes.Item(i);
}
return null;
}
It checks only the first level of the tree and works correctly.
Now we will modify this function so that it is able to check the whole tree with all subnodes.
function TreeFindNode(tree, node)
{
var ret;
var nodes = tree.Nodes.Count;
for(i = 0; i < nodes; i++)
{
Log.Message("Checking item '" + tree.Nodes.Item(i).Text + "'");
if(tree.Nodes.Item(i).Nodes.Count > 0)
{
ret = TreeFindNode(tree.Nodes.Item(i), node);
if(ret != null)
return ret;
}
if(tree.Nodes.Item(i).Text.OleValue == node)
return tree.Nodes.Item(i);
}
return null;
}
And here is a simple example which tries to find the node Node3:
function Test11()
{
var w = Sys.Process("NETTreeView").WinFormsObject("Form1");
var tree = w.WinFormsObject("treeView1");
Log.Message(TreeFindNode(tree, "Node3") != null);
}
Let’s run it and see what we get in the log.
Checking item 'Node1'
Checking item 'Node1.1'
Checking item 'Node1.2'
Checking item 'Node1.3'
Checking item 'Node5'
As we can see, our script has simply skipped nodes Node2, Node3 and Node4. It happened because JScript engine has used the same variable i for all instances of the TreeFindNode(), instead of creating new one for each copy of the function.
It is easy to understand that there could be two possibilities. We’ve just considered the first one, where some nodes has been skipped because loop variable has been changed by another copy of the same function. And another one could happen if some copy of the function will decrease the variable to the value which has already been used. In this case the loop will work endlessly.
It is enough to declare variable i in the beginning of the function, like this:
function TreeFindNode(tree, node)
{
var ret;
var i;
.............
}
