Friday, December 24, 2010

Virtual Objects-Descriptive Programming

When the application is having objects that are unable to identify by QTP then we will be using the concept virtual objects. We can create virtual objects using Virtual Object Manager. That created virtual objects will be stored as a VOT file in <QuickTest installation folder>\ dat \ VoTemplate. If we want to use those virtual objects in another machine or If we want to use the scripts which are using virtual objects in different machines, then We must copy the .VOT files to the machines where you want to run. Here it will become a maintenance problem. If we forgot / neglect to do that then the scripts will not work.
Descriptive Programming for Virtual Objects is the best solution to overcome this problem. In this you need not create any objects using virtual object manager. The Virtual Objects uses the properties x, y, width and height. All you need to do is you have to mention these properties & property values in script.
For example, in the below script I have virtually created a button under a page. If i execute this automatically that particular area will be highlighted. For this I haven’t created any virtual object using Virtual Object Manager.
Browser("name:=Google").Page("title:=Google").VirtualButton("x:=698","y:=128","height:=18","width:=41","name:=hi").highlight
So now I can run this script on any machine without considering VOT files.
You can even store the object information in description object and use that description object in script.
Set oVirtualDescription=Description.Create
oVirtualDescription("x").value=698
oVirtualDescription("y").value=128
oVirtualDescription("height").value=18
oVirtualDescription("width").value=41
Browser("name:=Google").Page("title:=Google").VirtualButton(oVirtualDescription).highlight

There is no facility to create VirtualEdit. We can do descriptive programming for the classes which can be created using Virtual Object Manager. The thumb rule for Virtual Objects Descriptive Programming is we have to use the properties that QuickTest learns for the virtual objects in to object repository. 

For Virtual Lists you need to use one more property "rows:=number" and use "rows:=number", "columns:=number" for virtual table. 

sample script for the beginners of Descriptive Programming.
'This is a sample script on Flight Reservation Application using Descriptive Programming
'Script Flow :- Open Flight Reservation Application --> Login --> Insert Order --> Open Order --> Delete Order
'Object Descriptions created in the script by using CreateObjectDescription Function.
'If object is using only one property then i have created a constant for it.

'*******************************************************************************
'Input Variables
'*******************************************************************************

Dim ApplicationPath
ApplicationPath=Environment("ProductDir")&"\samples\flight\app\flight4a.exe"

'*******************************************************************************
'Object Description Constants
'*******************************************************************************
'Login

Public Const Login_Dialog="text:=Login"

Set Login_Edit_AgentName=CreateObjectDescription("nativeclass:=Edit,attached text:=Agent Name:")
Set Login_Edit_Password=CreateObjectDescription("nativeclass:=Edit,attached text:=Password:")
Set Login_Btn_OK=CreateObjectDescription("nativeclass:=Button,text:=OK")

'Flight Reservation Window

Public Const FlightReservation_Window="text:=Flight Reservation"
Public Const FlightReservation_Menu= "menuobjtype:=2"
Public Const FlightReservation_OpenOrder_Menu="File;Open Order..."
Public Const FlightReservation_NewOrder_Menu="File;New Order"

Set FlightReservation_Obj_DateOfFlight= CreateObjectDescription("nativeclass:=MSMaskWndClass,attached text:=Date of Flight:")
Set FlightReservation_Cmb_FlyFrom= CreateObjectDescription("nativeclass:=ComboBox,attached text:=Fly From:")
Set FlightReservation_Cmb_FlyTo= CreateObjectDescription("nativeclass:=ComboBox,attached text:=Fly To:")
Set FlightReservation_Btn_Flight= CreateObjectDescription("nativeclass:=Button,text:=FLIGHT")
Set FlightReservation_Edit_Name=CreateObjectDescription("nativeclass:=Edit,attached text:=Name:")
Set FlightReservation_Edit_Tickets=CreateObjectDescription("nativeclass:=Edit,attached text:=Tickets:")
Set FlightReservation_Rdb_Class=CreateObjectDescription("nativeclass:=Button,text:=First")
Set FlightReservation_Btn_InsertOrder= CreateObjectDescription("nativeclass:=Button,text:=&Insert Order")
Set FlightReservation_Btn_UpdateOrder= CreateObjectDescription("nativeclass:=Button,text:=&Update Order")
Set FlightReservation_Btn_DeleteOrder= CreateObjectDescription("nativeclass:=Button,text:=&Delete Order")
Set FlightReservation_Edit_OrderNo=CreateObjectDescription("nativeclass:=Edit,attached text:=Order No:")

'Select Flights Dialog

Public Const FlightReservation_Dialog_FlightsTable= "text:=Flights Table"

Set FlightsTable_Lst_FlightList= CreateObjectDescription("nativeclass:=ListBox,attached text:=From")
Set FlightsTable_Btn_OK= CreateObjectDescription("nativeclass:=Button,text:=OK")

'Open Order Dialog

Public Const FlightReservation_Dialog_OpenOrder="text:=Open Order"

Set OpenOrder_ChkBtn_OrderNo=CreateObjectDescription("nativeclass:=Button,text:=&Order No.")
Set OpenOrder_Btn_OK=CreateObjectDescription("nativeclass:=Button,text:=OK")
Set OpenOrder_Edit_OrderNo=CreateObjectDescription("nativeclass:=Edit,window id:=1016")

