How to compare local variables with a reusable script in TestComplete?
I have created the following script that compares the contents of two project variables. Which two variables exactly can be defined through the parameters param1 and param2. This function can be called from within any keyword test since the variables are global.
function CompareTwoProjectVariables(param1, param2)
{
if (Project.Variables.VariableByName(param1) == Project.Variables.VariableByName(param2)) {
Log.message("The contents of variables " + param1 + "(" + Project.Variables.VariableByName(param1) + ")" + " and " + param2 + "(" + Project.Variables.VariableByName(param2) + ")" + " are equal.");
return true;
} else {
Log.Error("Variables " + param1 + "(" + Project.Variables.VariableByName(param1) + ")" + " and " + param2 + "(" + Project.Variables.VariableByName(param2) + ")" + " are not equal.");
return false;
}
}
The above script works perfectly for me. However I want to have a similar script that compares two local variables instead. I haven't been able to create a working script, because I can't succeed in calling the local variables correctly.
I hope someone can point me in the right direction.
Thanks
Keep your function as simple as possible. Here's a better approach,
function CompareVars(var1, var2) { // equal value and equal type if (var1 === var2) { Log.Message("Match!"); return true; } else { Log.Message("Do not match!"); return false; } } function main() { CompareVars(Project.Variables.param1, Project.Variables.param2); CompareVars(Project.Variables.VariableByName("param1"), Project.Variables.VariableByName("param2")); var str1 = "Hello" var str2 = "Hello" CompareVars(str1, str2); var int1 = 10 var int2 = 11 CompareVars(int1, int2); }
The function just compares two variables (and the type), and that's it. You just pass in the appropriate two parameters, and results will be shown as