- 1 Synchronization in QTP/UFT:
- 1.1 Where Synchronization is required:
- 1.2 Methods available in UFT/QTP to cope up with Synchronization
- 1.3 How to use Exist in UFT or QTP?
- 1.4 Sync Method
- 1.5 Asynchronous browser and scripting:
- 1.6 A sure-fire Approach:
- 1.7 Another approach I liked:
- 1.8 Share and Enjoy !
Synchronization in QTP/UFT:
Synchronization is an effective solution to make the tool(UFT/QTP) and the application under test to work together by coping up the execution speed differences between themselves. By this, we can add specific wait or delay time between the steps of a test.
Synchronization makes available a specified amount of time for an object to process before moving to the next step. So when we run a test or component(in BPT) our application may not always respond at the same speed as our computer does.
As the process it a little complicated with a client-server architecture. Once the request goes to the server, it processes the request and sends the result back to the client browser or client application.
Now in the client, the UFT/QTP is running at full speed. This causes the synchronization issue. In summary, the synchronization takes care between the timing issue between UFT, browser, and server.
Let’s look into a live example.
Synchronization is referred to as waiting time, in the sense, that if we have an application in which we have 3 steps for example. Let’s say we are recording the steps where the first step is to enter the information, the second step is clicking the submit button and the third step is to get the order No.
This is like our flight reservation system. Now when we click submit button it may take a few seconds to several minutes depending on the application or internet speed. After that, we can get the order no.
But when we play this in QTP/UFT it does it very fast, it does not wait for the processing of information after we click the submit button and may try to get the order no before it is getting generated which will give the error.
So, to overcome such problems we insert synchronization points. By inserting the synchronization point we are asking QTP to wait till the order no is getting generated and then retrieve the order no. It is a process of matching the speeds of both QTP and AUT in order to get proper execution and results. So Synchronization acts as a time interface between the tool(QTP or UFT) and the underlying application(UAT).
Where Synchronization is required:
During test execution, QTP/UFT gives instructions one by one with the same speed, but AUT takes less time for some operations execution and more time for some operations execution, that time we may not get proper execution and results. In order to get proper results in between QTP & AUT synchronization is required.
Below are the scenario where it is required to use synchronization:
- For client-server communications to finish.
- For a window or pop-up message to open.
- For a screen to change its component status like button gets enabled or disabled/ based on some dropdown the other dropdown values change etc.
- For the progress bar to complete 100%.
- For completing the transaction.
- For a web page to load.
So we can handle a synchronization issue by anticipated time and by synchronization of our test or component to ensure the required component and script is ready to take up the next step.
Methods available in UFT/QTP to cope up with Synchronization
There are several methods available in QTP for synchronization.
- Inserting Wait statements.
- Inserting waitproperty
- Inserting Synchronization points.
- Increasing Tool default synchronization time.
- Sync Method (Only for WEB)
- Exist Property
- Increasing the default time amount.
Inserting Wait Statements:
We can insert wait for statements in our test in order to make QTP/UFT wait until AUT completes the current operation. The wait is a hard-coded delay (wait X number of seconds). The wait should be avoided as much as possible.
The script will run faster and more reliably if we use the built-in synchronization functions of QTP /UFT (WaitProperty or Sync). This command is used to stop QTP(forcefully) for the specified time.
If we are very sure within that time frame the Application is bound to open, It is very smart to use it. The execution time may go high due to its usage.
Wait(time in seconds) wait(10)
Note: If we insert wait for statements QTP waits up to maximum time even though the operation is completed. The wait is a hardcoded sync point. Wait() method is a forcible wait.
Inserting Synchronization point:
- Place cursor in desired location
- keep tool under recording mode
- Insert menu > Synchronization point > show the object > click ok >
- select property name & Value (True)
- enter time in Milliseconds
- Click ok>Stop recording.
Note: if we insert Synchronization points, it does not wait up to maximum time, after completion of the current operations, it goes to the next step immediately.
object hierarchy.waitproperty "property name",value,time in milliseconds.
Increasing Tool default synchronization time:
- We need open Recording mode to enable this feature.
- We need to go to design | Synchronization Point
- We have to select the object for which we want to insert the sync point.
- After selecting the appropiate object we need to click on OK button.
- At this point the Add Synchronization Window pops up.
- We need to insert Property,value and TimeOut value.
File>settings>run tab>increase object synchronization time out>apply>ok
Note: If we increase QTP tool default time that can be applied for all statements in the test, but QTP does not wait up to maximum time unnecessarily, After completion of one statement execution it goes to the next statement immediately. Sync Method: (only for WEB)
Waits for the browser to complete current navigation.
Browser("Yahoo! Toolbar").Page("Yahoo! Toolbar").Sync
Selecting an appropriate Method:
Suppose in our test one or more statements only are taking more time for execution then selecting the “Inserting synchronization method” is better. Suppose in our test more statements are taking more time for execution then selecting “increasing tool time out”.The above two are local features but the wait statement is the Vbscript feature, even though some drawbacks are there in using the wait statement, it is better to use the wait statement in functions.
How to Use WaitProperty?
WaitProperty takes input as the property name, Value, and Timeout value and waits for the property to appear in the screen for the specified Timeout value.
object.WaitProperty(PropertyName, PropertyValue, [TimeOut])
(TimeOut Parameter is optional, in case it is not given it would take the default synchronization time “File –> Setting –> Run –> Object synchronization timeout”)
Set object = Browser("CreationTime:=0").Page("Index:=0").Link("text:=Accounts", "html tag:=A").WaitProperty "text","Accounts",3000 or Browser("CreationTime:=0").Page("Index:=0").Link("text:=Accounts", "html tag:=A").WaitProperty "text","Accounts",3000
WaitProperty is better than hard wait and semi dynamic in nature. The exution time may go high due to its usage.
How to use Exist in UFT or QTP?
Exist takes input as the Timeout value to sync. It waits for the specified timeout value to check if the object has appeared or not.
Set object = Browser("CreationTime:=0").Page("Index:=0").Link("text:=Accounts", "html tag:=A") If object.Exist(5) Then object.Click End If
Exist() method is also a semi forcible wait. The exution time may go high due to its usage.
Sync Method can be used only for web applications where there is always a lag between page loads.
Browser.Sync or Page. Sync waits for the navigation to complete, which means that the page has been downloaded completely and successfully.
This does not necessarily mean that all of the elements of the page have been downloaded (i.e., images, CSS, JS). It is only applicable for web applications.
Asynchronous browser and scripting:
UFT/QTP does not support asynchronous browser scripting(ajax) in a direct way.For these cases, we can use the browser’s readyState or busy property.
The syntax is as follows:
do wait(2) until ie.readyState = 4 loop busyCode= Browser("CreationTime:=0").getROProperty("busy")
However they are not that reliable.
A sure-fire Approach:
We can change the code a little bit
do wait(2) until Browser().Page().Object.Exits(2) loop
Another approach I liked:
Function WaitForObject(obj) WaitForObject = false Setting("DefaultTimeout") = 500 for i = 0 to 30 'or more If obj.Exist Then WaitForObject = true Exit For End If next Setting("DefaultTimeout") = 20000 ' or other value End Function
To my point of view the best approach is 'METHOD NAME : F_Sync 'ARGUMENTS : obj,Max_Wait,result 'DESCRIPTION : Waits for Oracle Navigator 'RETURNS : None ' Function F_Sync (obj,Max_Wait) Dim Wait_Time flag_wait = "False" Wait_Time=0 Do If obj.Exist(2) then flag_wait="True" Else Wait_Time=Wait_Time+5 If Wait_Time> Max_Wait Then Exit do End If End If loop until flag_wait = "True" End Function ----------------------------- obj will be Set obj=Browser("ABC").Page("VCV")
Instead of String, we can use boolean as well.