Checking Object State

Applies to TestComplete 15.20, last modified on January 19, 2022

This topic explains how you can check the state of an application’s object. For instructions on verifying that an object exists, see Checking Whether an Object Exists. To learn how to wait until the object state changes, see Waiting for Object State Changes.

Checking Object State in Keyword Tests

In keyword tests, you can check the current state of an application’s object using the If Object operation. It allows you to check if the object is enabled or disabled, visible or invisible and so on. Below are step-by-step instructions on configuring this operation:

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

  • Specify the object whose state you want to check, and press Next.

  • Choose the object state you want to test. The operation lets you test the following states:

    • Exists
    • Does not exist
    • Visible
    • Invisible
    • Enabled
    • Disabled
  • Click Finish to finish configuring the If Object operation.

To specify the operations to execute if the object has the specified state:

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

To specify the operations to execute if the object does not have the needed state:

  • 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 the Notepad’s window is visible and if so, types some text in it:

A keyword test with the If Object operation

As you can see, the If Object operation lets you test only limited number of object states. To perform an advanced state check, use the Property Checkpoint or If … Then operation.

Checking Object State in Scripts

To check an object’s state in script code, write an if … then statement that obtains the value of the object’s property indicating the desired state and performs the needed actions if the check succeeds or fails. Property checkpoints will generate the needed code for you automatically.

Below is an example of checking that a window is visible on screen:

JavaScript, JScript

var w = Sys.Process("Notepad").Window("Notepad");

if (w.VisibleOnScreen)
  w.Window("Edit").Keys("Test")
else
Log.Error("The Notepad window is invisible.");

Python

w = Sys.Process("notepad").Window("Notepad")
  
if (w.VisibleOnScreen):
  w.Window("Edit").Keys("Test")
else:
  Log.Error("The Notepad window is invisible")

VBScript

Dim w
Set w = Sys.Process("Notepad").Window("Notepad")

If w.VisibleOnScreen Then
  w.Window("Edit").Keys("Test")
Else
  Log.Error "The Notepad window is invisible."
End If

DelphiScript

var w;
begin
  w := Sys.Process('Notepad').Window('Notepad');

  if w.VisibleOnScreen then
    w.Window('Edit').Keys('Test')
  else
     Log.Error('The Notepad window is invisible.');
end;

C++Script, C#Script

var w = Sys["Process"]("Notepad")["Window"]("Notepad");

if (w["VisibleOnScreen"])
  w["Window"]("Edit")["Keys"]("Test")
else
   Log["Error"]("The Notepad window is invisible.");

See Also

Working With Application Objects and Controls
Common Tasks
Checking Whether an Object Exists
Waiting for Object State Changes
If Object Operation
If... Then Operation
Property Checkpoint Operation
About Property Checkpoints

Highlight search results