Monday, June 24, 2013

QTP(HP UFT) Synchronization statements and methods


QTP(HP UFT) Synchronization statements and methods
Synchronization methods in QTP :
  1.      Wait statement
  2.      Exist Property
  3.      Do While ( Conditional statement )
  4.      Sync ( this method is applicable only for web based appliations)
  5.      Synchronization : (An option in QTP)
1. Wait Statement: As word says it will wait for the specified amount of time before proceed to next line of statement to execute. We can specify the time in both seconds and milliseconds. It’s necessary to define time in the wait statement even it is zero else it will give you an error.

Syntax : Wait 5 / Wait (5)

·         Invalid Example : wait, wait () both will give errors.
·         Valid Examples : while  wait(0), wait 0, wait 0, 10000 are valid statements.

Wait statement with milliseconds

·         Valid : wait 0,10000 is a valid statement.
·         Invalid : wait (0, 10000) will give an error

Disadvantage of wait statement :

The disadvantage of wait statements is that it can’t be applied on a condition. If this statement is encountered by the compiler then it will wait for the specified amount of time. If specified time amount is too long then optimization will be the problem.

2. Exist Property: A property which can used while dealing with dynamic objects which takes time to display on the web pages. As the name suggests this property checks if the object exist on AUT before doing operations on the object. This property can be applied to almost all the objects. It is used as follows:

Syntax : Exist(10)

Example : objExist=Browser("").Page("").Frame("").webedit("Login").exist(09)

The above exist statement will check if the "Login" webbutton object exists on the WebPage or not. If it exist then it return True to “objExist”: and if not then it will return False to “objExist”. In real time we generally checks if some objects exist or not before doing some operation on it.

Example :
 
If Browser("Gmail").Page("Gmail").WebButton(“Login”).Exist(10) then     Browser("Gmail").Page("Gmail").WebButton(“Login”).Click   end if 

Difference between wait and Exist Statement:
Wait Statement
Exist method
Execution will wait specifically for the amount specified in Wait statement. If wait(10) is define then the QTP will wait for 10 seconds. No matter what happens. So if we have a script which runs 100 times then naturally it is going to increase the run time significantly.
Exist: Execution will move to next statement as soon as Exist return a True. So if Exist(10) is specified and the condition become true on the very first second then QTP will move to next statement. Thus we have directly saved the 9 second.
Wait is a statement
Exist is a property
Wait pause the execution for certain amount of time
Exist checks if a specified objects exists or not for the specified time
Syntax : Wait 5 / Wait (5)
Example :
objExist =B("").P("").WebEdit("").Exist - Good 
objExist = B("").P("").WebEdit("").Exist(10) - Good
Invalid Example : wait, wait () both will give errors.
Valid Examples : while  wait(0), wait 0, wait 0, 10000 are valid statements.
Exist can't be used with the empty braces.
Exist statement with Empty braces will wait for the object synchronization time i.e. if the time is not specified in the exist statement then it will take the time from object synchronization timeout.
Wait statement with milliseconds

Valid : wait 0,10000 is a valid statement.
Invalid : wait (0, 10000) will give an error
Exist can't be used with the empty braces. It will give u error. However we can use it with the "0" in Exist
objExist =B("").P("").WebEdit("").Exist() - Error
objExist =B("").P("").WebEdit("").Exist(0) – Good
objExist =B("").P("").WebEdit("").Exist  - will wait for Object synchronization time by default 20 secs
3. Do While: For Dynamic object in AUT “Do while loop” is a best option to synchronize. Objects in AUT are visible based upon state of some other objects. For example in an application we have submit forms/submit details pages/upload attachments etc; in these cases the next page generally takes times to load, time load varies depends upon the size, number, connection speed & server response time. So we can implement a simple “Do while loop” it will instruct to QTP to wait till the next page is visible. We can specify the timeout period in the loop to,  just in case something goes wrong.

 Example:

Dim propWait
propWait =0

Browser("").Page("").Frame("").Link("").Click
Do
If Browser("").Page("").Frame("").WebTable(""").Exist(5) Then
         If trim(Browser("").Page("").Frame("").WebTable("").getcelldata(1,1))= Then 
                    Exit Do
         end if    
end if        
propWait = propWait +1   ' propWait  is used to track no of attempts made to identify object.
If propWait =5  Then
      Reporter.ReportEvent micFail,"", ""
      ExitActionIteration
elseif propWait =2 then
      Browser("").Page("").Frame("").Link("").Click  ' perform click operation on link again.
end If
Loop until Counter    > 5  
' forcibly exit in case object webtable is not found after 5 attempts

The above loop wait for the webtable to upload after clicking on a particular link. The loop keeps on checking some specific value ( Header of the webtable) till specified amount of time. If the webtable has not appeared the script click on the link again to do submit the upload webtable request again. If the webtable is not uploaded after 5 attempts then it will report the fail and skips to the next action iteration.

4 Sync Method: We can use the Browser and Page sync methods to synchronize the Browser and Page navigation

The following sync statements will wait for the browser to complete its navigation i.e. till the browser status bar displays "Done"
Browser("").sync

The following sync statements will wait for the page to download i.e. displayed. Whenever we navigate to a page the page gets download, the page sync wait till the page is displayed
Browser("").Page("").sync

5 WaitProperty/ inserting synchronization from QTP tool

Option Explicit
Dim orderNo, App_path
App_path = Environment( "ProductDir" ) & "\samples\flight\app\"   ' ProductDir is In-built environment variable
SystemUtil.Run "flight4a.exe", "", App_path ,"open"

If Dialog("Login").Exist( 2000 ) Then 'A way of synchronization
    Dialog("Login").WinEdit("Agent Name:").Set "tester"
    Dialog("Login").WinEdit("Password:").Set "Mercury"
    Dialog("Login").WinButton("OK").Click
    wait 1
Else
    Reporter.ReportEvent micFail, "Synchronization time-out", "Login dialog is not dispalyed"
    ExitTest( "your test name" )  ' it is the name of your QTP Test
End If
If Not Window("Flight Reservation").Exist( 2000 ) Then
    Reporter.ReportEvent micFail, "Synchronization time-out", "Flight Reservation window is not dispalyed"
    ExitTest( "write u r test name here" )  ' it is the name of your QTP Test
End If

    Window("Flight Reservation").WinButton("Button").Click
    Window("Flight Reservation").ActiveX("MaskEdBox").Type "010112"
    Window("Flight Reservation").WinComboBox("Fly From:").Select "London"
    Window("Flight Reservation").WinComboBox("Fly To:").Select "Los Angeles"
    Window("Flight Reservation").WinButton("FLIGHT").Click
    Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
    Window("Flight Reservation").WinEdit("Name:").Set "Tester"
    Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
    Window("Flight Reservation").WinEdit("Tickets:").Set "2"
    Window("Flight Reservation").WinRadioButton("First").Set
    Window("Flight Reservation").WinButton("Insert Order").Click
    Window("Flight Reservation").ActiveX("Threed Panel Control").WaitProperty "text", "Insert Done...", 2000
    orderNo = Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")
    MsgBox orderNo, vbInformation, "Order Number"

No comments:

Post a Comment