Wait For Web Pages

Applies to TestComplete 15.47, last modified on January 20, 2023

The information in this topic applies both to cross-platform and classic web tests.

Complex dynamic web pages may take a few seconds to load, and the web page scripts can also load additional content after the page has been loaded. As a result, the web page load time may be longer during test playback than during recording. To avoid test failures due to timing issues, you can make your tests wait until a web page or element is completely loaded.

In scripts

In scripts, you can wait until a web page is loaded completely using the following approaches:

Waiting for the Page Object

The Sys.Browser().WaitPage method lets you pause the test execution until the Page object that contains the specified URL becomes accessible or until the specified period elapses.

Note: The method does not wait until a web page is loaded completely, it only waits for the Page object to become available.

You can use the Page.Exists property to determine whether a page has been loaded successfully (see below).

Using hard-coded delays

To wait until a web page is loaded completely, you can use one of the following methods:

You can specify the waiting timeout for the methods using their WaitTime parameter. The test run will be paused until the browser loads the page and becomes ready to accept user input or until the specified timeout is reached. If the parameter is omitted or equal to -1, the waiting timeout is specified by the project's Web page loading timeout option.

This works for simple pages and is acceptable in most situations. However, this may not work for complex pages, especially dynamic pages.The process may report that the page has been loaded, while some elements of the page are still being loaded or created. This happens, for instance, when the page contains frames, or script code that performs additional processing upon loading the page. To wait until a page is loaded, you can call the Delay method:

Web (Cross-Platform)

JavaScript

function useDelay()
{
  var page;

  // Run Internet Explorer and open a page
  var capabilities = {
    "platform": "Windows 10",
    "browserName": "Internet Explorer",
    "version": "11",
    "screenResolution": "1366x768"
    };
  var server = "http://hub.crossbrowsertesting.com:80/wd/hub";
  Browsers.RemoteItem(server, capabilities).Run("http://smartbear.com/");

  // Wait for 10 seconds until the page is loaded
  aqUtils.Delay(10000);

  // Get a page object
  page = Sys.Browser().Page("*");

  ...

}

JScript

function useDelay()
{
  var page;

  // Run Internet Explorer and open a page
  var capabilities = "{\"platform\": \"Windows 10\", \"browserName\": \"Internet Explorer\", \"version\": "\11\", \"screenResolution\": \"1366x768\"}";
  var server = "http://hub.crossbrowsertesting.com:80/wd/hub";
  Browsers.RemoteItem(server, capabilities).Run("http://smartbear.com/");

  // Wait for 10 seconds until the page is loaded
  aqUtils.Delay(10000);

  // Get a page object
  page = Sys.Browser().Page("*");

  ...

}

Python

def useDelay():
  # Run Internet Explorer and open a page
  capabilities = {
      "platform": "Windows 10", 
      "browserName": "Internet Explorer", 
      "version": "11", 
      "screenResolution": "1366x768"
  }
  server = "http://hub.crossbrowsertesting.com:80/wd/hub"
  Browsers.RemoteItem[server, capabilities].Run("http://smartbear.com/");

  # Wait for 10 seconds until the page is loaded
  aqUtils.Delay(10000);

  # Get a page object
  page = Sys.Browser().Page("*");

  # ...

VBScript

Sub useDelay
Dim page

  ' Run Internet Explorer and open a page
  capabilities = "{""platform"":""Windows 10"",""browserName"":""Internet Explorer"",""version"":""11"",""screenResolution"":""1366x768""}"
  server = "http://hub.crossbrowsertesting.com:80/wd/hub"
  Call Browsers.RemoteItem(server, capabilities).Run("http://smartbear.com/")

  ' Wait for 10 seconds until the page is loaded
  aqUtils.Delay 10000

  ' Get a page object
  Set page = Sys.Browser.Page("*")

  ...

End Sub

DelphiScript

