Waiting for an element to appear on a page

In the majority of cases, to determine the completion of the page download, we can use the Wait method. However, in some cases, download completion is determined by appearance of a specific element (occurring after the page download is flagged as completed).

In this recipe we will consider some methods to bind handler-functions to an event, signaling appearance of a certain webpage element. As an example, we would make use of the Login link on SmartBear main page.

Getting ready

Before we turn to waiting for the Login link to appear straight off the bat, we can make some simple preparations to simplify the task at hand:

  1. Open the main page of SmartBear company (http://smartbear.com/) in your browser (in our examples, we are using Internet Explorer).
  2. In TestComplete, open Object Browser and locate an element therein, which would correspond to the Login link (to this end, you could make use of the Object Spy tool).
  3. Right-click on this element and opt for the Map Object menu item.
    Getting ready
  4. In the opened window, click on Yes to automatically add the element with the given properties by default.
    Getting ready
  5. Open the element of the NameMapping project and locate the created linkLoginLink element therein.
  6. With the mouse, drag-and-drop the targeted element onto the element of formAspnetform. Now, the link has become a sibling element of the page.
    Getting ready
  7. We have additionally renamed the control elements by giving them succinct names for brevity and convenience's sake (pageMain, formMain, and so on).

How to do it...

To bind to the linkLogin element, we will use the WaitAliasChild method:

  1. Let's write the following code, which will be waiting for the link to appear:
    var br = Aliases.browser;
    br.ToUrl("http://smartbear.com");
    var page = br.pageMain;
    var form = page.formMain;
    var login = form.WaitAliasChild("linkLogin", 5000); 
  2. Now we will additionally check if the element is available on the page:
    if(!login.Exists)
    {
      Log.Error("Login link didn't appear on the page");
    }
  3. If you were to evoke this function again, it would transfer onto the SmartBear.com site and will wait for the Login link to appear.

How it works...

The WaitAliasChild method allows waiting for the appearance of a sibling element within the preset timeout, assigned by the second parameter of the method (in our case, we are dealing with 5000 milliseconds or 5 seconds).

This method returns the object, whose Exists method contains the information, whether the element has made it to the webpage or not. In case the element fails to show up, the Exists property will contain the False value, and we will see the error message in the log.

Such a method of handling web-pages is usually used at working with applications, where AJAX is employed, since some of the control elements may have extended latency, compared to that of the main page.

There's more...

Along with the WaitAliasChild method, there also exists a similar method of WaitNamedChild, allowing one to wait and observer the element, passing its name as a parameter from NameMapping instead of the Aliases.

See also

  • Working with the Wait methods is dealt with in greater detail in the Waiting for an object to appear recipe from Chapter 5, Accessing Windows, Controls, and Properties
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.136