Professional Documents
Culture Documents
As the name
suggests, Data Driven Testing Framework is a
framework which
is driven by test data, which means that test data is
the important
factor here. The basic expectation with this kind
of test
automation framework is that your test scripts
should be built
in such a way that it should work for different
sets of data
without any changes to the test script.
Consider a
scenario where you have a large number of user
credentials for an
application. You need to login to the application with
each of these
credentials to find out which of the user credentials
are working properly and which are not. In order to complete this task, you can create a script in QTP which
would read each of the user credentials and try to login to the application and at the end would report the
results.
What are the different Data Sources that can be used in QTP?
QTP supports a wide variety of Data Sources from where you can extract data for your test scripts. Some of
these are 1) Microsoft Excel
2) Text files
3) XML files
4) Databases like SQL, Oracle etc
5) and many more
Although there are many data sources that you can use with the QTP test scripts, the data
source that is used almost all the times is Microsoft Excel. In most of the applications that you would
be automating, you would be taking data from excel only.
However there are many cases where you would be using some different data sources also. For example, if
you are working on an end to end billing application, many a times the product and billing information are
stored in XMLs. So in these type of scenarios, your primary data source will be xml.
There would also be many cases where you would need to use more than one data source. For
example, in the above scenario, xmls will be your primary data source but you can use excel sheet also to
save login credentials, URLs etc of the application. So excel in this case would be the secondary data source.
Step 4: Now, the next step is to fill in the data in the data sheet. We will do this by setting up the header for
the columns first. Notice that the column headers are displayed as A, B, C, D and so on. To change a column
header, double click on that column header. QTP will provide a popup where you need to provide the new
value for the header. In this case, we want to change the value A to something meaningful like UserName.
So double-click on A and a new popup box will appear as shown below.
Step 5: Enter UserName in the text field in the popup message and click on Ok button. Once you do that you
would notice that the name of the column will get changed from A to UserName. Refer the image below.
Step 6: Similarly change the column header B to Password.
Wait(1)
Window("text:=Flight Reservation").Close() End If
After you run this code, you will get the same result i.e, for rows 2 and 3 the login will fail. But
did you notice the change in format of result in this case and the previous case?
a) The main advantage of this method is that it is very each to script for taking data from
the datatable.
b) Another advantage is that you can use the Run on all rows setting to run the same script
for multiple data without making any changes in the script.
Disadvantages
a) This concept cant be used for complex frameworks where you need to reuse the same
set of data for different test cases
b) This method also doesnt work with modular framework where the test data needs to be
grouped based on the functions rather than test cases.
Once you are done with all these initial things, you can start creating the test scripts.
Step by Step method to Create the First Test Case in the Framework
will
the
the
the
Function fnGetDataTableValue(strColumnName)
'Add a data sheet in the QTP Data Table
DataTable.AddSheet("dtDataSheet")
'Import the Excel Sheet into QTP Data Table
DataTable.ImportSheet "D:\DataSheet.xls", sTestCaseName, "dtDataSheet"
'Find and return the value based on the column name
DataTable.SetCurrentRow 1
fnGetDataTableValue = DataTable.Value(strColumnName, "dtDataSheet")
'Remove Reference
DataTable.DeleteSheet("dtDataSheet")
End Function
Step 10: Run the above code and see if it runs without any errors.
Once this is done, you can create the remaining portions of the script in the same way shown above. Since
the code to fetch data from the data table is already implemented, you need to just add required columns in
the excel sheet and use the custom functions in the script to fetch those values.This was all about the basics
of Data Driven Framework approach where you can fetch the data from excel sheet through QTP Data Table.
We have also created the code for all the 3 test cases using this same approach. If you wish, you can
download the entire framework and use it as a reference in case you get stuck anywhere.
In this final part of the articles on QTP Data Driven Framework, we will cover the 3rd method of storing and
retrieving data, where the data would be saved in excel sheet and would be directly fetched from
excel sheet only (as opposed to the previous method where the data was first copied from excel sheet to
QTP Data Table and then fetched from the data table).
This article will be created on the same lines as the previous article on Data Driven Framework. So lets
directly start with what all will be covered in this article.
1) TC_01_GmailInbox: In this test script, we will login to GMail and then will find out the
number of unread emails in Inbox.
2) TC_02_GMailLabel: Here, we will again login to GMail and then create a new Label.
3) TC_03_GoogleSearch: In this test case, we will open Google home page, then search for
a string and will find out the number of results returned for that particular search.
Once you have identified the test cases, the data sheet format and the method using which you will fetch the
test data, you can start creating your test scripts.
Step by Step method to Create the First Test Case in the Framework
To explain the entire process in a detailed step by step manner, we will take up the Login to GMail portion of
the first test case and then explain it in detail. This would help you become familiar and comfortable with the
process. Once this is done, you can yourself create the remaining test scripts without any issues.
Step 1: Open QTP, create a new Test Case and save it at any desired location.
Step 2: Since this is the first test case of the framework, we will write the script for GMail login and hardcode the data in the script itself. Then we will run the code to verify that the functionality is working fine.
Once we have verified that the functionality is working fine, we can then write the code that will fetch data
from the excel sheet.
Step 3: Create an Object Repository and associate it with your test case. Add the required objects for GMail
login and then write the code. The code should look something like this 'Open GMail
SystemUtil.Run "iexplore.exe", "http://www.gmail.com"
'Page Sync
Browser("Gmail").Page("Gmail").Sync
'Login to Gmail
Browser("Gmail").Page("Gmail").WebEdit("UserName").Set "valid gmail login id"
Browser("Gmail").Page("Gmail").WebEdit("Password").Set "valid gmail password"
Browser("Gmail").Page("Gmail").WebButton("SignIn").Click
'Page Sync
Browser("Inbox").Page("Inbox").Sync
Run the above code and see if it is working fine.
Step 4: Now you have to create the data sheet to replace the hard-coded data with your custom function
that will fetch the data from the excel sheet.
To do so, create a new excel workbook and save it as DataSheet.xls inside your framework folder. (you can
give any meaningful name to the Workbook)
You would notice that the excel workbook already has 3 excel sheets Sheet1, Sheet2 and Sheet3, added to
it. Since we require only one data sheet in the workbook, delete sheets Sheet2 and Sheet3 from the excel
workbook.
Now rename Sheet1 to some meaningful name (say TestData). Your excel workbook will look something like
this.
Step 5: Once you have created the data sheet, the next step will
be to add data to this excel sheet. Add the data in the excel
sheet as shown in the below image. Please note that the
values in Column 1 should be same as the test case
name, so that QTP can pick up the data correctly based
on Test Case name.
Step 6: Now you will need to write the code that will fetch the
data from this excel sheet. The logic for fetching the data goes
like this.
i. QTP opens the excel sheet.
ii. QTP loops through the first column to read the test case name from each cell.
iii. QTP compares the test case name (from the excel) with the current test case name
(which is currently opened in QTP).
iv. Once a match is found on the test case name, QTP the loops horizontally to read the data
from the particular row where the test case name
matched.
Check the below code that will fetch data based on Test
Case name and Column Name.
Function fnGetDataFromExcel(strColumnName)
Dim iReqdCol
fnGetDataFromExcel = "<empty>"
'Open the Excel
Set oExcel = CreateObject("Excel.Application")
Set oWorkbook = oExcel.Workbooks.Open(sExcelPath)
Set oSheet = oExcel.Sheets("TestData")
'Find the total number of rows in the excel
iRows = oSheet.UsedRange.Rows.Count
iCols = oSheet.UsedRange.Columns.Count
'Find the column number from where data needs to be fetched
For iC = 2 to iCols 'Loop started from 2 because the first column is Test Case name
If oSheet.Cells(1, iC).Value = strColumnName Then
iReqdCol = iC
Exit For
End If
Next
'Loop through the rows to find the test case name
For i=2 to iRows 'Loop started from 2 because 1st row is header
If oSheet.Cells(i, 1).Value = sCurrentTestCase Then
fnGetDataFromExcel = oSheet.Cells(i, iReqdCol)
End If
Next
'Close the Excel
oExcel.Quit()
Set oExcel = Nothing
Set oWorkbook = Nothing
Set oSheet = Nothing
End Function
Step 7: As done in the previous article, you can create a custom set function that will first fetch the value
from the excel sheet and then it will populate the fetched value in required text boxes in the application. The
code for the custom function is shown below.