procedure useDelay();
var server, capabilities, page : OleVariant;
begin

  // Run Internet Explorer and open a page
  capabilities := '{"platform":"Windows 10","browserName":"Internet Explorer","version":"11","screenResolution":"1366x768"}';
  server := 'http://hub.crossbrowsertesting.com:80/wd/hub';
  Browsers.RemoteItem[server, capabilities].Run('http://smartbear.com/');

  // Wait for 10 seconds until the page is loaded
  aqUtils.Delay(10000);

  // Get a page object
  page := Sys.Browser.Page('*');

   ...

end;

C++Script, C#Script

function useDelay()
{
  var page;

  // Run Internet Explorer and open a page
  capabilities = "{\"platform\":\"Windows 10\",\"browserName\":\"Internet Explorer\",\"version\":\"11\",\"screenResolution\":\"1366x768\"}";
  server = "http://hub.crossbrowsertesting.com:80/wd/hub";
  Browsers["RemoteItem"](server, capabilities)["Run"]("http://smartbear.com/");

  // Wait for 10 seconds until the page is loaded
  aqUtils["Delay"](10000);

  // Get a page object
  page = Sys["Browser"]()["Page"]("*");

   ...

}

Classic

JavaScript, JScript

function useDelay()
{
  var page;

  // Run Internet Explorer and open a page
  Browsers.Item(btIExplorer).Run("http://smartbear.com/");

  // Wait for 10 seconds until the page is loaded
  aqUtils.Delay(10000);

  // Get a page object
  page = Sys.Browser().Page("*");

  ...

}

Python

def useDelay():
  # Run Internet Explorer and open a page
  Browsers.Item[btIExplorer].Run("http://smartbear.com/");

  # Wait for 10 seconds until the page is loaded
  aqUtils.Delay(10000);

  # Get a page object
  page = Sys.Browser().Page("*");

  # ...

VBScript

Sub useDelay
Dim page

  ' Run Internet Explorer and open a page
  Browsers.Item(btIExplorer).Run("http://smartbear.com/")

  ' Wait for 10 seconds until the page is loaded
  aqUtils.Delay 10000

  ' Get a page object
  Set page = Sys.Browser.Page("*")

  ...

End Sub

DelphiScript

procedure useDelay();
var page : OleVariant;
begin

  // Run Internet Explorer and open a page
  Browsers.Item[btIExplorer].Run('http://smartbear.com/');

  // Wait for 10 seconds until the page is loaded
  aqUtils.Delay(10000);

  // Get a page object
  page := Sys.Browser.Page('*');

   ...

end;

C++Script, C#Script

function useDelay()
{
  var page;

  // Run Internet Explorer and open a page
  Browsers["Item"](btIExplorer)["Run"]("http://smartbear.com/");

  // Wait for 10 seconds until the page is loaded
  aqUtils["Delay"](10000);

  // Get a page object
  page = Sys["Browser"]()["Page"]("*");

   ...

}

Using hard-coded delays has a few disadvantages. For example, the test will be paused even if the page is already loaded. Also, the test might fail if the web page has not been loaded during the delay.

Waiting for an element on the page

To ensure that a web page has been loaded, you can wait until the “problematic” elements are loaded.

The general approach is to wait until the desired web page element becomes available or until it contains the correct values. For instance, if you know that the page script adds HTML tags or modifies their text, you can create a test that will wait until the element is available (or until the content is changed).

Web (Cross-Platform)

The following code example shows how to wait for an element whose href attribute includes TestComplete:

JavaScript

  let page = Sys.Browser().Page("*");

  // Waiting for the object
  do
  {
    var obj = page.WaitElement("//a[contains(@href, 'TestComplete')]", 100);
  }
  while (! obj.Exists)
  ...

JScript

  var page = Sys.Browser().Page("*");

  // Waiting for the object
  do
  {
    var obj = page.WaitElement("//a[contains(@href, 'TestComplete')]", 100);
  }
  while (! obj.Exists)
  ...

Python

page = Sys.Browser().Page("*");
# Waiting for the object
while True:
  obj = page.WaitElement("//a[contains(@href, 'TestComplete')]", 100);
  if obj.Exists:
    break;
  
