How to handle synchronization in UFT/QTP
In any test automation framework, browser handling is the most important part during automation scripting. Let’s see how to handle synchronization in UFT. which you can encounter during your work with test automation using UFT.
A robust Synchronization mechanism plays a vital role in making your test stable. If part of your tests fails due to a bad wait function between two clicks, then it is high time to revisit your wait function and make it more error-proof.
Here in the below-written code, we have two assumptions.
- The application is a normal web-based application with a hierarchy as Browser>Page etc.
- The browser returns ReadyState= complete or 4 once it is fully loaded.
It checks 2 things in the same order as given below.
- First, it checks if a page is existing within the first 120 seconds.
- Second, it waits for the browser to return ReadyState=complete/4
Example code: How to handle synchronization in UFT
Function Wait_Sync()
nBrowser_Count = Browser_Count
If nBrowser_Count > 0 Then
waitCounter = 0
intStartTime = 0
Do
wait 1
waitCounter = waitCounter + 1
If (waitCounter = 120) Then
Exit Do
End If
bPageExistFlag = Browser("CreationTime:=" & nBrowser_Count - 1).Page("title:=.*").Exist(0)
Loop WhilebPageExistFlag = False
Do
On Error Resume Next
Wait 0,500
waitCounter = 0
If bPageExistFlag = True Then
ReadyState_Val = Browser("CreationTime:=" & nBrowser_Count - 1).Page("index:=0").Object.ReadyState
Else
ReadyState_Val = "Loading"
End If
sReadyState_Val = CStr(ReadyState_Val)
If intStartTime = 600 Then
'Print intStartTime
Exit Do
End If
intStartTime = intStartTime + 1
iFinalVal = Eval(InStr(sReadyState_Val,4) > 0 Or InStr(sReadyState_Val,"complete") > 0)
Loop UntiliFinalVal = True
End If
End Function