Interacting With Console Windows

Applies to TestComplete 14.70, last modified on April 22, 2021

TestComplete allows testing different kinds of applications: Windows-based, web, Flex and others created with different compilers and IDEs. Also, you can easily test console applications using TestComplete. For more information on the general approach used for testing of such applications, see Testing Console Applications - Overview.

Simulating Keystrokes

Like windows of other applications, the console windows contain specific methods and properties that let you simulate user actions over them. You can simulate keystrokes sent to these windows by using the Keys method. The following code demonstrates the use of this method.

JavaScript, JScript

var p, w;
  
p = Sys.Process("MyApp");
w = p.Window("ConsoleWindowClass", "*");
w.Keys("MyString [Enter]");
// ...

Python

p = Sys.Process("MyApp");
w = p.Window("ConsoleWindowClass", "*");
w.Keys("MyString [Enter]");
# ...

VBScript

Set p = Sys.Process("MyApp")
Set w = p.Window("ConsoleWindowClass", "*")
Call w.Keys("MyString [Enter]")
' ...

DelphiScript

var
  p, w : OleVariant;
begin
  p := Sys.Process('MyApp');
  w := p.Window('ConsoleWindowClass', '*');
  w.Keys('MyString [Enter]');
// ...
end;

C++Script, C#Script

var p, w;
  
p = Sys["Process"]("MyApp");
w = p["Window"]("ConsoleWindowClass", "*");
w.Keys("MyString [Enter]");
// ...

For more information, see also Simulating Keystrokes.

Obtaining Text of Console Windows

To obtain the text of the console window, use the wText property of the Window object that correspond to this console window. wText is a specific property that TestComplete adds to console applications’ windows. The property returns the text of the entire console window. This text is multi-line. To retrieve individual lines from it, you can use the GetListItem and GetListLength methods of the aqString object:

JavaScript, JScript

function MyTest()
{
  var p, w, txt, cnt, i, s;

  p = Sys.Process("MyApp");
  w = p.Window("ConsoleWindowClass", "*");

  w.Keys("MyString [Enter]");

  // Obtain the window test
  txt = w.wText;
  // Specify the separator
  aqString.ListSeparator = "\r\n";
  // Obtain the list's length
  cnt = aqString.GetListLength(txt);

  for (i = 0; i < cnt; i++)
  {
    // Obtain a line
    s = aqString.GetListItem(txt, i);
    // Post the line to the test log
    Log.Message(s);
  }
}

Python

def MyTest():

  p = Sys.Process("MyApp");
  w = p.Window("ConsoleWindowClass", "*");

  w.Keys("MyString [Enter]");

  # Obtain the window test
  txt = w.wText;
  # Specify the separator
  aqString.ListSeparator = "\r\n";
  # Obtain the list's length
  cnt = aqString.GetListLength(txt);

  for i in range (0, cnt):
    # Obtain a line
    s = aqString.GetListItem(txt, i);
    # Post the line to the test log 
    Log.Message(s);

VBScript

Sub MyTest
  Dim p, w, txt, cnt, i, s

  Set p = Sys.Process("MyApp")
  Set w = p.Window("ConsoleWindowClass", "*")

  Call w.Keys("MyString [Enter]")

  ' Obtain the window test
  txt = w.wText
  ' Specify the separator
  aqString.ListSeparator = vbNewLine
  ' Obtain the list's length
  cnt = aqString.GetListLength(txt)

  For i = 0 To cnt - 1
    ' Obtain a line
    s = aqString.GetListItem(txt, i)
    ' Post the line to the test log
    Call Log.Message(s)
  Next
End Sub

DelphiScript

procedure MyTest;
var
  p, w, txt, cnt, i, s;
begin
  p := Sys.Process('MyApp');
  w := p.Window('ConsoleWindowClass', '*');

  w.Keys('MyString [Enter]');

  // Obtain the window test
  txt := w.wText;
  // Specify the separator
  aqString.ListSeparator := #13#10;
  // Obtain the list's length
  cnt := aqString.GetListLength(txt);

  for i := 0 to cnt - 1 do
  begin
    // Obtain a line
    s := aqString.GetListItem(txt, i);
    // Post the line to the test log
    Log.Message(s);
  end;
end;

C++Script, C#Script

function MyTest()
{
  var p, w, txt, cnt, i, s;

  p = Sys["Process"]("MyApp");
  w = p["Window"]("ConsoleWindowClass", "*");

  w["Keys"]("MyString [Enter]");

  // Obtain the window test
  txt = w["wText"];
  // Specify the separator
  aqString["ListSeparator"] = "\r\n";
  // Obtain the list's length
  cnt = aqString["GetListLength"](txt);

  for (i = 0; i < cnt; i++)
  {
    // Obtain a line
    s = aqString["GetListItem"](txt, i);
    // Post the line to the test log
    Log["Message"](s);
  }
}

Checking the Completion Status

Also, it is a typical situation when you do not need to obtain the whole text of the console window, but you need to check only the completion status generated by the console application. In such cases, you can use the properties and methods provided by the Windows WshShell object.

The code below demonstrates how you can ping the desired web server via cmd.exe and then check the status of the ensuing result.

JavaScript, JScript

function CheckingResult()
{
  // Ping the server
  var Result = WshShell.Run("ping Server_name.com", 1, true);

  // Check the result
  if (Result == 0)
    Log.Message("Connection succeeded")
  else
    Log.Error("Server is unavailable");
}

Python

def CheckingResult():
  # Ping the server
  Result = WshShell.Run("ping Server_name.com", 1, True);

  # Check the result
  if (Result == 0):
    Log.Message("Connection succeeded");
  else:
    Log.Error("Server is unavailable");

VBScript

Sub CheckingResult

  ' Ping the server
  Result = WshShell.Run("ping Server_name.com", 1, true)

  ' Check the result
  if Result = 0 then
    Log.Message("Connection succeeded")
  else
    Log.Error("Server is unavailable")
  end if
End Sub

DelphiScript

function CheckingResult();
var
  Res;
begin

  // Ping the server
  Res := WshShell.Run('ping Server_name.com', 1, true);

  // Check the result
  if (Res = 0) then
    Log.Message('Connection succeeded')
  else
    Log.Error('Server is unavailable')

end;

C++Script, C#Script

function CheckingResult()
{
  // Ping the server
  var Result = WshShell["Run"]("ping Server_name.com", 1, true);

  // Check the result
  if (Result == 0)
    Log["Message"]("Connection succeeded")
  else
    Log["Error"]("Server is unavailable");
}

See Also

Keys Action
Testing Console Applications - Overview
Working With Console StdIn and StdOut Streams

Highlight search results