Monday, 1 October 2012

L6 - Synchronization

What is Synchronization?

Synchronization is to sync the actions of application and QTP execution of tests, it is quite possible that QTP needs to wait for application to perform some action before executing next step, but how QTP would know that QTP should wait or not at any specific step, to handle this situation QTP use 3 types of Synchronization.

When Synchronization is required?

Whenever QTP has to wait for some time for some action to be performed completely before moving to next action.

1. Global Synchronization

Synchronization can be set globally and for example if we set synchronization time as 10 second, it works on best time formula, as if some action(Condition) is performed in 1 second then QTP will not wait for next 9 second to perform next action, it will move to next step immediately. If the action(Condition) is not done in 10 seconds the QTP will throw error .

How to set synchronization
File -> Settings -> Run -> Object synchronization time out.
In below example 20 second is set as synchronization time out.

2. Local Constant

The other way to set local constant synchronization is to use wait function.
To make QTP wait for 10 seconds, wait(10) should be used, by using "wait", it will make sure that QTP waits for exact 10 seconds whenever QTP encounters wait in test.

It does not depend on any other condition/completion of previous step.

3. Local Variable Synchronization

We can insert synchronization point explicitly anywhere on above of the global synchronization point, in that case it will work as Global synchronization point + local synchronization point.

To demonstrate the working of local synchronization point we can take example of Flight reservation sample application.

In this application , after Click on "Insert Order" button, it process the order and process bar comes for few seconds, now to learn working of local synchronization,

 Step 1 first set File -> Settings -> Run -> Object synchronization time out = 0 which means synchronization is 0 and we have to put local synchronization at this point before executing next step.




Step 2

While processing is being done, all the objects are disabled, so we can design the synchronization point to check if the text box is enabled or not.

Click on "Recording" button -> Insert Synchronization point -> and click on the text box marked in below image (We will put synchronization at this point until it is enabled)

Press Ok


We have to set 3 fields in next image (Add Synchronization point screen).
1. Property name
2. Property value
3. Timeout (in milliseconds)


Now press Ok, a new line of code will be inserted.
Window("Flight Reservation").WinEdit("Name:").WaitProperty "enabled", True, 10000
QTP will wait to get "Edit" enabled till 10 seconds, using best time formula (If it is enabled before 10 seconds, QTP will move to next step in the test at that point of time.)

********************Complete Code********************

Window("Flight Reservation").Activate
Window("Flight Reservation").WinMenu("Menu").Select "File;New Order"
Window("Flight Reservation").ActiveX("MaskEdBox").Type "121212"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Denver"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").Activate
Window("Flight Reservation").WinEdit("Name:").Set "Manu"
Window("Flight Reservation").WinButton("Insert Order").Click
Window("Flight Reservation").WaitProperty "enabled", TRUE, 10000
Window("Flight Reservation").WinEdit("Name:").WaitProperty "enabled", True, 10000
Window("Flight Reservation").Activate
Window("Flight Reservation").WinEdit("Name:").SetSelection 0,4
Window("Flight Reservation").WinEdit("Name:").Set "Joshi"
Window("Flight Reservation").WinEdit("Name:").Type  micTab