Best UFT Synchronization method – How to handle dynamic wait in UFT/QTP using readyState value 4

In any test automation framework, browser handling is the most important part during automation scripting. 

UFT Synchronization – 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.

  1. The application is a normal web-based application with a hierarchy as Browser>Page etc.
  2. The browser returns ReadyStatecomplete or 4 once it is fully loaded.

It checks 2 things in the same order as given below.

  1. First, it checks if a page is existing within the first 120 seconds.
  2. Second, it waits for the browser to return ReadyState=complete or 4

The code will wait using a loop and will keep running unless the readyState turns 4 or complete.

Example code: UFT Synchronization – 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

You may also read

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.