Closing Tested Applications

Applies to TestComplete 15.31, last modified on March 17, 2022

You can close a running application under test in any of the following ways:

To close a tested application from a keyword test, you can call the methods described above by using the Process Action, Call Object Method, Run Code Snippet, or Run Script Routine operation.

The code below shows how to close a tested application. The Process.Close method in the code closes one application instance, while the TestedApp.Close method closes all the running instances of the tested application.

JavaScript, JScript

function ClosingApplications()
{
var LastPad;
  //Launch three instances of Notepad
  //and obtain the third one as the LastPad process
  LastPad=TestedApps.Notepad.Run(3);
  ...
  //Close a Notepad instance
  LastPad.Close(); //The Process.Close method is called
  aqUtils.Delay(1000); // Wait until the application is closed
  ...
  //Close the two remaining instances of Notepad
  TestedApps.Notepad.Close(); //The TestedApp.Close method is called
}

Python

def ClosingApplications():
  # Launch three instances of Notepad
  # and obtain the third one as the LastPad process
  LastPad=TestedApps.notepad.Run(3)
  # ...
  # Close a Notepad instance
  LastPad.Close() # The Process.Close method is called
  aqUtils.Delay(1000) # Wait until the application is closed
  # ...
  # Close the two remaining instances of Notepad
  TestedApps.Notepad.Close() # The TestedApp.Close method is called

VBScript

Sub ClosingApplications
  'Launch three instances of Notepad
  'and obtain the third one as the LastPad process
  Set LastPad=TestedApps.Notepad.Run(3)
  ...
  'Close a Notepad instance
  LastPad.Close 'The Process.Close method is called
  aqUtils.Delay(1000) ' Wait until the application is closed
  ...
  'Close the two remaining instances of Notepad
  TestedApps.Notepad.Close 'The TestedApp.Close method is called
End Sub

DelphiScript

procedure ClosingApplications;
var LastPad: OleVariant;
begin
  //Launch three instances of Notepad
  //and obtain the third one as the LastPad process
  LastPad:=TestedApps.Notepad.Run(3);
  ...
  //Close a Notepad instance
  LastPad.Close; //The Process.Close method is called
  aqUtils.Delay(1000); // Wait until the application is closed
  ...
  //Close the two remaining instances of Notepad
  TestedApps.Notepad.Close; //The TestedApp.Close method is called
end;

C++Script, C#Script

function ClosingApplications()
{
var LastPad;
  //Launch three instances of Notepad
  //and obtain the third one as the LastPad process
  LastPad=TestedApps["notepad"]["Run"](3);
  ...
  //Close a Notepad instance
  LastPad["Close"](); //The Process.Close method is called
  aqUtils["Delay"](1000); // Wait until the application is closed
  ...
  //Close the two remaining instances of Notepad
  TestedApps["notepad"]["Close"](); //The TestedApp.Close method is called
}

TestComplete considers an application to be a tested application only if it is added to the Tested Applications collection in the current project, and you launch the application from TestComplete (either from the user interface or from a test). This prevents TestComplete from affecting applications that you launch manually (for example, via the Start | All Programs menu, a shortcut, Windows Explorer or other manager, and so on).

The following sample code shows how this works.

JavaScript, JScript

  //Suppose we have already launched Notepad with Windows Explorer
  //and Calculator has been launched by selecting the TestComplete Run Selected context menu item

function ManipulatingTestedApps()
{
  TestedApps.Notepad.Run(2);
  TestedApps.MSPaint.Run(2);
  TestedApps.Calculator.Run(2);
  //Now there are 3 instances of Notepad, 3 instances of Calculator, and 2 instances of MSPaint
  ...
  TestedApps.Notepad.Terminate();
  //Now there are 3 instances of Calculator, 2 instances of MSPaint and one instance of Notepad
  ...
  TestedApps.TerminateAll();
  //All tested applications are closed (including Calculator launched from TestComplete).
  //Only the Notepad instance launched by the user is running.
};

VBScript

  'Suppose we have already launched Notepad with Windows Explorer
  'and Calculator has been launched by selecting the TestComplete Run Selected context menu item

Sub ManipulatingTestedApps
  TestedApps.Notepad.Run(2)
  TestedApps.MSPaint.Run(2)
  TestedApps.Calculator.Run(2)
  'Now there are 3 instances of Notepad, 3 instances of Calculator, and 2 instances of MSPaint
  ...
  TestedApps.Notepad.Terminate
  'Now there are 3 instances of Calculator, 2 instances of MSPaint and one instance of Notepad
  ...
  TestedApps.TerminateAll
  'All tested applications are closed (including Calculator launched from TestComplete).
  'Only the Notepad instance launched by the user is running.
End Sub

DelphiScript

  //Suppose we have already launched Notepad with Windows Explorer
  //and Calculator has been launched by selecting the TestComplete Run Selected context menu item

procedure ManipulatingTestedApps;
begin
  TestedApps.Notepad.Run(2);
  TestedApps.MSPaint.Run(2);
  TestedApps.Calculator.Run(2);
  //Now there are 3 instances of Notepad, 3 instances of Calculator, and 2 instances of MSPaint
  ...
  TestedApps.Notepad.Terminate;
  //Now there are 3 instances of Calculator, 2 instances of MSPaint and one instance of Notepad
  ...
  TestedApps.TerminateAll;
  //All tested applications are closed (including Calculator launched from TestComplete).
  //Only the Notepad instance launched by the user is running.
end;

C++Script, C#Script

  //Suppose we have already launched Notepad with Windows Explorer
  //and Calculator has been launched by selecting the TestComplete Run Selected context menu item

function ManipulatingTestedApps()
{
  TestedApps["Notepad"]["Run"](2);
  TestedApps["MSPaint"]["Run"](2);
  TestedApps["Calculator"]["Run"](2);
  //Now there are 3 instances of Notepad, 3 instances of Calculator, and 2 instances of MSPaint
  ...
  TestedApps["Notepad"]["Terminate"]();
  //Now there are 3 instances of Calculator, 2 instances of MSPaint and one instance of Notepad
  ...
  TestedApps["TerminateAll"]();
  //All tested applications are closed (including Calculator launched from TestComplete).
  //Only the Notepad instance launched by the user is running.
};

Note: If the timeout set for the test run elapses, TestComplete can stop a project suite, project, or test item run. In this case, the test engine terminates all tested applications launched from TestComplete and then generates the OnTimeout event. See Stopping Tests on Timeout.

See Also

About Tested Applications
Running Tested Applications

Highlight search results