cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Community Manager

[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:Star_Gold.pngStar_Gold.pngStar_gray.pngStar_gray.png

 

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!


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Leader

Passing custom command-line arguments as test parameter

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 :

image.png

 

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.

Un sourire et ça repart

View solution in original post

Tags (1)
7 REPLIES 7
Highlighted
Staff

Re: Your Input Needed - Passing custom command-line arguments as test parameter

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.

image.png

image.png

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!

Best,
Justin Kim
Highlighted
Community Manager

Re: Your Input Needed - Passing custom command-line arguments as test parameter

Thank you for the suggestion @hkim5 ! 

Let's see if community members have ideas in this regard.

 

How would you go about this task?

@tristaanogre @BenoitB @Marsha_R @cunderw @Reshail @AlexKaras  


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Community Leader

Passing custom command-line arguments as test parameter

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 :

image.png

 

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.

Un sourire et ça repart

View solution in original post

Tags (1)
Highlighted
Community Manager

Re: Your Input Needed - Passing custom command-line arguments as test parameter

Thank you for the JavaScript examples Benoit!

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


Sonya Mihaljova
Community and Education Specialist

Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Frequent Contributor

Re: Your Input Needed - Passing custom command-line arguments as test parameter

 

 

 

 

 

 

JamesK
Highlighted
Community Leader

Re: Your Input Needed - Passing custom command-line arguments as test parameter

It depends of your project/framework logic.

In my own fmwk i have a setup file context.js for every project, which is the place of all context initialization and context teardown.

 

For you, you can simply :

- put all the code given here in one file, CmdParams.js for example,

- add his file to your project

- and in the Project test item management tab add a call of BuildCmdParams on a new test item in the first line.

 

If you want to call the GetCmdParams in other unit, just add //USEUNIT CmdParams on first line of the unit which need to call it.

 

 

Un sourire et ça repart

Highlighted
Frequent Contributor

Re: Your Input Needed - Passing custom command-line arguments as test parameter

Thanks

JamesK
New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Compare images using the Region Checkpoint

See replies!

Converting UTC TimeDate in an Excel file

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors