Checking Whether an Object Exists

Applies to TestComplete 15.62, last modified on March 14, 2024

In your automated tests, before you simulate user actions over an object or validate the object state, you may need to check if the object exists in the system. Otherwise, if the test engine is not able to get the object, your test may fail.

In keyword tests

In keyword tests, use the If Object operation to verify that an object exists. For example:

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

  2. Specify the object to verify, and click Next.

  3. In the list of available states to check, choose Exists and click Finish.

  4. 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.

  5. To specify the operations to execute if the object does not exist, 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 if the object exists

Click the image to enlarge it.

Web (Cross-Platform)

Checking if the object exists

Click the image to enlarge it.

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

In script tests

  1. Get the desired object using the appropriate WaitNNN method. For example:

    • In desktop applications, use WaitProcess, WaitWindow, WaitWinFormsObject and so on.

    • In web applications, use the WaitBrowser and WaitElement methods.

    • In mobile applications, use the WaitProcess and WaitElement methods.

    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. To get the object by its name, use the WaitChild method.

  2. Use the Timeout parameter of the wait method to set the maximum time (in milliseconds) to wait for the object. Use the 0 timeout to get the object and return immediately.

  3. Check the Exists property of the resulting object.

If you get the object the ordinary way, without the Wait method, and the object does not exist, its properties and methods, including the Exists property, cannot be accessed. TestComplete will report the “Object does not exist” error if you try getting the object’s Exists property. That is why the Wait methods should be used. If the object is found, the method returns it, and the object’s Exists property is True. If the object is not found, the method creates and returns a “stub” object whose Exists property is False. Therefore, checking the Exists property will not cause the “Object does not exist” error.

Desktop

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

Web (Cross-Platform)

JavaScript, JScript

function Test()
{
  // …
  var browser = Sys.Browser();
  var page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore/");

  var textbox = page.WaitElement("#instasearch", 1000);
  if (textbox.Exists)
  {
    // The Search text box exists on the page
    // …
  }
  else
  {
    // The Search text box does not exist
    // …
  }
  // …
}

Python

def Test():
  # …
  browser = Sys.Browser()
  page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore/")

  textbox = page.WaitElement("#instasearch", 1000)
  if (textbox.Exists):
    # The Search text box exists on the page
    # …
  else:
    # The Search text box does not exist
    # …
  # …

VBScript

Sub Test()
  ' …
  Set browser = Sys.Browser()
  Set page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore/")

  Set textbox = page.WaitElement("#instasearch", 1000)
  If textbox.Exists Then
    ' The Search text box exists on the page
    ' …
  Else
    ' The Search text box does not exist
    ' …
  End If
  ' …
End Sub

DelphiScript

procedure Test();
var browser, page, textbox, searchbutton;
begin
  // …
  browser := Sys.Browser();
  page := browser.Page('*services.smartbear.com/samples/TestComplete*/smartstore/');

  textbox := page.WaitElement('#instasearch', 1000);
  if textbox.Exists then
    begin
      // The Search text box exists on the page
      // …
    end
  else
    begin
      // The Search text box does not exist
      // …
    end;
  // …
end;

C++Script, C#Script

function Test()
{
  // …
  var browser = Sys["Browser"]();
  var page = browser["Page"]("*services.smartbear.com/samples/TestComplete*/smartstore/");

  var textbox = page["WaitElement"]("#instasearch", 1000);
  if (textbox["Exists"])
  {
    // The Search text box exists on the page
    // …
  }
  else
  {
    // The Search text box does not exist
    // …
  }
  // …
}

See Also

Working With Application Objects and Controls
Common Tasks
Checking Object State

Highlight search results