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 pr...
  • BenoitB's avatar
    BenoitB
    5 years ago

    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.