Descriptive Programming in QTP and Database Connection in QTP – Tutorial #25

In this tutorial, you will learn what Descriptive Programming and Database connection in QTP i.e. how to connect to external data sources like databases and MS Excel sheets using QTP. 

Descriptive programming is a mechanism for creating tests where you use “Programmatic description” of objects instead of recording them.

=> Click Here For QTP Training Tutorials Series

Descriptive Programming Database Connection

A quick note about this QTP article series before we move to the 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 them.

Give us your feedback:

feedback

I am looking forward to your feedback not just for this tutorial series but also about the overall site. Please click here to share your thoughts about this website.  It will hardly take a 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


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:

  • Static Descriptive programming
  • Dynamic Descriptive programming

Static Descriptive Programming

A 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:

 Browser(“creationtime:=0”).Page(“title:=Google”).WebButton(“name:=Google Search”) 

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 on 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 have it on 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 occasion by not hard coding the property values and by creating the objects at runtime.

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 on a page, you can access each one of them in 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”)

Note that:

  • 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 on 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

In this case, using a ‘For loop’ is better because, you don’t need to know how many objects are in your description object.

A couple of points to note:

  • You will need the 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 ChildObject methods.

Connecting to Commonly Used External Data Sources from QTP

There will be many instances while you are preparing the tests that you will have to connect to an external DB or some other data sources. Once connected, you will also 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.

Database Connection in QTP

To connect to a database, we typically use an ADO connection object. ADO is Microsoft’s ActiveX Data Objects.

Given below are the steps to be followed:

#1) Create a DSN. Please refer to the database checkpoint tutorial to see how this is done or create one from the control panel.
#2) Create a connection object:
Set conn=CreateObject(“ADODB.connection”)
#3) Create a recordset object. The recordset object holds the results of the query that we are going to run.
Set rs=CreateObject(“ADODB.RecordSet”)
#4) Open the connection object and run the query:
conn.Open “DSN=testDB2;UID=swatiseela;[email protected]
rs.Open “Select * from abc”,conn
#5) All the query results can now be accessed using the “rs” object.
#6) For example, if you want to get the count of the rows returned, you can use
rs.getrows
#7) For example, the table has 2 rows and 3 columns(a,b,c) and you can access the values as follows:
Msgbox rs.fields(0).a
Msgbox rs.fiels(0).b
Msgbox rs.fields(0).c
#8) You can use a loop statement if there are too many values to be accessed.
#9) 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 codes at one time:

Set conn=CreateObject(“ADODB.connection”)
Set rs=CreateObject(“ADODB.RecordSet”)
conn.Open “DSN=testDB2;UID=swatiseela;[email protected]”
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 that has sheets with columns and rows where we put in the data.

The following are 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:UsersSwatiDesktopQTPtest.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 that we can use depending on your needs.

  • 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 the sheet with the index i
  • x=excelobj.activeworkbook.sheets.count – To get the count of how many sheets are in a workbook
  • excelobj. activeworkbook.saves “CompletePathWithNewName.xls” – To save the workbook under a new name

This finishes not just this article but our QTP training series. In the next article, we will cover some more important QTP interview questions with answers. Please let us know your comments and questions.

=> Visit Here For The QTP Training Tutorials Series

Stay tuned for more useful articles and tutorials on software testing! If you are not subscribed to our free email newsletter, please do so now by clicking here.

Related Post

Leave a Reply

Your email address will not be published.