QTP Descriptive Program
QTP Descriptive Program
com
www.gcreddy.com
www.gcreddy.net
For QTP Related Documents visit: www.gcreddy.com Types of Programmatic Descriptions (Descriptive Programming)
1) Static: - We enter the set of properties and values that describe the object directly in a VBScript statement. a) Direct specification of description in script Browser(micclass:=Browser).Page(micclass:=page).Link(name:=gcreddy).Click b) Assigning description to the variables and use that variables in script g_MainBrowser g_MainPage g_Lnk_Login =micclass:=Browser =micclass:=Page =name:=Login
Browser(g_MainBrowser).Page(g_MainPage).Link(g_Lnk_Login).Click 2. Dynamic: - We add a collection of properties and values to a Description object, and then enter the Description object name in the statement. Set oBrowser = Description.create oBrowser (micclass).value=Browser oBrowser (name).value= Google Set oPage = Description.create oPage (micclass).value=Page oPage (name).value= Google Set oLink = Description.create oLink (name).value= Login oLink (index).value= 1 Browser(oBrowser).Page(oPage).Link(oLink).click Using the Static type to enter programmatic descriptions directly into your statements may be easier for basic object description needs. However, in most cases, using the Dynamic type provides more power, efficiency, and flexibility. Handling Runtime added objects / Dynamic Objects / Same Description Objects Dynamic objects : Property values are getting changed regularly.
Same Description Objects : Property values are same for multiple objects. Runtime Added Object runtime. : Object is not there in recording time but it might be add in
Ex: Always the job links in Naukri and Monster sites will get update. Child object method is very useful to get control on Runtime added objects or dynamic objects or same description objects which are there application.
Descriptive Programming Examples: Dim oLinkDescription Dim oLinks Dim lnkCount Set oLinkDescription=Description.Create oLinkDescription("name").value="signin" set oLinks=browser("micclass:=Browser").page("micclass:=Page").ChildObject s(oLinkDescription) if oLinks.count <>0 then oLinks(0).click End If *********************************************************** This code will click on the first signin link. To click on second signin link replace oLinks(0).click to oLinks(1).click in if condition. In the same manner if any object properties are changed we can get index of the object using childobjects method. Ex2: If there is a login page having username, password, signin button fields. Think that the signin button property is getting changed frequently from signin to login or enter. Then we can use child objects method to get the buttons count from the page, and then using index we click on the button which we want. Set oButtonDescription=Description.Create oButtonDescription ("micclass").value="Button" set oButtons=browser("micclass:=Browser").page("micclass:=Page").ChildObj ects(oButtonDescription) for cnt=0 to oButtons.count-1 oButtons(cnt).highlight Next
For QTP Related Documents visit: www.gcreddy.com The above code will highlight all buttons in the page. This will make easy to identify the index of the button. Using index we can easily click on the button. What method do we need to follow... Static or Dynamic? There is no rule that we have to use static or dynamic. But based on situation we have to decide whether we need to use static or dynamic. If a single property is enough for an object then go for static. And go for Dynamic if multiple properties need to specify for an object. EX: Static Public const LoginPage_Edit_UserName Public const LoginPage_Edit_PassWord = name:=UserName = name:=Password
Public const LoginPage_Button_Login = name:=Login To use these constants in your script always its a good practice to follow is Keep these constants in a vbs file and associate to your QTP Test. Dynamic If youre going for dynamic the only problem is repetitive code. Every time we need use description.create which leads to more maintenance. There are three objects and if we want to use multiple properties for those objects we need to write description.create for three times. Solution: Public const LoginPage_Edit_UserName Public const LoginPage_Edit_PassWord = name:=UserName = name:=Password
Set LoginPage_Button_Login = CreateObjectDescription(name:=Login,type:=submit) *********************************************************** ******* Function CreateObjectDescription(StrProperties) Dim objDescription Dim ObjArr Dim PropCount For Manual Testing documents visit: www.gcreddy.net 5
For QTP Related Documents visit: www.gcreddy.com 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 *********************************************************** ******* This function gives a way to use multiple properties for any number of objects with less code maintenance. But the only one rule is we need follow is set statement. Because the return value of the function CreateObjectDescription is a description object. To store any object in a variable we need to use Set statement. The parameter which you pass to the function should be in string format. EX: Set LoginPage_Button_Login = CreateObjectDescription(name:=Login,type:=submit) Can I Use the combination of Object Repository and Descriptive programming in a statement? Yes. But the hierarchy must be in Test Object to Descriptive. Ex:Acceptable Browser(OR LogicalName).Page(OR LogicalName).Link(name:Login).click Or Browser(OR LogicalName).Page(micclass:Page).Link(name:Login).click Here the hierarchy is Test Object to Descriptive. Browser and page are representing Object Repository names and the link is taking description.
For QTP Related Documents visit: www.gcreddy.com Not acceptable Browser(micclass:=Browser).Page(OR LogicalName).Link(name:Login).click Or Browser(micclass:=Browser).Page(micclass:=Page).).Link(OR LogicalName).click This means that from the point where you have started giving description to the objects in a statement, you must continue giving description to the total objects in that hierarchy. Using Regular Expressions You can directly use regular expressions in the properties. Ex1: Public const LoginPage_Edit_UserName = name:=UserName.*
Ex2: Browser(micclass:=Browser).Page(micclass:=page).Link(name:=Login. *).Click Ex3: Set oLink = Description.create oLink (name).value= Login.* oLink (index).value= 1 Browser(micclass:=Browser).Page(micclass:=page).Link(oLink).click By default regular expressions for description object is enable. To disable Regular expressions for description object Set oLink = Description.create oLink (name).value= Login.* oLink (name).RegularExpression=False oLink (index).value= 1 In continuation of my old post Descriptive Programming in QTP, I prepared this sample script for the beginners of Descriptive Programming. 'This is a sample script on Flight Reservation Application using For Manual Testing documents visit: www.gcreddy.net 7
For QTP Related Documents visit: www.gcreddy.com 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\flight4 a.exe" '********************************************************* ********************** 'Object Description Constants '********************************************************* ********************** 'Login Public Const Login_Dialog="text:=Login" Set Login_Edit_AgentName=CreateObjectDescription("nativeclass:=Edit,att ached text:=Agent Name:") Set Login_Edit_Password=CreateObjectDescription("nativeclass:=Edit,attac hed text:=Password:") Set Login_Btn_OK=CreateObjectDescription("nativeclass:=Button,text:=OK ") 'Flight Reservation Window Public Public Public Public Const Const Const Const FlightReservation_Window="text:=Flight Reservation" FlightReservation_Menu= "menuobjtype:=2" FlightReservation_OpenOrder_Menu="File;Open Order..." FlightReservation_NewOrder_Menu="File;New Order"
For QTP Related Documents visit: www.gcreddy.com 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:=E dit,attached text:=Name:") Set FlightReservation_Edit_Tickets=CreateObjectDescription("nativeclass:= Edit,attached text:=Tickets:") Set FlightReservation_Rdb_Class=CreateObjectDescription("nativeclass:=Bu tton,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 For Manual Testing documents visit: www.gcreddy.net 9
For QTP Related Documents visit: www.gcreddy.com OpenOrder_ChkBtn_OrderNo=CreateObjectDescription("nativeclass:=Bu tton,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("Sudhakar","Mercury") 'Insert an Order OrderInserted=InsertOrder() 'Open Inserted Order Call OpenOrder(OrderInserted) 'Delete Inserted Order Call DeleteOrder (OrderInserted) 'Close Application Call CloseApplication() For Manual Testing documents visit: www.gcreddy.net 10
For QTP Related Documents visit: www.gcreddy.com '############################################ ################################# '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_D ateOfFlight).Type "111111" Window(FlightReservation_Window).WinComboBox(FlightReservation_C mb_FlyFrom).Select "Denver" Window(FlightReservation_Window).WinComboBox(FlightReservation_C mb_FlyTo).Select "Frankfurt" Window(FlightReservation_Window).WinButton(FlightReservation_Btn_Fl ight).Click For Manual Testing documents visit: www.gcreddy.net 11
For QTP Related Documents visit: www.gcreddy.com Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_Fli ghtsTable).WinList(FlightsTable_Lst_FlightList).Select 0 Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_Fli ghtsTable).WinButton(FlightsTable_Btn_OK).Click Window(FlightReservation_Window).WinEdit(FlightReservation_Edit_Na me).Set "sudhakar kakunuri" Window(FlightReservation_Window).WinEdit(FlightReservation_Edit_Tick ets).Set 10 Window(FlightReservation_Window).WinRadioButton(FlightReservation_ Rdb_Class).Set Window(FlightReservation_Window).WinButton(FlightReservation_Btn_I nsertOrder).Click Window(FlightReservation_Window).WinButton(FlightReservation_Btn_D eleteOrder).WaitProperty "enabled",true,10000 InsertOrder=Window(FlightReservation_Window).WinEdit(FlightReservat ion_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_Op enOrder).WinCheckBox(OpenOrder_ChkBtn_OrderNo).Set "ON" Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_Op enOrder).WinEdit(OpenOrder_Edit_OrderNo).Set OrderNumber Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_Op enOrder).WinButton(OpenOrder_Btn_OK).Click If err.Description="" Then Reporter.ReportEvent micPass,"Open Order","Order Opened For Manual Testing documents visit: www.gcreddy.net 12
For QTP Related Documents visit: www.gcreddy.com 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_D eleteOrder).Click Window(FlightReservation_Window).Dialog(FlightReservations_PopupDia log).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 For Manual Testing documents visit: www.gcreddy.net 13
'********************************************************* ********************** '********************************************************* ********************** Function CreateObjectDescription(StrProperties) Dim Dim Dim Dim objDescription ObjArr PropCount 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
14