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


In this tutorial you will learn what descriptive programming in QTP is, and Database connection in QTP i.e. how to connect to external data sources like database and MS Excel sheets using QTP. 

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:

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

**************

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:

 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 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”)

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

Database Connection in QTP: 
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:
Set conn=CreateObject(“ADODB.connection”)
c) Create a record set object. The record set object holds the results of the query that we are going to run.
Set rs=CreateObject(“ADODB.RecordSet”)
d) Open the connection object and run the query:
conn.Open “DSN=testDB2;UID=swatiseela;pwd=testing@123”
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
rs.getrows
g) For example, the table has 2 rows and 3 columns(a,b,c) you can access the values as follows:
Msgbox rs.fields(0).a
Msgbox rs.fiels(0).b
Msgbox rs.fields(0).c
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.


The Best Software Testing Training You'll Ever Get!

software testing QA training

9 comments ↓

#1 Shaukat on 05.25.13 at 6:00 am

i would like to tell big thanks to you for QTP tutorial really its helpful lot to me. Is any idea to write book on how to track interview on QTP or please let me know if you could already wrote about this in your blog.

#2 Sibu S on 06.07.13 at 8:30 am

Many Thanks for such an Effective Training Series of QTP

#3 Venu on 06.10.13 at 10:14 am

In the above programming code(Connecting to MS Excel sheets)

‘To get the data from sheet2 cell ID C6 you have specified the code logic as

testretrurnval = excelobj.activeworkbook.sheets(3).cells(6,3)

I think it should be testretrurnval = excelobj.activeworkbook.sheets(2).cells(6,2) as we are extracting the data from sheet 2 and nt sheet 3. If i’m wrong let me knoe…I think i’m nt

#4 Swati on 06.10.13 at 6:07 pm

@Venu: you are right! We will have it corrected. Thanks for pointing out.

#5 soumya on 09.24.13 at 4:59 am

i have two browser opened.in two browser ,google home page is opened.i have to put some data in my second opened google page.can u plz guide me???

#6 kiranmayee on 11.05.13 at 7:49 am

Step 1: Launch http://www.infibeam.com/ (add url as variable from excel file dont use datatable methods)

Set myxl = createobject(“excel.application”)

myxl.Workbooks.Open “C:\Users\kiranmayee\Desktop\infi.xlsx”

myxl.Application.Visible = true

set mysheet = myxl.ActiveWorkbook.Worksheets(“Sheet1″)

‘Get the max row occupied in the excel file
Row=mysheet.UsedRange.Rows.Count

‘Get the max column occupied in the excel file
Col=mysheet.UsedRange.columns.count

‘To read the data from the entire Excel file
‘For i= 1 to Row
‘ For j=1 to Col
Systemutil.Run mysheet.cells(1,1).value
‘ Next
‘Next

‘Save the Workbook
‘myxl.ActiveWorkbook.Save

‘Close the Workbook
myxl.ActiveWorkbook.Close

‘Close Excel
myxl.Application.Quit

‘step 2:Navigate to All Stores->electronics->calling tablets->select product with range (1000-15000INR) (use descriptive prgramming for this flow) click on “Buy Now” button”

Dim hWnd
‘browser(“Infibeam.com: Online Shopping”).page(“Infibeam.com: Online Shopping”).WebElement(“Mobiles & Tablets”).Click
‘hWnd = Browser(“Infibeam.com: Online Shopping”).GetROProperty(“hwnd”)
‘Window(“hwnd:=” & hWnd).Type micRight
‘Window(“hwnd:=” & hWnd).select “Calling”

‘browser(“name:=Infibeam.com: Online Shopping for Mobiles, Books, Tablets, Gifts & more in India”).page(“title:=Infibeam.com: Online Shopping for Mobiles, Books, Tablets, Gifts & more in India”).webelement(“innertext:=ALL STORES”).Click
Browser(“Infibeam.com: Online Shopping”).Page(“Infibeam.com: Online Shopping”).Link(“Calling”).Click
Browser(“Infibeam.com: Online Shopping”).Page(“Calling Tablets Store:”).Image(“HCL ME Tablet Connect”).Click
Browser(“Infibeam.com: Online Shopping”).Page(“HCL ME Tablet Connect”).Image(“submit”).Click 29,22

‘step 3:product name and exact price of product should be captured and write in Test file

Dim x,y,fso,f

x=browser(“Shopping Cart”).Page(“Shopping Cart”).Link(“HCL ME Tablet Connect”).GetTOProperty(“name”)
msgbox x

y=browser(“Shopping Cart”).Page(“Shopping Cart”).webelement(“class:=price”).GetROProperty(“innertext”)
msgbox y

Set fso=createobject(“scripting.FileSystemObject”)
Set f=fso.OpenTextFile(“C:\Users\kiranmayee\Desktop\a1.txt”,2,true)
f.WriteLine(x)
f.WriteLine(y)

‘step 4:check whether added product is available into shopping cart (create reusable function with valid parameter)
RunAction “Copy of Action2″, oneIteration
‘If browser(“Shopping Cart”).Page(“Shopping Cart”).Link(“HCL ME Tablet Connect”).Exist Then
‘Reporter.ReportEvent micPass,”product is successfully added in to the shopping cart”,”Test passed”
‘else
‘Reporter.ReportEvent micFail, “product is not added in the shopping cart”, “Test failed”
‘End If