'Flight Reservation Popup Dialog
Public Const FlightReservations_PopupDialog="text:=Flight Reservations"
Set FlightReservations_Btn_YES= CreateObjectDescription("nativeclass:=Button,text:=&Yes")

''*******************************************************************************
'Object Description Constants End
'*******************************************************************************

'#############################################################################
'Main Script Start
'#############################################################################
'Open Application
Call OpenApplication (ApplicationPath)

'Login
Call Login("Mercury","Mercury")

'Insert an Order
OrderInserted=InsertOrder()

'Open Inserted Order
Call OpenOrder(OrderInserted)

'Delete Inserted Order
Call DeleteOrder (OrderInserted)

'Close Application
Call CloseApplication()

'#############################################################################
'Main Script End
'#############################################################################

'Functions Start
'*******************************************************************************
Function OpenApplication(AppPath)
    InvokeApplication AppPath
End Function
'*******************************************************************************
'*******************************************************************************
Function Login(iUserName,iPassword)

Dialog(Login_Dialog).WinEdit(Login_Edit_AgentName).Set iUserName
Dialog(Login_Dialog).WinEdit(Login_Edit_Password).Set iPassword
Dialog(Login_Dialog).WinButton(Login_Btn_OK).Click

If err.Description="" Then
    Reporter.ReportEvent micPass,"Login","Login Successful"
Else
    Reporter.ReportEvent micFail,"Login","Login Failed"
End If
End Function
'*******************************************************************************
'*******************************************************************************
Function InsertOrder()

Window(FlightReservation_Window).WinMenu(FlightReservation_Menu).Select FlightReservation_NewOrder_Menu
Window(FlightReservation_Window).WinObject(FlightReservation_Obj_DateOfFlight).Type "111111"
Window(FlightReservation_Window).WinComboBox(FlightReservation_Cmb_FlyFrom).Select "Denver"
Window(FlightReservation_Window).WinComboBox(FlightReservation_Cmb_FlyTo).Select "Frankfurt"
Window(FlightReservation_Window).WinButton(FlightReservation_Btn_Flight).Click
Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_FlightsTable).WinList(FlightsTable_Lst_FlightList).Select 0
Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_FlightsTable).WinButton(FlightsTable_Btn_OK).Click
Window(FlightReservation_Window).WinEdit(FlightReservation_Edit_Name).Set "TEST NAME"
Window(FlightReservation_Window).WinEdit(FlightReservation_Edit_Tickets).Set 10
Window(FlightReservation_Window).WinRadioButton(FlightReservation_Rdb_Class).Set
Window(FlightReservation_Window).WinButton(FlightReservation_Btn_InsertOrder).Click
Window(FlightReservation_Window).WinButton(FlightReservation_Btn_DeleteOrder).WaitProperty "enabled",true,10000
InsertOrder=Window(FlightReservation_Window).WinEdit(FlightReservation_Edit_OrderNo).GetROProperty("text")

If err.Description="" Then
    Reporter.ReportEvent micPass,"Insert Order","Order Inserted Successfully"
Else
    Reporter.ReportEvent micFail,"Insert Order","Failed in Inserting an Order"
End If

End Function

'*******************************************************************************
'*******************************************************************************
Function OpenOrder(OrderNumber)

Window(FlightReservation_Window).WinMenu(FlightReservation_Menu).Select FlightReservation_OpenOrder_Menu
Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_OpenOrder).WinCheckBox(OpenOrder_ChkBtn_OrderNo).Set "ON"
Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_OpenOrder).WinEdit(OpenOrder_Edit_OrderNo).Set OrderNumber
Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_OpenOrder).WinButton(OpenOrder_Btn_OK).Click

If err.Description="" Then
    Reporter.ReportEvent micPass,"Open Order","Order Opened Successfully"
Else
    Reporter.ReportEvent micFail,"Open Order","Failed in Opening an Order"
End If

End Function
'*******************************************************************************
'*******************************************************************************
Function DeleteOrder(OrderNumber)

Call OpenOrder(OrderNumber)

Window(FlightReservation_Window).WinButton(FlightReservation_Btn_DeleteOrder).Click
Window(FlightReservation_Window).Dialog(FlightReservations_PopupDialog).WinButton(FlightReservations_Btn_YES).Click

If err.Description="" Then
    Reporter.ReportEvent micPass,"Delete Order","Order Deleted Successfully"
Else
    Reporter.ReportEvent micFail,"Delete Order","Failed in Deleting an Order"
End If

End Function
'*******************************************************************************
'*******************************************************************************

Function CloseApplication()
    Window(FlightReservation_Window).Close
    If err.Description="" Then
        Reporter.ReportEvent micPass,"Close Application","Application Closed Successfully"
    Else
        Reporter.ReportEvent micFail,"Close Application","Failed in Closing the Application"
    End If
End Function

'*******************************************************************************
'*******************************************************************************
Function CreateObjectDescription(StrProperties)

    Dim objDescription
    Dim ObjArr
    Dim PropCount
    Dim ObjProperty
   
    Set objDescription=Description.Create

    ObjArr=split(StrProperties,",")
   
    For PropCount=0 to ubound(ObjArr)
        ObjProperty=split(ObjArr(PropCount),":=")
        objDescription(ObjProperty(0)).value=ObjProperty(1)
    Next
   
  Set CreateObjectDescription=objDescription

End Function

'*******************************************************************************
'Functions End
'*******************************************************************************



No comments:

Post a Comment