A quick note about this QTP article series before we move to details of this tutorial:
This is the last tutorial in our online QTP training series. I hope you all enjoyed these tutorials and started learning from it. If you are new here please head over to the very first tutorial in this training series.
Give us feedback:
I am looking for your feedback not just for this tutorial series but about the overall site. Please click here to share your thoughts about this website. It will hardly take couple of minutes to complete. We value your feedback and suggestions. Let us know what we can do to improve your experience with SoftwareTestingHelp.com
In this tutorial you will learn what descriptive programming in QTP is, and how to connect to external data sources like database and MS Excel sheets using QTP.
Descriptive programming in QTP
Descriptive programming is a mechanism for creating tests where you use “Programmatic description” of objects instead of recording them.
Using this technique QTP can be made to identify objects that are not in the repository.
There are 2 variations of descriptive programming:
1) Static Descriptive programming
2) Dynamic Descriptive programming
Static Descriptive programming
Static method is when you try to access an object by using a set of properties and values directly in a VB statement.
Syntax: TestObject(“Property name1:=property value”,”property name 2:=property value”,….n)
This is how you use it:
Dynamic Descriptive programming
This works by creating a description object. Look at the following example to create a webButton object.
Set Testdesc=description.create Testdesc(“micClass”).value= “webButton”
- micClass refers to the predefined classes in QTP. The values you can assign can be webbutton, weblist etc.
- In QTP 10 micClass values are case sensitive but in QTP 11 onwards they are not. If you write webbutton in QTP 10 it will fail. You will have to write webButton. But the same webbutton will pass in QTP 11.
You can extract all the objects of a certain class in a page by using the following statement:
Set ObjectList=Browser(“creationtime:=0”).Page(“title:=*”).ChildObjects(Testdesc) Msgbox ObjectList.count
The above set of statements will extract all the buttons on a page and store them in the ObjectList object.
The versatility of using descriptive programming is that these lines of code will work on any open page. You can open google.com in your browser and it will count how many buttons are on that page. It will work exactly the same way if you had amazon.com or any other site open.
This is because we have the name of the title of the page set to * which is a regular expression.
So you can see, how we can write code that can be used in more than one occasions by not hard coding the property values and by creating the objects at run time.
Let us take our example a little further. Say I am trying to print the names of all the webbuttons on the page one after the other.
If there are 4 buttons in a page, you can access each one of them the following way:
Msgbox ObjectList (0).GetRoProperty(“name”) –This will print the name of the first button.
Msgbox ObjectList (1).GetRoProperty(“name”)
Msgbox ObjectList (2).GetRoProperty(“name”)
Msgbox ObjectList (3).GetRoProperty(“name”)
- The index of the child objects starts from 0
- Since the object achieves its properties at runtime, we use the GetRoProperty method to retrieve the same.
We can change the above code to work for any number of Buttons in the page by using a ‘For loop’ and repeating the statements within the ‘For block’ until it reaches the end of the object count.
For i=0 to ObjectList.count -1 to Step 1 Msgbox ObjectList (i).GetRoProperty(“name”) Next
Using a ‘For loop’ is better because in this case you don’t need to know how many objects are in your description object.
Couple of points to note:
- You will need practice to master descriptive programming. No matter how many examples you look at and understand, you will need hands on experience to really work with it.
- As a tester you are not expected to know how the objects are coded into your AUT and what values they are set to. So use ObjectSpy from time to time to choose the right properties to view the properties.
- The test results will indicate that the test object was created dynamically during the run session using a programming description or the ChildObject methods.
Connecting to some commonly used external data sources from QTP:
There will be many instances while you are preparing your tests that you will have to connect to an external DB or some other data sources. Once connected, you also will have to move data to and from these apps to QTP and vice versa. Though it is beyond the scope of these articles to provide a complete guide for working with external interfaces, we will look into a few that are most commonly used.
To connect to a database we typically use an ADO connection object. ADO is Microsoft’s ActiveX Data Objects.
The following are the steps to be followed:
a) Create a DSN. Please refer to the database checkpoint tutorial to see how this is done or create one from the control panel.
b) Create a connection object:
c) Create a record set object. The record set object holds the results of the query that we are going to run.
d) Open the connection object and run the query:
rs.Open “Select * from abc”,conn
e) All the query results can now be accessed using the “rs” object.
f) For example, if you want to get the count of the rows returned, you can use
g) For example, the table has 2 rows and 3 columns(a,b,c) you can access the values as follows:
h) You can use a loop statement if there are too many values to be accessed.
i) Some of the functions that record set object can use are: rs.move, rs.movenext, rs.getrows, rs.close, rs.open, etc.
Let us look at all the code at one time:
Set conn=CreateObject(“ADODB.connection”) Set rs=CreateObject(“ADODB.RecordSet”) conn.Open “DSN=testDB2;UID=swatiseela;pwd=testing@123” rs.Open “Select * from abc”,conn msgbox rs.getrows Msgbox rs.fields(0).a Msgbox rs.fiels(0).b Msgbox rs.fields(0).c Msgbox rs.fields(1).a Msgbox rs.fiels(1).b Msgbox rs.fields(1).c rs.close conn.close
Connecting to MS Excel sheets
We all know that when we open an excel application, the entire file is a workbook which has sheets with columns and rows where we put in the data.
The following is the code and comments to help you understand how it is done.
‘Create an excel application object Set excelobj = CreateObject(“Excel.Application”) ‘Set it to visible, otherwise it will be visible in the task manager but you will not be able to view it but it continues to work in the background excelobj.visible = true ‘Opens a workbook at the path speficified. If you need to open a new workbook, use excelobj.workbooks.Add excelobj.workbooks.Open(“C:\Users\Swati\Desktop\QTP\test.xls”) ‘Sets the current sheet as i. the sheet number starts from 1 i=1 Set sheet1 = excelobj.activeworkbook.sheets(i) ‘write to a cell in sheet 1. The cell is row 8 column 4, D8. excelobj.activeworkbook.sheets(1).cells(8,4) = “Test QTP Write to cell” ‘To get the data from sheet2 cell ID C6 testretrurnval = excelobj.activeworkbook.sheets(3).cells(6,3) ‘save changes excelobj.activeworkbook.save ‘close the workbook excelobj.activeworkbook.close ‘Close Excel application excelobj.quit ‘Clear memory Set excelobj = nothing
Apart from the above functions we have the following ones we can use depending on your need.
- excelobj.activeworkbook.sheets.add – To add a new sheet
- excelobj.activeworkbook.sheets(i).delete – To delete a sheet with index i
- excelobj.activeworkbook.sheeets(i).name = “Name of your choice” – To change the name of a sheet with the index i.
- x=excelobj.activeworkbook.sheets.count – to get the count of how many sheets are in a workbook
- excelobj. activeworkbook.saveas “CompletePathWithNewName.xls” – to save the workbook under a new name
This finishes not just this article but our QTP training series. In next article we will cover some more important QTP interview questions with answers. Please let us know your comments and questions.
Stay tuned for more useful articles and tutorials on software testing! If you are not subscribed to our free email newsletter, please do it now by clicking here.