...

VBScript

Dim page

   Set page = Sys.Browser.Page("*")

  ' Wait for the object
  Do
    Set obj = page.WaitElement("//a[contains(@href, 'TestComplete')]", 100)
  Loop Until obj.Exists

  ...

DelphiScript

var page, obj : OleVariant;
begin
   page := Sys.Browser.Page('*');

  // Wait for the object
  repeat
    obj := page.WaitElement('//a[contains(@href, "TestComplete")]', 100);
  until obj.Exists;
  ...
end;

C++Script, C#Script

  var page = Sys["Browser"]()["Page"]("*");

  // Waiting for the object
  do
  {
    var obj = page["WaitElement"]("//a[contains(@href, 'TestComplete')]", 100);
  }
  while (! obj["Exists"])
  ...

Classic

The following code example shows how to wait for an element whose contentText property equals TestComplete:

Note: This example is not compatible with cross-platform web testing.

JavaScript

let page;

   // Run Internet Explorer and open a page
   Browsers.Item(btIExplorer).Run("http://smartbear.com/");
   page = Sys.Browser().Page("*");

  // Waiting for the object
  do
  {
    Delay(100);
    obj = page.NativeWebObject.Find("contentText", "TestComplete");
  }
  while (! obj.Exists)
  ...

JScript

var page;

   // Run Internet Explorer and open a page
   Browsers.Item(btIExplorer).Run("http://smartbear.com/");
   page = Sys.Browser().Page("*");

  // Waiting for the object
  do
  {
    Delay(100);
    obj = page.NativeWebObject.Find("contentText", "TestComplete");
  }
  while (! obj.Exists)
  ...

Python

# Run Internet Explorer and open a page
Browsers.Item[btIExplorer].Run("http://smartbear.com/");
page = Sys.Browser().Page("*");

# Waiting for the object
while True:
  Delay(100);
  obj = page.NativeWebObject.Find("contentText", "TestComplete");
  if obj.Exists:
    break;
  
# ...

VBScript

Dim page

   ' Run Internet Explorer and open a page
   Browsers.Item(btIExplorer).Run("http://smartbear.com/")
   Set page = Sys.Browser.Page("*")

  ' Wait for the object
  Do
    Delay 100
    Set obj = page.NativeWebObject.Find("contentText", "TestComplete")
  Loop Until obj.Exists

  ...

DelphiScript

var page, obj : OleVariant;
begin
  // Run Internet Explorer and open a page
   Browsers.Item[btIExplorer].Run('http://smartbear.com/');
   page := Sys.Browser.Page('*');

  // Wait for the object
  repeat
    Delay(100);
    obj := page.NativeWebObject.Find('contentText', 'TestComplete');
  until obj.Exists;
  ...
end;

C++Script, C#Script

var page;

   // Run Internet Explorer and open a page
  Browsers["Item"](btIExplorer)["Run"]("http://smartbear.com/");
  page = Sys["Browser"]()["Page"]("*");

  // Waiting for the object
  do
  {
    Delay(100);
    obj = page["NativeWebObject"]["Find"]("contentText", "TestComplete");
  }
  while (! obj["Exists"])
  ...

In keyword tests

To open a web page from keyword tests in TestComplete, you use the Navigate operation. The operation waits until the web page is downloaded completely for the period specified by its WaitTime parameter. If the parameter is omitted, the waiting time is specified by the project's Web page loading timeout option. However, the operation may report that the page has been loaded while it is still being loaded (for example, scripts of dynamic web pages may load additional content or modify the existing content after the page has been loaded).

To work around the problem, you can specify an additional wait timeout using the Delay operation. This operation pauses your test for the specified period.

You can also call any of the methods described above from your keyword tests. To do this, use the Call Object Method or Run Code Snippet operation. For a detailed description of how to call methods from your keyword tests, see the Calling Object Methods topic.

See Also

How To
Navigate to Web Pages
Test Dynamic Web Pages (Classic Web Tests)

Highlight search results