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.
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:
pageMain
, formMain
, and so on).To bind to the linkLogin
element, we will use the
WaitAliasChild
method:
var br = Aliases.browser; br.ToUrl("http://smartbear.com"); var page = br.pageMain; var form = page.formMain; var login = form.WaitAliasChild("linkLogin", 5000);
if(!login.Exists) { Log.Error("Login link didn't appear on the page"); }
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.
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.
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