Applies to TestComplete 14.40, last modified on June 16, 2020

TestComplete provides several approaches that you can use to interact with console window output. For instance, you can use the wText property of the corresponding Window object (see Interacting With Console Windows). However, the wText property implements single access to a console window’s text.

When working with console applications, you may need to work with them in interactive mode, that is send commands to the console and receive responses from them. This implies working with standard input and output streams that are associated with the console window. You can do this by using the Windows WshShell object. It provides scripting access to the native Windows shell. WshShell object’s Exec method runs an application in a child command-shell and returns the WshScriptExec object. Its StdIn, StdOut, and StdErr properties provide scripting access to StdIn, StdOut, and StdErr streams of the tested application.

The example below demonstrates how you can interact with a console application (cmd.exe) from a test. It sends several commands to the application and reads the output:

JavaScript, JScript

function Main()
{
  var WshShellExecObj = WshShell.Exec("cmd.exe");

  // Flush the stream
  var out = readTillChar(WshShellExecObj, ">");
  Log.Message(out);

  // Send the "ver" command
  sendCommand(WshShellExecObj, "ver");
  out = readTillChar(WshShellExecObj, ">");
  Log.Message("See the ver command output in the Details panel of the test log", out);

  // Send the "ping" command
  var server_name = "www.smartbear.com";
  sendCommand(WshShellExecObj, "ping " + server_name);
  out = readTillChar(WshShellExecObj, ">");
  Log.Message("See the ping command output in the Details panel of the test log", out);

  // Send the "ipconfig" command
  sendCommand(WshShellExecObj, "ipconfig");
  out = readTillChar(WshShellExecObj, ">");
  Log.Message("See the ipconfig output in the Details panel of the test log", out);

}

// Read the console output stream
function readTillChar(WshShellExecObj, endChar)
{
  var out = "";
  var curChar;
  while (!WshShellExecObj.StdOut.AtEndOfStream)
  {
    curChar = WshShellExecObj.StdOut.Read(1);
    out += curChar;
    if (curChar == endChar) break;
  }
  return out;
}

// Send a command to the console input stream
function sendCommand(WshShellExecObj, command)
{
  if (WshShellExecObj != null)
  {
    WshShellExecObj.StdIn.Write(command + "\n");
  }
}

Python

def Main():
  WshShellExecObj = WshShell.Exec("cmd.exe");

  # Flush the stream 
  out = readTillChar(WshShellExecObj, ">")
  Log.Message(out)
  
  # Send the "ver" command and the new line character
  sendCommand(WshShellExecObj, "ver")
  out = readTillChar(WshShellExecObj, ">")
  Log.Message("See the ver command output in the Details panel of the test log", out)
  
  # Send the "ping" command and the new line character
  server_name = "www.smartbear.com"
  sendCommand(WshShellExecObj, "ping " + server_name)
  out = readTillChar(WshShellExecObj, ">")
  Log.Message("See the ping command output in the Details panel of the test log", out)
  
  # Send the "ipconfig" command and the new line character
  sendCommand(WshShellExecObj, "ipconfig")
  out = readTillChar(WshShellExecObj, ">")
  Log.Message("See the ipconfig output in the Details panel of the test log", out)
  

# Read the console output stream
def readTillChar(WshShellExecObj, endChar):
  out = ""
  while not WshShellExecObj.StdOut.AtEndOfStream:
    curChar = WshShellExecObj.StdOut.Read(1)
    out = out + curChar
    if (curChar == endChar):
      return out
      
# Send a command to the console input stream
def sendCommand(WshShellExecObj, command):
  if WshShellExecObj != None:
    WshShellExecObj.StdIn.Write(command + "\n")

VBScript

Sub Main
  Dim WshShellExecObj, out

  Set WshShellExecObj = WshShell.Exec("cmd.exe")

  ' Flush the stream
  out = readTillChar(WshShellExecObj, ">")
  Log.Message(out)

  ' Send the "ver" command
  Call sendCommand(WshShellExecObj, "ver")
  out = readTillChar(WshShellExecObj, ">")
  Call Log.Message("See the ver command output in the Details panel of the test log", out)

  ' Send the "ping" command
  server_name = "www.smartbear.com"
  Call sendCommand(WshShellExecObj, "ping " & server_name)
  out = readTillChar(WshShellExecObj, ">")
  Call Log.Message("See the ping command output in the Details panel of the test log", out)

  ' Send the "ipconfig" command
  Call sendCommand(WshShellExecObj, "ipconfig")
  out = readTillChar(WshShellExecObj, ">")
  Call Log.Message("See the ipconfig command output in the Details panel of the test log", out)