‘Step 5:Close the browser
Browser(“Shopping Cart”).Close

#7 Ritu on 11.08.13 at 11:57 am

@Swati as you told that Venu’s commnet is correct for Connecting to MS Excel Sheets.

testretrurnval=excelobj.activeworkbook.sheets(2).cells(6,2)

But i think it should be

testretrurnval=excelobj.activeworkbook.sheets(2).cells(6,3)

because cell ID is C6.
Please let me know if I’m wrong.

#8 kiranmayee on 11.10.13 at 4:40 pm

‘Step 1: Launch http://www.infibeam.com/ (add url as variable from excel file dont use datatable

Set myxl = createobject(“excel.application”)

myxl.Workbooks.Open “C:\Users\kiranmayee\Desktop\infi.xlsx”

myxl.Application.Visible = true

set mysheet = myxl.ActiveWorkbook.Worksheets(“Sheet1″)

‘Get the max row occupied in the excel file
Row=mysheet.UsedRange.Rows.Count

‘Get the max column occupied in the excel file
Col=mysheet.UsedRange.columns.count

‘To read the data from the entire Excel file
For i= 1 to Row
For j=1 to Col
Systemutil.Run mysheet.cells(i,j).value
Next
Next

‘Save the Workbook
‘myxl.ActiveWorkbook.Save

‘Close the Workbook
myxl.ActiveWorkbook.Close

‘Close Excel
myxl.Application.Quit

RunAction “Navigating_calling tablets”, oneIteration

‘step 2:Navigate to All Stores->electronics->calling tablets->select product with range (1000-15000INR) (use descriptive prgramming for this flow) click on “Buy Now” button”

browser(“name:=Infibeam.com: Online Shopping for Mobiles, Books, Tablets, Gifts & more in India”).page(“title:=Infibeam.com: Online Shopping for Mobiles, Books, Tablets, Gifts & more in India”).link(“name:=ALL STORES”).Click
browser(“name:=Infibeam.com – All Stores – Books, Gifts, Electronics, Automobiles, Lifestyle”).page(“title:=Infibeam.com – All Stores – Books, Gifts, Electronics, Automobiles, Lifestyle”).webtable(“name:=Books”).link(“name:=Mobiles”).Click
browser(“name:=Mobiles Price List, New Mobiles Price India, Latest Mobile Phones to Buy Online in India – Infibeam.com”).page(“title:=Mobiles Price List, New Mobiles Price India, Latest Mobile Phones to Buy Online in India – Infibeam.com”).link(“name:= Tablets “).Click
browser(“name:=Tablet Price List, New Tablets Prices India, Tablet Online Shopping Store – Infibeam.com”).page(“title:=Tablet Price List, New Tablets Prices India, Tablet Online Shopping Store – Infibeam.com”).link(“href:=http://www.infibeam.com/Portable_Electronics/calling-tablets-showcase.html”).click
Dim Tot_no_products,All_products,product_pice,i,product_name,price_product,Buying_product
Set oDesc = Description.Create
oDesc(“micclass”).value = “WebElement”
oDesc(“class”).Value = “normal”
set All_products=Browser(“name:=Calling Tablets: Tablet with SIM, Tablet with Calling Facility – Infibeam.com”).Page(“title:=Calling Tablets: Tablet with SIM, Tablet with Calling Facility – Infibeam.com”).ChildObjects(oDesc)
Tot_no_products=All_products.count
msgbox Tot_no_products

For i = 0 to Tot_no_products -1
product_price=All_products(i).getroproperty(“innertext”)

if eval(product_price>10000 and product_price<=15000)Then
'print product_price 'Displays all the prices which are in between 10000-15000
a=i
end if
next
All_products(a).Click
Browser( "creationtime:=0" ).Sync
Buying_product=Browser( "creationtime:=0" ).page("name:=.*").webelement("class:=product-title-big ").GetTOProperty("innertext")
Browser( "creationtime:=0" ).page("name:=.*").image("name:=submit").Click
RunAction "Captured_ in Text file", oneIteration

'step 3:product name and exact price of product should be captured and write in Test file

product_name=browser("name:=Shopping Cart").page("title:=Shopping Cart").link("html tag:=A").GetTOProperty("innertext")
product_name=browser("name:=Shopping Cart").page("title:=Shopping Cart").link("html tag:=A").GetTOProperty("innertext")
Set fso=createobject("scripting.FileSystemObject")
Set f=fso.OpenTextFile("C:\Users\kiranmayee\Desktop\a1.txt",2,true)
f.WriteLine(product_name)
f.WriteLine(price_product)
RunAction "Checking the product", oneIteration

'step 4: check whether added product is available into shopping cart (create reusable function with valid parameter)
If Buying_product = product_name Then
reporter.ReportEvent micPass, " selected product is successfully added in to the shopping cart","Test passed"
else
Reporter.ReportEvent micFail, "selected product is not added in the shopping cart","Test failed"
End If

RunAction "Close_browser", oneIteration

'step 5: close the browser

browser("name:=Shopping Cart").Close

#9 Eswar on 01.30.14 at 2:16 pm

Where can I contact you?

Leave a Comment