Forum Discussion

Deef's avatar
Deef
Occasional Contributor
19 days ago
Solved

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

     

5 Replies

  • Deef's avatar
    Deef
    Occasional Contributor

    I get your point. Especially when it requires two extra parameters in the function.

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    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

     

    • Deef's avatar
      Deef
      Occasional Contributor

      Thank you rraghvani, this one function now works for me for both local and global variables. 

      I am now trying to get a more extended log message which states the name of the variable and the value of the variable. I succeeded with the value of the variable, but not yet with the names of the variables that are compared (as I did in my original script).

      This is my new script:

      function CompareVars(var1, var2)
      {
          // equal value and equal type
          if (var1 === var2) {
              Log.Message("OK: " + var1 + " is same as " + var2);
              return true;
          } else {
              Log.Error("NOT OK: " + var1 + " is not same as " + var2);
              return false;
          }
      }
      
      function main()
      {
          CompareVars(Project.Variables.param1, Project.Variables.param2);   
          CompareVars(Project.Variables.VariableByName("param1"), Project.Variables.VariableByName("param2"));   
      }

       

      • Hassan_Ballan's avatar
        Hassan_Ballan
        Icon for Champion Level 1 rankChampion Level 1

        I would not use the reference Project.Variables.VariableByName and stick with using Project.Variables.Var1. In TestComplete, both Project.Variables.Var1 and Project.Variables.VariableByName("Var1") are used to access project variables, but there's a key difference in how they're used and when you might prefer one over the other.

        🔹 Project.Variables.Var1
        Direct access to the variable named Var1.
        Compile-time check: If Var1 doesn't exist, you'll get an error immediately.
        Faster performance (negligible in most cases, but technically true).
        Better readability if you're working with known variable names.
        ✅ Use when you know the variable name ahead of time.

        🔹 Project.Variables.VariableByName("Var1")
        Dynamic access using a string to specify the variable name.
        Allows you to use a variable or expression to determine the name at runtime.
        More flexible, but prone to runtime errors if the name doesn't exist.
        Useful in functions that loop through or reference variables dynamically.
        ✅ Use when you don’t know the variable name at design time.

        function main()
        {
            CompareVars("Var1", Project.Variables.Var1, "Var2", Project.Variables.Var2);   
        }
        
        function CompareVars(var1name, var1value, var2name, var2value)
        {
            if (var1value === var2value) {
                Log.Message("OK: " + var1name + " = '" + var1value + "' is same as " + var2name + " = '" + var2value + "'");
                return true;
            } else {
                Log.Error("NOT OK: " + var1name + " = '" + var1value + "' is not same as " + var2name + " = '" + var2value + "'");
                return false;
            }
        }

        P.S. Remember to give a like and mark as solution.