Terminate Method (Process and Browser Objects)

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

Terminates the specified application process by calling the Windows TerminateProcess function.

Declaration

TestObj.Terminate()

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
Result None

Applies To

The method is applied to the following objects:

View Mode

To view this method in the Object Browser panel and in other panels and dialogs, activate the Advanced view mode.

Result Value

None.

Remarks

When a process is closed by the Windows TerminateProcess function, destructors and other finalization code of the application are not called. So, you should use the Terminate method when Close cannot stop the application.

Example

The following sample code demonstrates how to close the notepad.exe process. If the process cannot be closed, it is terminated.

JavaScript, JScript

function Close_Notepad()
{
  // Obtains the notepad.exe process
  var p = Sys.Process("notepad");
  // Closes the process
  p.Close();
  // Checks whether the process is closed
  if (p.Exists)
    // Terminates the process
    p.Terminate();
}

Python

def Close_Notepad():
  # Obtains the notepad.exe process
  p = Sys.Process("notepad")
  # Closes the process
  p.Close()
  # Checks whether the process is closed
  if (p.Exists):
    # Terminates the process
    p.Terminate()

VBScript

Sub Close_Notepad
  Dim p
  ' Obtains the notepad.exe process
  Set p = Sys.Process("notepad")
  ' Closes the process
  p.Close
  ' Checks whether the process is closed
  If p.Exists Then
    ' Terminates the process
    p.Terminate
  End If
End Sub

DelphiScript

procedure Close_Notepad();
var p;
begin
  // Obtains the notepad.exe process
  p := Sys.Process('notepad');
  // Closes the process
  p.Close;
  // Checks whether the process is closed
  if p.Exists then
    // Terminates the process
    p.Terminate;
end;

C++Script, C#Script

function Close_Notepad()
{
  // Obtains the notepad.exe process
  var p = Sys["Process"]("notepad");
  // Closes the process
  p["Close"]();
  // Checks whether the process is closed
  if (p["Exists"])
    // Terminates the process
    p["Terminate"]();
}

The following example demonstrates how to close several instances of the notepad.exe process. If one of the instances cannot be closed, it is terminated.

JavaScript, JScript

function Close_Notepad_Instances()
{
  // Obtains the notepad.exe process
  var p = Sys.FindChild("ProcessName", "Notepad");

  // Iterates through all the instances of the notepad.exe process
  // until all of them are closed
  while (p.Exists)
  {
    p.Close();
    // Waits until the process is closed
    var isClosed = p.WaitProperty("Exists", false);
    // Checks whether the process has been closed successfully
    if (! isClosed)
      // Terminates the process
      p.Terminate();

    // Obtains another instance of the notepad.exe process
    p = Sys.FindChild("ProcessName", "Notepad");
  }

}

Python

def Close_Notepad_Instances():
  # Obtains the notepad.exe process
  p = Sys.FindChild("ProcessName", "Notepad")

  # Iterates through all the instances of the notepad.exe process
  # until all of them are closed
  while (p.Exists):
    p.Close()
    # Waits until the process is closed
    isClosed = p.WaitProperty("Exists", False)
    # Checks whether the process has been closed successfully
    if (not isClosed):
      # Terminates the process
      p.Terminate()

    # Obtains another instance of the notepad.exe process
    p = Sys.FindChild("ProcessName", "Notepad")

VBScript

Sub Close_Notepad_Instances
  Dim p, isClosed
  ' Obtains the notepad.exe process
  Set p = Sys.FindChild("ProcessName", "Notepad")

  ' Iterates through all the instances of the notepad.exe process
  ' until all of them are closed
  While p.Exists
    p.Close
    ' Waits until the process is closed
    isClosed = p.WaitProperty("Exists", False)
    ' Checks whether the process has been closed successfully
    If Not isClosed Then
      ' Terminates the process
      p.Terminate
    End If

    ' Obtains another instance of the notepad.exe process
    Set p = Sys.FindChild("ProcessName", "Notepad")
  Wend

End Sub

DelphiScript

procedure Close_Notepad_Instances();
var p, isClosed;
begin
  // Obtains the notepad.exe process
  p := Sys.FindChild('ProcessName', 'Notepad');

  // Iterates through all the instances of the notepad.exe process
  // until all of them are closed
  while p.Exists do
  begin
    p.Close;
    // Waits until the process is closed
    isClosed := p.WaitProperty('Exists', false);
    // Checks whether the process has been closed successfully
    if not isClosed then
      // Terminates the process
      p.Terminate;

    // Obtains another instance of the notepad.exe process
    p := Sys.FindChild('ProcessName', 'Notepad');
  end;

end;

C++Script, C#Script

function Close_Notepad_Instances()
{
  // Obtains the notepad.exe process
  var p = Sys["FindChild"]("ProcessName", "Notepad");

  // Iterates through all the instances of the notepad.exe process
  // until all of them are closed
  while (p["Exists"])
  {
    p["Close"]();
    // Waits until the process is closed
    var isClosed = p["WaitProperty"]("Exists", false);
    // Checks whether the process has been closed successfully
    if (! isClosed)
      // Terminates the process
      p["Terminate"]();

    // Obtains another instance of the notepad.exe process
    p = Sys["FindChild"]("ProcessName", "Notepad");
  }

}

To view more examples that demonstrate how to use the Terminate method, see Working With Tested Applications in Tests.

See Also

Close Action (Process and Browser Objects)
TestedApp.Close Method
TestedApp.Terminate Method

Highlight search results