In this tutorial, you will learn about Descriptive Programming and establishing database connections in QTP, involving connections to external data resources like databases and MS Excel worksheets using QTP.
Descriptive programming uses a testing method where objects are characterized programmatically rather than being captured.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
=> Proceed Here For The QTP Training Tutorials Series
Before diving into the specifics of the tutorial, here’s a brief overview of our QTP article series:
This is the concluding tutorial from our online QTP training series. We hope these tutorials have been insightful and valuable for you.
Send us your feedback:
We look forward to your valuable feedback on not only this tutorial series but also on our overall website. Please follow this link to share your opinions about our website. It would take only a few moments. We highly appreciate your inputs and suggestions. Let us know how we can enhance your SoftwareTestingHelp.com experience.
What You Will Gain:
Descriptive Programming in QTP
Descriptive Programming initiatives test creation by programmatically specifying objects rather than recording them.
This technique empowers QTP to recognize objects that are not in the object repository.
Descriptive Programming comes in two types:
- Static Descriptive programming
- Dynamic Descriptive programming
Static Descriptive Programming
In static descriptive programming, objects are accessed by employing a combination of properties and values directly in a VB statement.
Syntax: TestObject(“Property name1:=property value”,”property name 2:=property value”,….n)
Here’s an illustration:
Browser("creationtime:=0").Page("title:=Google").WebButton("name:=Google Search")
Dynamic Descriptive Programming
Dynamic descriptive programming hinges on devising a description object to characterize an object.
Set Testdesc = description.create Testdesc("micClass").value = "webButton"
- micClass alludes to defined classes in QTP, such as webButton or weblist.
- In QTP 10, micClass values are case-sensitive whereas they’re not in QTP 11. Consequently, “webbutton” would fail and “webButton” must be used in QTP 10. That being said, “webbutton” and “webButton” would both work in QTP 11.
The following declaration draws out all objects of a certain class on a page:
Set ObjectList = Browser("creationtime:=0").Page("title:=*").ChildObjects(Testdesc) Msgbox ObjectList.count
This code extracts all buttons on a page and conserves them in the ObjectList object.
Descriptive programming is valuable due to its aptitude to function on any activated page. Regardless of whether it’s google.com, amazon.com or another site, the code remains consistent.
This is achieved as the page title is designated to * using a regular expression.
Descriptive programming permits code to be written once and employed in multiple scenarios by formulating objects at runtime and not hardcoding property valuations.
Continuing with our demonstration, imagine we wish to print the names of all the webbuttons on a page:
Here’s the method to access each button:
Msgbox ObjectList(0).GetRoProperty(“name”) – This prints the name of the first button.
Msgbox ObjectList(1).GetRoProperty(“name”)
Msgbox ObjectList(2).GetRoProperty(“name”)
Msgbox ObjectList(3).GetRoProperty(“name”)
Point to Remember:
- The index of child objects begins from 0.
- Given that properties of objects are obtained at runtime, the GetRoProperty method should be used.
The code above can be transformed to function for any number of buttons on the page using a ‘For loop’ that loops through the object count:
For i = 0 to ObjectList.count - 1 Step 1 Msgbox ObjectList(i).GetRoProperty("name") Next
A ‘For loop’ method is favorable when the number of objects in the description object is unknown.
Here are a few noteworthy points:
- Mastery of descriptive programming requires practice. Practical experience is crucial to utilize it effectively.
- As a tester, you don’t need to be aware of how objects are coded in your application under test (AUT) or the values they possess. Utilize ObjectSpy to inspect the properties and select the appropriate ones.
- The test outcomes will state that the test object was dynamically created during the operating session utilizing a programming description or ChildObject procedures.
Engaging with Commonly Utilized External Data Resources from QTP
During test formulation, occasions will arise where it’s necessary to establish connections to external databases or other data resources. At times, you may be required to shift data between these sources and QTP.
Though providing a comprehensive guide for interacting with external interfaces is beyond the range of these articles, we will go over certain commonly utilized ones.
Establishing a Database Connection in QTP
An ADO connection object is typically used to link to a database. ADO signifies Microsoft’s ActiveX Data Objects.
Follow the steps mentioned below:
#1) Design a DSN (Data Source Name). Please reference the database checkpoint tutorial or form one from the control panel.
#2) Develop a connection object:
Set conn = CreateObject(“ADODB.connection”)
#3) Construct a recordset object to accommodate query results:
Set rs = CreateObject(“ADODB.RecordSet”)
#4) Operate the connection object and run the query:
conn.Open “DSN=testDB2;UID=swatiseela;pwd=testing@123”
rs.Open “Select * from abc”, conn
#5) Use the “rs” object to access the query results.
#6) To obtain the count of returned rows, use:
rs.getrows
#7) For instance, if the table contains 2 rows and 3 columns (a, b, c), the values can be accessed as shown below:
Msgbox rs.fields(0).a
Msgbox rs.fields(0).b
Msgbox rs.fields(0).c
#8) Deploy a loop statement if there are several values to access.
#9) Functions of the recordset object encompass rs.move, rs.movenext, rs.getrows, rs.close, rs.open, etc.
The following is the unified code:
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.fields(0).b Msgbox rs.fields(0).c Msgbox rs.fields(1).a Msgbox rs.fields(1).b Msgbox rs.fields(1).c rs.close conn.close
Establishing Connections to MS Excel Worksheets
While working with an Excel application, the entire file serves as a workbook containing sheets with columns and rows designated for data.
Following is the code including comments intended to aid your comprehension of the procedure.
' Formulate an Excel application object Set excelobj = CreateObject("Excel.Application") ' Set it to visible to view the Excel application excelobj.visible = true ' Launch a workbook at the outlined path. To open a new workbook, utilize excelobj.workbooks.Add excelobj.workbooks.Open("C:UsersSwatiDesktopQTPtest.xls") ' Specify the current sheet as i. Sheet numbers begin from 1 i = 1 Set sheet1 = excelobj.activeworkbook.sheets(i) ' Compose to a cell in sheet1. For example, cell D8 excelobj.activeworkbook.sheets(1).cells(8,4) = "Test QTP Write to cell" ' Retrieve the data from sheet2 cell ID C6 testretrurnval = excelobj.activeworkbook.sheets(3).cells(6,3) ' Preserve the changes excelobj.activeworkbook.save ' Terminate the workbook excelobj.activeworkbook.close ' End Excel application excelobj.quit ' Clean memory Set excelobj = nothing
Aside from the above functions, here are a few handy ones:
- excelobj.activeworkbook.sheets.add – Adds a new sheet
- excelobj.activeworkbook.sheets(i).delete – Eradicates a sheet with index i
- excelobj.activeworkbook.sheeets(i).name = “Name of your preference” – Modifies the name of the sheet with index i
- x = excelobj.activeworkbook.sheets.count – Retrieves the count of sheets in a workbook
- excelobj.activeworkbook.saves “CompletePathWithNewName.xls” – Stores the workbook under a new name
This brings us to the end of our QTP training series. In our next article, we’ll go over some key QTP interview queries with responses. Your comments and questions are always appreciated, so feel free to share them.
=> Go Here For The QTP Training Tutorials Series
Stay subscribed for additional beneficial articles and tutorials on software testing! If you haven’t subscribed to our complimentary email newsletter, consider doing so by following this link.