Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago

[TechCorner Challenge #1] Passing custom command-line arguments as test parameter

Hi Community!

 

This resource we are creating together has gained so much value over the years. Tons of helpful content can be found in the TestComplete Community thanks to all of you!

 

I propose that we take it to the next level by adding even more useful things to the TechCorner tag - a tag where we keep your tech articles, script examples, use cases, etc.

 

Here's your chance to share expertise and contribute.

 

Task: Write a script that will take the value of a custom argument from the TC command line and use this value to run a parameterized test.

 

Difficulty:

 

For example, imagine that you have a web test that takes the name of the browser as a parameter. The script should check the command line TC was launched with, find the /BrowserName parameter, read its value, and pass it to the web test as a parameter. If the command line contains /BrowserName=chrome, the web test should launch Chrome; for /BrowserName=firefox the web test should launch Firefox, etc.

 

Good luck!

  • Task: Write a script that will take the value of a custom argument from the TC command line and use this value to run a parameterized test.

     

    This is a solution created for [TechCorner Challenge #1]

     

    My  way ..

     

     

     

    // The object we'll populate
    var cmdRun = {
       exec   : "",
       params : [],
       count  : 0
    };
    
    function BuildCmdParams() {
       // Get executable name, which is always ParamStr(0), a so old MS-DOS rule ...
       cmdRun.exec = BuiltIn.ParamStr(0);
       // Beware, the ParamCount() method returns 0 if no additional parameter and btw ignore the real ParamStr(0) !
       // But if parameter(s) exists you should loop to one more because in this case the param 0 exists ... quite strange yes ..
       cmdRun.count = BuiltIn.ParamCount();
       Log.Message("The executable file is '" + cmdRun.exec + "' and it has " + cmdRun.count.toString() + " parameter" + cmdRun.count == 0 ? "" : "s");
       if (cmdRun.count != 0) {
          cmdRun.count++;
          for (let i = 0; i < cmdRun.count; i++) {
             cmdRun.params[i] = BuiltIn.ParamStr(i);
             Log.Message("Param " + i.toString() + " = '" + cmdRun.params[i] + "'");
          }   
       }   
    }
    
    function GetCmdParamsValue(ParamName = "") {
       result = "";
       if ((ParamName == "") || (cmdRun.count == 0))
          return result;
       for (let i = 0; i < cmdRun.count; i++)
          // To avoid the false positive if a param name is also a param value somewhere, check that the param name is well at beginning, ignoring the / and ignoring also character case
          if (aqString.Find(cmdRun.params[i], ParamName, 0, false) == 1) {
             // Here you can use a aqString.Find of the separator character or a split .. upon your choice. For the example, i use a split and assuming separator is =, so index 0 is name of parameter, index 1 is value of parameter
             result = cmdRun.params[i].split("=")[1];
             break;
          }
       return result;
    }
    
    function TestIt() {
       BuildCmdParams();
       Log.Message("The value of BrowserToUse is " + GetCmdParamsValue("BrowserToUse"));
    }

     

     

     

    The cmd line is :

    TestComplete.exe "C:\Users\Public\Documents\TestComplete\Apex\Apex.pjs" /r /p:DataFeed /BROWSERTOUSE=Firefox /ns /e

     

    The result is :

     

    The production code without comments/log :

     

     

    var cmdRun = {
       exec   : "",
       params : [],
       count  : 0
    };
    
    function BuildCmdParams() {
       cmdRun.exec  = BuiltIn.ParamStr(0);
       cmdRun.count = BuiltIn.ParamCount();
       if (cmdRun.count != 0) {
          cmdRun.count++;
          for (let i = 0; i < cmdRun.count; i++) 
             cmdRun.params[i] = BuiltIn.ParamStr(i);
       }   
    }
    
    function GetCmdParamsValue(ParamName = "") {
       result = "";
       if ((ParamName == "") || (cmdRun.count == 0))
          return result;
       for (let i = 0; i < cmdRun.count; i++)
          if (aqString.Find(cmdRun.params[i], ParamName, 0, false) == 1) {
             result = cmdRun.params[i].split("=")[1];
             break;
          }
       return result;
    }
    

     

     

     

    You can improve it into putting it all in object style notation.

  • As of right now (as far as I am aware, so please feel free to add on to my input), this can be done using the aqEnvironment.GetEnvironmentVariable() method.

    You would parameterize these variables to define whatever test parameter it is we want to change. Whether it be browser type, browser url, etc.

    and then your batch file or your set of command line arguments would look like:

    cd "C:\Program Files (x86)\SmartBear\TestComplete 14\Bin 
    set myText=textmessage
    TestComplete.exe "C:\Users\justin.kim\Desktop\TC_practice\TestProject1\TestProject1.pjs" /r /p:Demo_Project1 /t:"KeywordTests|env_URL" /e /ExportSummary:"C:\myresults\results.xml"

    where "set myText=textmessage" is defining that "command line parameter" for our test case that is being accessed through the aqEnvironment.GetEnvironmentVariable() method. 

    ----------------------

    so needless to say, if someone has a faster way of doing this, I'd love to hear some thoughts!

    • BenoitB's avatar
      BenoitB
      Community Hero

      Task: Write a script that will take the value of a custom argument from the TC command line and use this value to run a parameterized test.

       

      This is a solution created for [TechCorner Challenge #1]

       

      My  way ..

       

       

       

      // The object we'll populate
      var cmdRun = {
         exec   : "",
         params : [],
         count  : 0
      };
      
      function BuildCmdParams() {
         // Get executable name, which is always ParamStr(0), a so old MS-DOS rule ...
         cmdRun.exec = BuiltIn.ParamStr(0);
         // Beware, the ParamCount() method returns 0 if no additional parameter and btw ignore the real ParamStr(0) !
         // But if parameter(s) exists you should loop to one more because in this case the param 0 exists ... quite strange yes ..
         cmdRun.count = BuiltIn.ParamCount();
         Log.Message("The executable file is '" + cmdRun.exec + "' and it has " + cmdRun.count.toString() + " parameter" + cmdRun.count == 0 ? "" : "s");
         if (cmdRun.count != 0) {
            cmdRun.count++;
            for (let i = 0; i < cmdRun.count; i++) {
               cmdRun.params[i] = BuiltIn.ParamStr(i);
               Log.Message("Param " + i.toString() + " = '" + cmdRun.params[i] + "'");
            }   
         }   
      }
      
      function GetCmdParamsValue(ParamName = "") {
         result = "";
         if ((ParamName == "") || (cmdRun.count == 0))
            return result;
         for (let i = 0; i < cmdRun.count; i++)
            // To avoid the false positive if a param name is also a param value somewhere, check that the param name is well at beginning, ignoring the / and ignoring also character case
            if (aqString.Find(cmdRun.params[i], ParamName, 0, false) == 1) {
               // Here you can use a aqString.Find of the separator character or a split .. upon your choice. For the example, i use a split and assuming separator is =, so index 0 is name of parameter, index 1 is value of parameter
               result = cmdRun.params[i].split("=")[1];
               break;
            }
         return result;
      }
      
      function TestIt() {
         BuildCmdParams();
         Log.Message("The value of BrowserToUse is " + GetCmdParamsValue("BrowserToUse"));
      }

       

       

       

      The cmd line is :

      TestComplete.exe "C:\Users\Public\Documents\TestComplete\Apex\Apex.pjs" /r /p:DataFeed /BROWSERTOUSE=Firefox /ns /e

       

      The result is :

       

      The production code without comments/log :

       

       

      var cmdRun = {
         exec   : "",
         params : [],
         count  : 0
      };
      
      function BuildCmdParams() {
         cmdRun.exec  = BuiltIn.ParamStr(0);
         cmdRun.count = BuiltIn.ParamCount();
         if (cmdRun.count != 0) {
            cmdRun.count++;
            for (let i = 0; i < cmdRun.count; i++) 
               cmdRun.params[i] = BuiltIn.ParamStr(i);
         }   
      }
      
      function GetCmdParamsValue(ParamName = "") {
         result = "";
         if ((ParamName == "") || (cmdRun.count == 0))
            return result;
         for (let i = 0; i < cmdRun.count; i++)
            if (aqString.Find(cmdRun.params[i], ParamName, 0, false) == 1) {
               result = cmdRun.params[i].split("=")[1];
               break;
            }
         return result;
      }
      

       

       

       

      You can improve it into putting it all in object style notation.

      • sonya_m's avatar
        sonya_m
        SmartBear Alumni (Retired)

        Thank you for the JavaScript examples Benoit!

        For everyone interested - the BuiltIn object is available in all scripting languages.