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?