Close Action (Process and Browser Objects)

Applies to TestComplete 15.0, last modified on November 17, 2021

Description

Closes the specified application process by sending the WM_SYSCOMMAND message with the SC_CLOSE parameter to the main window of the process. When called for multi-process web browsers, the method closes the main process of the browser as well as its auxiliary processes.

The method also checks whether the process is showing a window that asks you to save the changes. If it is, the method stops waiting for the process. In this case, it will not close it and post an informative message to the test log.

If Close fails to stop the process, use Terminate.

Declaration

TestObj.Close(WaitTimeOut)

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
WaitTimeOut [in]    Optional    Integer Default value: 60000   
Result None

Applies To

View Mode

This method is available in the Object Browser panel and in other panels and dialogs in both Basic and Advanced view modes.

Parameters

The method has the following parameter:

WaitTimeOut

Specifies a timeout, in milliseconds, to wait for the process to close. The test execution will pause for the specified time or until the process closes. When the method is called for the Browser object, it pauses the test execution for the specified time or until the main and child processes are closed.

Result Value

None.

Example

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

JavaScript, JScript

function CloseNotepad()
{
  // Obtains the notepad.exe process
  var p = Sys.Process("notepad");
  // Closes the process
  p.Close();
  // Checks whether the process is closed
  if (p.Exists)
  {
    Log.Warning(p.ProcessName + " seems to hang, terminating.");
    p.SaveDumpToLog()
    // Terminates the process
    p.Terminate();
  }
}

Python

def CloseNotepad():
  # Obtains the notepad.exe process
  p = Sys.Process("notepad")
  # Closes the process
  p.Close()
  # Checks whether the process is closed
  if p.Exists:
    Log.Warning(p.ProcessName + " seems to hang, terminating.")
    p.SaveDumpToLog()
    p.Terminate() # Terminates the process

VBScript

Sub CloseNotepad
  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
    Log.Warning p.ProcessName + " seems to hang, terminating."
    p.SaveDumpToLog
    ' Terminates the process
    p.Terminate
  End If
End Sub

DelphiScript

procedure CloseNotepad();
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
  begin
    Log.Warning(p.ProcessName + ' seems to hang, terminating.');
    p.SaveDumpToLog;
    // Terminates the process
    p.Terminate;
  end;
end;

C++Script, C#Script

function CloseNotepad()
{
  // Obtains the notepad.exe process
  var p = Sys["Process"]("notepad");
  // Closes the process
  p["Close"]();
  // Checks whether the process is closed
  if (p["Exists"])
  {
    Log["Warning"](p.ProcessName + " seems to hang, terminating.");
    p["SaveDumpToLog"]();
    // 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, the code terminates the instance.

JavaScript, JScript

function CloseAllNotepads()
{
  // 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();
    // Checks whether the process is closed
    if (p.Exists)
    {
      Log.Warning(p.ProcessName + " seems to hang, terminating.");
      // Terminates the process
      p.Terminate();
    }

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

}

Python

def CloseAllNotepads():
  # 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()
    # Checks whether the process is closed
    if p.Exists:
      Log.Warning(p.ProcessName + " seems to hang, terminating.")
      # Terminates the process
      p.Terminate()
      
    # Obtains another instance of the notepad.exe process
    p = Sys.FindChild("ProcessName", "notepad")

VBScript

Sub CloseAllNotepads
  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
    ' Checks whether the process is closed
    If p.Exists Then
      Log.Warning p.ProcessName + " seems to hang, terminating."
      ' 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 CloseAllNotepads();
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;
    // Checks whether the process is closed
    if p.Exists then
    begin
      Log.Warning(p.ProcessName + ' seems to hang, terminating.');
      // Terminates the process
      p.Terminate;
    end;

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

end;

C++Script, C#Script

function CloseAllNotepads()
{
  // 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"]();
    // Checks whether the process is closed
    if (p["Exists"])
    {
      Log["Warning"](p.ProcessName + " seems to hang, terminating.");
      // Terminates the process
      p["Terminate"]();
    }

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

}

See Also

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

Highlight search results