FindChild method fails intermittently when called from keyword test
I've following common wait function for our web application which fails 7 out of 10 times when called from keyword tests - the parameters passed to function are of string type so I'm using eval as suggested in other community posts.
function Wait_for_child_object_to_exist(parentObject, propertiesArray, valuesArray, depth)
{
var counter = 0;
Sys.Refresh();
if (typeof propertiesArray == 'string') {
propertiesArray = eval(propertiesArray);
}
if (typeof valuesArray == 'string') {
valuesArray = eval(valuesArray);
}
parentObject = eval(parentObject);
/* //printing arrays and param values
Log.Message(parentObject);
Log.Message(propertiesArray.length);
for (var i=0; i<propertiesArray.length; i+=1)
{
Log.Message(propertiesArray[i] + " ");
Log.Message(valuesArray[i] + " ");
} */
if(!parentObject.Exists) //wait for the parent object if it doesn't exists
Delay(3000);
if(parentObject.Exists){
var child_object = parentObject.FindChild(propertiesArray, valuesArray, depth, true); // This will find the child at any level
while((!child_object.Exists) && (counter < 90)){ //This will wait until child_object exists
aqUtils.Delay(500);
counter++;
Log.Message("wait for child loop counter " + counter);
}
if(child_object.Exists)
{
Log.Message("child object found in the system");
return child_object;
}
else
Log.Error("Failed to find the child object with given properties");
}
else
Log.Error("Parent object: " + parentObject+ " is missing"); //if parent object doesn't exist
}
However if I call this function as a part of another function so that parameter passing can be handled through script itself then it works consistently. But it doesn't always work if I pass the function paramters from keyword tests, it ends up with error "Failed to find the child object with given properties" even thought that object exists in the system.
Can anyone please suggest if the above FindChild approach is right? Is there anything wrong with parameter passing from keyword tests?
Hi,
> fails 7 out of 10 times
Does failure and pass occur for exactly the same data? Or just with different data the function may pass or fail ?
It is difficult to provide an answer without seen how the function is called (I am interested in exact parameters' values). I am wondering because, for example, if you search by more than one parameter, then you must pass arrays for properties and values, but if the actual paremeter is, say, 'Param1, Param2' string then it will not be resolved to the array even with the help of eval() function.
Another note:
var child_object = parentObject.FindChild(propertiesArray, valuesArray, depth, true); // This will find the child at any level while((!child_object.Exists) && (counter < 90)){ //This will wait until child_object exists ...
This code will not work as expected, because if child_object is not found by .FindChild() at the moment of search, the returned value will be an empty stub object with .Exists property equal to false. And the object will not be re-evaluated even if the sought for object appears later. So while loop is useless here.
The correct way is
either to use the .FindChildEx() method instead of the .FindChild()
or use the loop like this:
var child_object; do { child_object = parentObject.FindChild(propertiesArray, valuesArray, depth, true); if (!child_object.Exists) Delay(500); } while((!child_object.Exists) && (counter < 90));
The first approach will just wait for the object to appear within the specified timaout while the second one allows some additional actions to be done while waiting. For example, some confirmation window that must be closed before the sought for object appears can be processed with the latter approach.
P.S.
> // This will find the child at any level
Actually, not at 'any level' but at the level up to the 'depth' depth.
Incorrect commenting might appear to be even worse than absence of the such at all... :)