09-08-2010, 01:12 AM
There are several ways to do dynamic waits for AJAX calls. It all depends on what is changing on the page. For example, sometimes an object exists on the page, but is just hidden, either by having coordinates off the visible area, or by having a height and width of 0, or by having the visible property set to false. Otherwise, objects can be inserted into the DOM via an AJAX call. All of these scenarios require different code. The following examples are registered functions so that they can be called on a WebElement directly. To illustrate waiting for a specific object to have a height and width greater than 0 (thereby making the object visible):
To wait for the existence of an object:
Code:
'@Description Waits until the test object has width and height greater than zero, pass Null for timeout to accept default of 10 seconds.
Public Sub WaitForVisibility(test_object, ByVal timeout)
If IsEmpty(timeout) Or IsNull(timeout) Then timeout = 10 End If
Dim start: start = Timer
Do While (test_object.GetROProperty("width") <= 0 And test_object.GetROProperty("height") <= 0) And (Timer - start < timeout)
Wait 0, 500
test_object.Init
Loop
Wait 0, 500
End Sub
RegisterUserFunc "WebElement", "WaitForVisibility", "WaitForVisibility"
To wait for the existence of an object:
Code:
'@Description Waits until the test object exists, pass Null for timeout to accept default of 10 seconds.
Public Sub WaitForExistence(test_object, ByVal timeout)
If IsEmpty(timeout) Or IsNull(timeout) Then timeout = 10 End If
Dim start: start = Timer
Do While (Not test_object.Exist(0)) And (Timer - start < timeout)
Wait 0, 500
test_object.Init
Loop
Wait 0, 500
End Sub
RegisterUserFunc "WebElement", "WaitForExistence", "WaitForExistence"