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

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated February 27, 2024

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;pwd=testing@123”
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;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 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:\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 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.

Was this helpful?

Thanks for your feedback!

Recommended Reading

14 thoughts on “Descriptive Programming in QTP and Database Connection in QTP – Tutorial #25”

  1. Hi, i need help with PDF files.
    Inmy application i have a print button, by clicking on it , the report is downloaded and saved in commonfolder. I need help in automating this script. Right now by script i am able to download but i am unable to go the saved folder and open it and read the data . Could u please help me

    Reply
  2. so i have these two similar functions sort of

    function type(fnhtlmid, fnvalue)
    set trbrowserAndPage = Browser(“title:=.*”).Page(“title:=.*”)
    set strobject = description.Create()
    strobject(micClass).value = “WedEdit”
    strobject(html id).value = fnhtmlid
    strobject(visible).value = True
    strbrowserAndPage.WebEdit(strobject).Set fnvalue
    End function

    function strType(fnhtlmid, fnvalue)
    set trbrowserAndPage = Browser(“title:=.*”).Page(“title:=.*”)
    trbrowserAndPage .WebEdit(“micClass:=WebEdit”, “html id:=”&fnhtmlid, “visible:=True”).Set fnvlaue
    End function

    for these two functions above,
    Type “fnhtmlid”, “fnvalue” works
    strType “fnhtmlid”, “fnvalue” throws an exception (it says fnhtmlid is undefined)
    Any idea how to get “strType” to work.

    Reply
  3. ‘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

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

    Reply
  5. 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???

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

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

    Reply
  8. @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.

    Reply

Leave a Comment