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

  • 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"));   
      }

       

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

        The value is more important than the variable name! If the comparison fails, you can easily go to the Call Stack, which will take you directly to where the method call was made. From here, you can easily diagnose the issue.

        You could do this,

        function CompareVars(str1, var1, str2, var2)
        {
            // equal value and equal type
            if (var1 === var2) {
                Log.Message(`OK: "${str1}=${var1}" is the same as "${str2}=${var2}"`);
                return true;
            } else {
                Log.Error(`NOT OK: "${str1}=${var1}" is not the same as "${str2}=${var2}"`);
                return false;
            }
        }
        
        function test1()
        {
            CompareVars("Project.Variables.param1", 10, "Project.Variables.param2", 10);
        }

        Which outputs,

        But it's not advisable, and is considered not good practice.

  • Deef's avatar
    Deef
    Occasional Contributor

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