Checking Object State

Applies to TestComplete 15.68, last modified on October 16, 2024

In your automated tests, you often need to verify the state of your tested application. Usually, you do this by checking properties of objects in the application. For example, you can check whether the window is visible on screen or whether a web element is present on a web page or whether a control is enabled.

Note: To learn how to check whether a tested object exists, see Checking Whether an Object Exists. To learn how to wait until the object state changes, see Waiting for Object State Changes.

In keyword tests

In keyword tests, you can check the object state by using the If Object operation. It allows checking if the object exists, is enabled or disabled, or is visible or invisible. For example:

  1. Add the If Object operation to your keyword test. TestComplete will show the Operation Parameters wizard.

  2. Specify the object whose state you want to check, and click Next.

  3. Choose one of the following states to check:

    • Exists
    • Does not exist
    • Visible
    • Invisible
    • Enabled
    • Disabled
  4. Click Finish.

  5. To specify the operations to run if the object has the specified state, add the needed operations to the test as child items of the If Object operation.

  6. To specify the operations to run if the object does not have the needed state, add the Else operation right after the If Object operation, and then add the needed operations as child items of the Else operation.

Desktop

Checking the object state

Click the image to enlarge it.

Web (Cross-Platform)

Checking the object state

Click the image to enlarge it.

The If Object operation can check only a limited number of states. To perform more advanced state check, you can use property checkpoints or specify custom instructions by using the If … Then operation.

In script tests

To check an object’s state, write an if … then statement that gets the object’s property indicating the expected state. For example

Desktop

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.");

Web (Cross-Platform)

JavaScript, JScript

function Test()
{
  var url = "https://services.smartbear.com/samples/TestComplete14/smartstore/";
  Browsers.Item(btChrome).Run(url);

  var textbox = Sys.Browser().Page("*").FindElement("#instasearch");
  if (textbox.VisibleOnScreen)
  {
    textbox.Keys("ball");
  }
  else
  {
    Log.Error("The Search text box is not visible.");
  }
}

Python

def Test():
  url = "https://services.smartbear.com/samples/TestComplete14/smartstore/"
  Browsers.Item[btChrome].Run(url)

  textbox = Sys.Browser().Page("*").FindElement("#instasearch")
  if (textbox.VisibleOnScreen):
    textbox.Keys("ball")
  else:
    Log.Error("The Search text box is not visible.")

VBScript

Sub Test()
  url = "https://services.smartbear.com/samples/TestComplete14/smartstore/"
  Browsers.Item(btChrome).Run(url)

  Set textbox = Sys.Browser().Page("*").FindElement("#instasearch")
  If textbox.VisibleOnScreen Then
    textbox.Keys("ball")
  Else
    Log.Error("The Search text box is not visible.")
  End If
End Sub

DelphiScript

procedure Test();
var url, textbox;
begin
  url := 'https://services.smartbear.com/samples/TestComplete14/smartstore/';
  Browsers.Item[btChrome].Run(url);

  textbox := Sys.Browser().Page('*').FindElement('#instasearch');
  if textbox.VisibleOnScreen then
    textbox.Keys('ball')
  else
    Log.Error('The Search text box is not visible.');
end;

C++Script, C#Script

function Test()
{
  var url = "https://services.smartbear.com/samples/TestComplete14/smartstore/";
  Browsers["Item"](btChrome)["Run"](url);

  var textbox = Sys["Browser"]()["Page"]("*")["FindElement"]("#instasearch");
  if (textbox["VisibleOnScreen"])
  {
    textbox["Keys"]("ball");
  }
  else
  {
    Log["Error"]("The Search text box is not visible.");
  }
}

As an alternative, you can add a property checkpoint to your script test. See About Property Checkpoints.

See Also

Common Tasks
Checking Whether an Object Exists
Waiting for Object State Changes
About Property Checkpoints

Highlight search results