End Sub

' Read the console output stream
Function readTillChar(WshShellExecObj, endChar)
  Dim out, curChar

  Do While Not WshShellExecObj.StdOut.AtEndOfStream
    curChar = WshShellExecObj.StdOut.Read(1)
    out = out + curChar
    If curChar = endChar Then
      readTillChar = out
      Exit Function
    End If
  Loop
End Function

' Send a command to the console input stream
Sub sendCommand(WshShellExecObj, command)
  If Not WshShellExecObj Is Nothing Then
    WshShellExecObj.StdIn.Write(command & VbCrLf)
  End If
End Sub

DelphiScript

function readTillChar(WshShellExecObj, endChar); forward;
function sendCommand(WshShellExecObj, command); forward;

procedure Main;
var WshShellExecObj, outs, server_name;
begin
  WshShellExecObj := WshShell.Exec('cmd.exe');

  // Flush the stream
  outs := readTillChar(WshShellExecObj, '>');
  Log.Message(outs);

  // Send the "ver" command
  sendCommand(WshShellExecObj, 'ver');
  outs := readTillChar(WshShellExecObj, '>');
  Log.Message('See the ver command output in the Details panel of the test log', outs);

  // Send the "ping" command
  server_name := 'www.smartbear.com';
  sendCommand(WshShellExecObj, 'ping ' + server_name);
  outs := readTillChar(WshShellExecObj, '>');
  Log.Message('See the ping command output in the Details panel of the test log', outs);

  // Send the "ipconfig" command
  sendCommand(WshShellExecObj, 'ipconfig');
  outs := readTillChar(WshShellExecObj, '>');
  Log.Message('See the ipconfig command output in the Details panel of the test log', outs);

end;

// Read the console output stream
function readTillChar(WshShellExecObj, endChar);
var
  outs, curChar: OleVariant;
begin
  while not WshShellExecObj.StdOut.AtEndOfStream do
  begin
    curChar := WshShellExecObj.StdOut.Read(1);
    outs := outs + curChar;
    if curChar = endChar then
    begin
      Result := outs;
      break;
    end;
  end;
end;

// Send a command to the console input stream
procedure sendCommand(WshShellExecObj, command);
begin
  if WshShellExecObj <> nil then
    WshShellExecObj.StdIn.Write(command + #13#10);
end;

C#Script

function Main()
{
  var WshShellExecObj = WshShell["Exec"]("cmd.exe");

  // Flush the stream
  var out = readTillChar(WshShellExecObj, ">");
  Log.Message(out);

  // Send the "ver" command
  sendCommand(WshShellExecObj, "ver");
  out = readTillChar(WshShellExecObj, ">");
  Log.Message("See the ver command output in the Details panel of the test log", out);

  // Send the "ping" command
  var server_name = "www.smartbear.com";
  sendCommand(WshShellExecObj, "ping " + server_name);
  out = readTillChar(WshShellExecObj, ">");
  Log.Message("See the ping command output in the Details panel of the test log", out);

  // Send the "ipconfig" command
  sendCommand(WshShellExecObj, "ipconfig");
  out = readTillChar(WshShellExecObj, ">");
  Log.Message("See the ipconfig output in the Details panel of the test log", out);

}

// Read the console output stream
function readTillChar(WshShellExecObj, endChar)
{
  var out = "";
  var curChar;
  while (!WshShellExecObj["StdOut"]["AtEndOfStream"])
  {
    curChar = WshShellExecObj["StdOut"]["Read"](1);
    out += curChar;
    if (curChar == endChar) break;
  }
  return out;
}

// Send a command to the console input stream
function sendCommand(WshShellExecObj, command)
{
  if (WshShellExecObj != null)
  {
    WshShellExecObj["StdIn"]["Write"](command + "\n");
  }
}

See Also

Testing Console Applications - Overview
Interacting With Console Windows

Highlight search results