Checking Whether an Object Exists

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

This topic explains how you can check whether an application’s process, window or object exists in the system.

Checking Whether an Object Exists From Keyword Tests

In keyword tests, use the If Object operation to verify that an object exists. Below are the steps to configure this operation:

  • Add the If Object operation to your keyword test. TestComplete will display the Operation Parameters wizard.

  • Specify the object to verify, and press Next.

  • In the list of available states to check, choose Exists and press Finish.

To specify the operations to execute if the object exists:

  • Add the needed operations to your test as child items of the If Object operation.

To specify the operations to execute if the object does not exist:

  • Add the Else operation right after the If Object operation.

  • Add the needed operations as child items of the Else operation.

The following image shows a sample keyword test that checks if Notepad is running (that is, if Notepad’s process exists in the system):

Note, that by default TestComplete waits for an object to become available for a time period specified in the Auto-wait timeout project property. You can set a custom timeout to increase or decrease that time:

  • Select Set Auto-Wait Timeout from the context menu of the If Object operation.

  • In the ensuing dialog, uncheck the Use project default Auto-wait timeout option.

  • Enter the desired timeout value (in milliseconds) in the edit box and press OK.

Checking Whether an Object Exists From Scripts

In scripts, you check whether an object exists in the following way:

  1. Address the desired object using the appropriate WaitNNN method: WaitProcess, WaitWindow, WaitWinFormsObject and so on.

    In the Timeout parameter of the wait method, specify the maximum time (in milliseconds) to wait until the object appears in the system, or 0 to check and return immediately.

    Tip: To find out which wait method to use, see what method is used to address the desired object in the Object Browser or the recorded code and add the Wait prefix to the method name.

    If the object is addressed directly by its name, use the WaitChild method.

  2. Check the Exists property of the resulting object.

Below are some examples:

JavaScript, JScript

if (Sys.WaitProcess("notepad").Exists)
  // Notepad is running
else
  // Notepad is not running

Python

if (Sys.WaitProcess("notepad").Exists):
  # Notepad is running
else:
  # Notepad is not running

VBScript

If Sys.WaitProcess("notepad").Exists Then
  ' Notepad is running
Else
  ' Notepad is not running
End If

DelphiScript

if Sys.WaitProcess('notepad').Exists then
  // Notepad is running
else
  // Notepad is not running

C++Script, C#Script

if (Sys["WaitProcess"]("notepad")["Exists"])
  // Notepad is running
else
  // Notepad is not running

JavaScript, JScript

if (Sys.Process("notepad").WaitWindow("#32770", "About Notepad", 1, 5000).Exists)
  // The About dialog is opened in Notepad
else
  // The About dialog didn't come up after 5 seconds

Python

if (Sys.Process("notepad").WaitWindow("#32770", "About Notepad", 1, 5000).Exists):
  # Notepad is running
else:
  # Notepad is not running

VBScript

If Sys.Process("notepad").WaitWindow("#32770", "About Notepad", 1, 5000).Exists Then
  ' The About dialog is opened in Notepad
Else
  ' The About dialog didn't come up after 5 seconds
End If

DelphiScript

if Sys.Process('notepad').WaitWindow('#32770', 'About Notepad', 1, 5000).Exists then
  // The About dialog is opened in Notepad
else
  // The About dialog didn't come up after 5 seconds

C++Script, C#Script

if (Sys["Process"]("notepad")["WaitWindow"]("#32770", "About Notepad", 1, 5000)["Exists"])
  // The About dialog is opened in Notepad
else
  // The About dialog didn't come up after 5 seconds

The reason for addressing the target object via the special WaitNNN method is that TestComplete accesses application objects in real-time mode. In this mode, if the object being addressed does not exist, its properties and methods (including the Exists property) cannot be accessed.

Wait methods provide a solution for this problem. They check for the object to appear within the indicated timeout. If the object is found to exist, the wait method returns that object, so that the Exists property is True. If the object does not appear within the timeout, the wait method creates and returns a special “stub” object with the Exists property equal to False. This way, the check can be successfully performed no matter whether the object exists or not.

If you address the desired object in an ordinary way, without using the WaitNNN method, you will get the “Object does not exist” error in case the object is not found. So, keep in mind that the Exists property must always be used in combination with a WaitNNN method.

See Also

Working With Application Objects and Controls
Common Tasks
Checking Object State

Highlight search results