17
For QTP Related Documents visit: www.gcreddy.com Descriptive Programming in QTP For QTP Documents visit: www.gcreddy.com For Manual Testing Documents visit: www.gcreddy.net For Manual Testing documents visit: www.gcreddy.net 1

qtpDescriptiveProgram.doc

Embed Size (px)

DESCRIPTION

descriptive program

Citation preview

Page 1: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

Descriptive Programming in QTP

For QTP Documents visit:

www.gcreddy.com

For Manual Testing Documents visit:

www.gcreddy.net

For Manual Testing documents visit: www.gcreddy.net 1

Page 2: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

What is Descriptive Programming?

Providing Objects information directly into the Test Script, instead of storing objects information in the Object Repository.

Descriptive Programming is a method of performing operation on the object which is not there in Object Repository. We can also use programmatic descriptions to perform the same operation on several objects with certain identical properties, or to perform an operation on an object whose properties match a description that we determine dynamically during the run session.

Tests using Descriptive programming are faster than Tests using Object Repository

Benefits of Descriptive Programming

If the AUT (Application Under Test) is having Dynamic Objects, then it is better to use descriptive programming. Incase of object repository, it is Difficult to handle dynamic Objects.

When we have more objects to perform operations, in performance point of view descriptive programming is better than object repository method. If we use object repository for generating huge tests, then QTP performance will be decreased.

Descriptive Programming is useful if the AUT is having objects that are adding in the Run Time. We can’t add Objects to Object Repository in run time.

If we need to start Test Automation process before the AUT is ready

Descriptive Programming is useful, if the AUT is having similar type of objects or similar name objects. Incase of Object Repository, It will create multiple objects with same description unnecessarily.

Shared Object Repository is not changeable by multiple persons at a time.Maintenance becomes harder if all the team members have created their own object repositories. In this situation Descriptive Programming is very flexible.

Tests using Descriptive Programming can be executed in any version of QTP without any changes.

Code Portability: Just code is enough to run Tests. We can copy and paste in other scripts for any other required requirement.

Reusing of Properties: We can assign properties to a global variable and reuse it for same type of objects.

Plug & Play: Any time Tests will be in ready to run state. No need to worry about any other settings or files.

Just Maintenance of variables: We can store the object properties in the form of variables in a txt / vbs file which will be placed in central location. In this case we just need to maintain that file.

For Manual Testing documents visit: www.gcreddy.net 2

Page 3: qtpDescriptiveProgram.doc

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 =“micclass:=Browser”g_MainPage =“micclass:=Page”g_Lnk_Login =“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.createoBrowser (“micclass”).value=”Browser”oBrowser (“name”).value= “Google”

Set oPage = Description.createoPage (“micclass”).value=”Page”oPage (“name”).value= “Google”

