Forum Discussion

torus's avatar
torus
Contributor
2 years ago

How do you run executable utility from the command line that requires parameters?

nothing has worked. I've tried everything but nothing is working:

 

 

function testme()
{
  Run_CommandLineExecutable("KillBrowserDrivers", "KillBrowserDrivers.exe", "chrome msedge");
}

function Run_CommandLineExecutable(folderName, executableName, arguments)
{
  var projectPath = Project.Path;
  var utilityPath = projectPath + `_UTILITIES_\\${folderName}\\${executableName}`;
 
  var fileExists = aqFile.Exists(utilityPath);
  
  utilityPath = utilityPath + ` ${arguments}`;
  
  //WshShell.Run("powershell " + utilityPath);
  
  //var shell= Sys.OleObject("WScript.Shell");
  //var result = shell.Run(utilityPath);
  var returnval = Sys.OleObject("WScript.Shell").Exec(`cmd notepad.exe`); //${utilityPath}`);

  
  var puasehere= 23;
}

 

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    A tidy up of your coding, and it does work

    function testme()
    {
        Run_CommandLineExecutable("Python", "Python.exe", "/version");
    }
    
    function Run_CommandLineExecutable(folderName, executableName, arguments)
    {
        var utilityPath = Project.Path + `Utilities\\${folderName}\\${executableName}`;
        if (aqFile.Exists(utilityPath)) {
            var command = utilityPath + ` ${arguments}`;
            WshShell.Run(`cmd /C ${command}`);
        }
    }

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    nothing has worked.

    Quite often a piece of additional information like what exactly was expected, what was the actual result and any additional related information might help with guessing about one's actual problem.

     

    Doesn't https://support.smartbear.com/testcomplete/docs/reference/program-objects/wshshell/index.html and related https://support.smartbear.com/testcomplete/docs/testing-with/advanced/using-external-functions/running-powershell-scripts.html help articles help?

     

    • torus's avatar
      torus
      Contributor

      Thanks AlexKaras for the reply. I'd been trying to figure this out for much too long. I finally tried powershell like suggested in your link (instead of continuing to try using the cmd prompt) and that worked perfectly. Below is the code that finally worked:

       

       

      function killBrowsers()
      {
        WshShell.Run("powershell C:\\autoUiTests\\KillBrowserDrivers\\KillBrowserDrivers.exe chrome msedge");
        // Below shows if you have tha name of executable in a variable:
        var utilityPath = "C:\\autoUiTests\\KillBrowserDrivers\\KillBrowserDrivers.exe chrome msedge";
        WshShell.Run("powershell " + utilityPath);
      }

       

       

      For anyone trying to find answers, make sure to put all the correct spacing into your command (space between 'powershell' and the executable path/name. And a space between the exe name and the arguments.).

       

       

      = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

      Other things I attempted that did NOT seem to work are below. Below is code that does NOT work (just for posterity's sake):

       

       

      function CodeThatDidntWork()
      {
        var utilityPath = "C:\\autoUiTests\\KillBrowserDrivers\\KillBrowserDrivers.exe chrome msedge";
      
        // attempt #1
        TestedApps.Add('cmd.exe', utilityPath, 1, true, '');
        TestedApps.cmd.Run(1, true);
      
        // attempt #2
        var shell= Sys.OleObject("WScript.Shell");
        shell.Run(utilityPath);
        
        // attempt #3
        Sys.OleObject("WScript.Shell").Exec(`cmd ${utilityPath}`);
      
      }

       

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        torus 

        Hi,

         

        OK, it's great to hear that you've got your code working, thank you for the update.

         

        Two more notes if cmd is used:

        -- It might be required to use the /C switch as already pointed-out by rraghvani ;

        -- Sometimes you must consider additional quotes for the parameters passed to cmd in order cmd properly processed them. For example, assuming that chrome and msedge are parameters for KillBrowserDrivers.exe, the following command line might be required if you try to execute the same process from the command prompt (i.e. start another instance of cmd and pass the required command to it):

        > cmd \C "KillBrowserDrivers.exe chrome msedge"

        When you move this to your code you must either enclose all command into single quotes (if this is allowed by the scripting language used in your project) or duplicate double quotes (actually - triple them). E.g. (untested, but I hope that the reason behind is clear):

        either

        WshShell.Run( 'cmd /C "KillBrowserDrivers.exe chrome msedge" ' );

        or

        WshShell.Run( "cmd /C """KillBrowserDrivers.exe chrome msedge""" " );