Set oLink = Description.createoLink (“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 : Object is not there in recording time but it might be add in runtime.

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.

For Manual Testing documents visit: www.gcreddy.net 3

Page 4: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

Ex1: If there are multiple signin links on the page, using child objects method we can click on any signin link.

Descriptive Programming Examples:

Dim oLinkDescriptionDim oLinksDim lnkCount

Set oLinkDescription=Description.CreateoLinkDescription("name").value="signin"

set oLinks=browser("micclass:=Browser").page("micclass:=Page").ChildObjects(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.CreateoButtonDescription ("micclass").value="Button"set oButtons=browser("micclass:=Browser").page("micclass:=Page").ChildObjects(oButtonDescription)

for cnt=0 to oButtons.count-1oButtons(cnt).highlightNext

For Manual Testing documents visit: www.gcreddy.net 4

Page 5: qtpDescriptiveProgram.doc

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 = “name:=UserName”

Public const LoginPage_Edit_PassWord = “name:=Password”

Public const LoginPage_Button_Login = “name:=Login”

To use these constants in your script always it’s a good practice to follow is Keep these constants in a vbs file and associate to your QTP Test.

‘Dynamic

If you’re 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 = “name:=UserName”

Public const LoginPage_Edit_PassWord = “name:=Password”

Set LoginPage_Button_Login = CreateObjectDescription(“name:=Login,type:=submit”)

‘******************************************************************Function CreateObjectDescription(StrProperties)

Dim objDescriptionDim ObjArrDim PropCountDim ObjProperty

Set objDescription=Description.Create

For Manual Testing documents visit: www.gcreddy.net 5

Page 6: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

ObjArr=split(StrProperties,",")

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

Next

Set CreateObjectDescription=objDescriptionEnd 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”).clickOrBrowser(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.

‘Not acceptable

Browser(“micclass:=Browser”).Page(OR LogicalName).Link(“name:Login”).click

OrBrowser(“micclass:=Browser”).Page(“micclass:=Page”).).Link(OR LogicalName).click

For Manual Testing documents visit: www.gcreddy.net 6

Page 7: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

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.createoLink (“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.createoLink (“name”).value= “Login.*”oLink (“name”).RegularExpression=FalseoLink (“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 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

For Manual Testing documents visit: www.gcreddy.net 7

Page 8: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

'*******************************************************************************Dim ApplicationPathApplicationPath=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:")

For Manual Testing documents visit: www.gcreddy.net 8

Page 9: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

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 DialogPublic Const FlightReservations_PopupDialog="text:=Flight Reservations"Set FlightReservations_Btn_YES= CreateObjectDescription("nativeclass:=Button,text:=&Yes")

For Manual Testing documents visit: www.gcreddy.net 9

Page 10: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

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

'#############################################################################'Main Script Start'#############################################################################'Open ApplicationCall OpenApplication (ApplicationPath)

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

'Insert an OrderOrderInserted=InsertOrder()

'Open Inserted OrderCall OpenOrder(OrderInserted)

'Delete Inserted OrderCall DeleteOrder (OrderInserted)

'Close ApplicationCall CloseApplication()

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

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

For Manual Testing documents visit: www.gcreddy.net 10

Page 11: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

Dialog(Login_Dialog).WinEdit(Login_Edit_AgentName).Set iUserNameDialog(Login_Dialog).WinEdit(Login_Edit_Password).Set iPasswordDialog(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 IfEnd Function'*******************************************************************************'*******************************************************************************Function InsertOrder()

Window(FlightReservation_Window).WinMenu(FlightReservation_Menu).Select FlightReservation_NewOrder_MenuWindow(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).ClickWindow(FlightReservation_Window).Dialog(FlightReservation_Dialog_FlightsTable).WinList(FlightsTable_Lst_FlightList).Select 0Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_FlightsTable).WinButton(FlightsTable_Btn_OK).ClickWindow(FlightReservation_Window).WinEdit(FlightReservation_Edit_Name).Set "sudhakar kakunuri"Window(FlightReservation_Window).WinEdit(FlightReservation_Edit_Tickets).Set 10Window(FlightReservation_Window).WinRadioButton(FlightReservation_Rdb_Class).SetWindow(FlightReservation_Window).WinButton(FlightReservation_Btn_InsertOrder).ClickWindow(FlightReservation_Window).WinButton(FlightReservation_Btn_DeleteOrder).WaitProperty "enabled",true,10000InsertOrder=Window(FlightReservation_Window).WinEdit(FlightReservation_Edit_OrderNo).GetROProperty("text")

If err.Description="" Then    Reporter.ReportEvent micPass,"Insert Order","Order Inserted

For Manual Testing documents visit: www.gcreddy.net 11

Page 12: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

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_MenuWindow(FlightReservation_Window).Dialog(FlightReservation_Dialog_OpenOrder).WinCheckBox(OpenOrder_ChkBtn_OrderNo).Set "ON"Window(FlightReservation_Window).Dialog(FlightReservation_Dialog_OpenOrder).WinEdit(OpenOrder_Edit_OrderNo).Set OrderNumberWindow(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).ClickWindow(FlightReservation_Window).Dialog(FlightReservations_PopupDialog).WinButton(FlightReservations_Btn_YES).Click

If err.Description="" Then

For Manual Testing documents visit: www.gcreddy.net 12

Page 13: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

    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 IfEnd 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

For Manual Testing documents visit: www.gcreddy.net 13

Page 14: qtpDescriptiveProgram.doc

For QTP Related Documents visit: www.gcreddy.com

End Function

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

For QTP Interview questions and answers:

www.gcreddy.com

For Manual Testing documents visit: www.gcreddy.net 14