Data Driven Framework in QTP : The Complete Guide – Part 1
In the last couple of articles, we covered QTP Linear Framework (and its two main limitations) and saw how QTP Modular Framework can overcome one of these limitations. In this article, we will see how you can use Data Driven Framework in QTP to overcome the 2nd limitation (data re-usability) of the linear framework approach.
Before we begin, let us see what all will be covered in this article.
UPDATES
1) You are free to download and play around with the code used for this framework. The download link is available at the end of the article.
2) Just like this, we have written more articles on various other QTP Frameworks. If you wish to have a look at those, please visit QTP Framework main page. The bottom of the page contains links to other QTP Frameworks.
Topics that will be covered in this article are –
- 1) What is Data Driven Framework and what are its components.
- 2) What are the different sources from where you can use data in your test scripts.
- 3) What are the different ways to extract test data from Microsoft Excel in QTP.
- 4) What is the approach that will be used in this article.
- 5) Sample Scripts.
- 6) Few small assignments to help you practice and understand the concepts clearly.
- 7) Advantages and Disadvantages of the approach covered in this article.
What is Data Driven Automation Framework?
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 is the basic approach used to create a Data Driven Framework?
If you would have noticed in the previous articles on Linear and Modular Automation Frameworks, the test scripts were written in a way where the test data was “hard-coded” with the test script. i.e. the data and the script were together as a single unit. Now try to superimpose this approach on the scenario (checking multiple login credentials) we discussed above. If you were to use this approach on the above scenario, you would need to change the test data in the script after each run which is not such a good idea because the basic premise of automation is “you have to aim for least amount of manual intervention possible”.
There is one more important point to be considered here. You would have noticed that QTP experts are involved in creating the test scripts and many a times manual testing team is given the job of running the scripts for regression and other purposes. Mostly they would not be having much idea about how to use QTP and hence it will be difficult for them to go through the code and change the test data. This process might leads to some errors also if things are not done properly. Because of this, “code + data” together as a one unit doesn’t work out very well.
So now lets come back to the point. What is the approach to create a data driven framework? In simple terms, all you have to do is to just separate your code and data. And your Data Driven Framework is ready. You would have to store your data in some external files (such as text files, excel, database etc) and figure out a way to read the data from this external source and pass it to your application through some variables. That’s all..
Components of Data Driven Framework in QTP
In order to create a Data Driven Framework in QTP, you need to have the following components available.
1) Test Script & Object Repository: The Test Script and the Object Repository (if you are not using complete Descriptive Programming) reads the data from the external sheet and then uses it on the application under test.
2) External Data Source: There will be an external data source that will contain all the test data that you need to run your test script.
Figure below shows the pictorial representation of the data driven framework in QTP.
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.
Different ways to extract data from Microsoft Excel in QTP
QTP (and VBScript) provides you with 3 ways using which you can extract data from an excel sheet. These 3 methods are –
Method 1 – Using QTP And Data Table. Here, you can save all your data in QTP data table and write the script which will extract data from the data table.
Method 2 – Using QTP, Data Table and MS Excel. In this method, the QTP first copies the data from the excel sheet into QTP data table. It then uses the QTP in-built methods to read data from the data table.
Method 3 – Using QTP and MS Excel. Here the data is stored in MS Excel and QTP uses EXCEL COM methods to read the data directly from the excel sheet only.
The approach used in this article
In this article, the script that we will create will use the first method shown above. That is, we will create a test case and save the data in the QTP Data Table. Then we will write the script that will read the data from the data table and use it in the application (The remaining two methods which use MS Excel will be covered in the next part of this article).
Sample Script for Data Driven Framework
Sample Script: In this article, we will create a script that will check the Login functionality of windows based Mercury Flight Reservation application. Instead of hard-coding the data in the code, we will store the data in QTP Data Table and write code in QTP to take data from the data table itself. Also, we will provide multiple rows of data in the data table and instruct QTP to run the test script for all the rows in the data table. For example, if the data table contains 10 rows of data, the script will also execute 10 times, that is, one time for each row.
Let’s see how to implement this in a step by step manner.
Step 1: Create a New Test Case in QTP and Save it at any desired location.
Step 2: In the test case, verify if Data Table is visible at the left bottom of the screen. If not, select View -> Data Table from the QTP menu. Data Table should now be visible at the left bottom of the screen (Refer image below).
Step 3: Click on the Data Table to open it. This would open the empty data table as shown in the below figure.
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.
Step 7: Now is the time to enter some data in the datatable. Enter some valid user credentials in the first row. Don’t forget to save the test case after changing the data. Figure below shows the snapshot of the datatable after adding single row of data.
Please note that there are two sheets available in the datatable – Global and Action1, and we are adding the data in the Global sheet.
Step 8: Once we have the data ready, its time to write the code that will fetch the data from the datatable. To fetch the value from the DataTable, we will use DataTable.Value command. If you want to fetch the username from “Global” sheet, the command will be var = DataTable.Value(“UserName”, “Global”). When you run this statement, value “anish” will be stored in variable “var”.
Let’s see the entire script that will –
a) Open Flight Reservation Login screen.
b) Enter user id and password.
c) If Login is successful, QTP will close the application.
d) If Login is unsuccessful, then also QTP will close the application.
In this example, we will use descriptive programming approach without adding any objects to the Object Repository. To learn more about descriptive programming approach in QTP, you can read Descriptive Programming in QTP Part1 and Part2.
'Open Flight Reservation Application SystemUtil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4a.exe" 'Enter UserId and Password Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set DataTable.Value("UserName", "Global") Dialog("text:=Login").WinEdit("attached text:=Password:").Set DataTable.Value("Password", "Global") 'Click on Ok button Dialog("text:=Login").WinButton("text:=OK").Click 'Check if Login is Successful or Not 'Check if error message is displayed If Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(4) Then 'Click on Ok button to close the dialog Dialog("text:=Login").Dialog("text:=Flight Reservations").WinButton("text:=OK").Click 'Click on Cancel to Close the Login Screen Dialog("text:=Login").WinButton("text:=Cancel").Click Reporter.ReportEvent micFail, "Login Unsuccessful", "" Else 'Login is successful. close the application Reporter.ReportEvent micPass, "Login Successful", "" Wait(1) Window("text:=Flight Reservation").Close() End If
Step 9: Now we have run the code with a single row of data. Let us now add more rows to the data table to run the code for all these data. Refer the below screenshot where we have added total 5 rows in the data table.
Step 10: To run the test case for all the rows in the datatable, you can use one of the following 2 methods –
Method 1: In this method, we don’t make any change in the code, but change the QTP settings to run the test cases as many number of times as the number of rows in the DataTable. To change this setting, select File -> Settings -> Run (from the left hand side pane) and select the Radio button “Run on all rows”. This would instruct QTP to run the script for all the rows in the DataTable. Refer the below screenshot for the setting.
If you now run the above code, you would see that the script is run 5 times automatically with different set of data during each run.
Refer the below screenshot for the test result. Here you can note that the iterations 2 and 3 have failed because of invalid login credentials.
By the way, if you are not aware of the login credentials for the flight application, it is: Login Name – ‘any name 4 or more characters long’ and password – mercury
Method 2: In this method, you don’t change to setting in QTP to “Run on all rows”. Instead you modify the code so that it takes the data from each row in the datatable using a loop. For this method, again select File -> Settings -> Run and select the radio button – “Run one iteration only”. Now we have to add a loop to our code so that it can run on all the 5 rows of data. Check the modified code below which will run on all the rows in the DataTable.
iRows = DataTable.GetRowCount 'Loop through all the rows For iR = 1 to iRows 'Set the current row DataTable.SetCurrentRow iR 'Open Flight Reservation Application SystemUtil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4a.exe" 'Enter UserId and Password Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set DataTable.Value("UserName", "Global") Dialog("text:=Login").WinEdit("attached text:=Password:").Set DataTable.Value("Password", "Global") 'Click on Ok button Dialog("text:=Login").WinButton("text:=OK").Click 'Check if Login is Successful or Not 'Check if error message is displayed If Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(4) Then 'Click on Ok button to close the dialog Dialog("text:=Login").Dialog("text:=Flight Reservations").WinButton("text:=OK").Click 'Click on Cancel to Close the Login Screen Dialog("text:=Login").WinButton("text:=Cancel").Click Reporter.ReportEvent micFail, "Login Unsuccessful", "" Else 'Login is successful. close the application Reporter.ReportEvent micPass, "Login Successful", "" Wait(1) Window("text:=Flight Reservation").Close() End If Next
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?
Advantages and Disadvantages of using DataTable as a Data Source
Advantages
- 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 can’t be used for complex frameworks where you need to reuse the same set of data for different test cases.
- b) This method also doesn’t work with modular framework where the test data needs to be grouped based on the functions rather than test cases.
Assignments
Assignment 1: If you would noticed in the test script, the url of the application is hard-coded in the script. Add the url in the DataTable and change the code accordingly so that all the data is taken from the data sheet only.
Assignment 2: This test case had used the “Global” sheet to add the data. Can you say what changes we need to make in the script to take data from the “Action1” sheet? Also, what is the difference between “Global” and “Action1” sheet?
Assignment 3: To retrieve data from the data table, we have used DataTable.Value( “ColumnName”, “SheetName”) method. Can you list down the other ways that can be used to retrieve data from the DataTable.
Assignment 4: Compare the results of the 2 methods we used above and let us know why is the result format different.
You can add your responses to the above questions in the comments section.
This was all about Data Driven Framework that uses DataTable to store the test data. In the next article we will have a look at the method where the data is retrieved from excel sheets.
Whats your thought on this article? Let us know your comments in the comments section.
If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.
Visit QTP Frameworks Main Page for more articles on QTP Frameworks. You can also visit our QTP Tutorials page for more QTP Tutorials.
Pingback: QTP Frameworks – An Introduction - Automation Repository - Automation Repository()
Pingback: Selecting Data Sheet format for a Data Driven Framework in QTP - Automation Repository - Automation Repository()
Pingback: Designing Data Driven Framework in QTP – Part 2 - Automation Repository - Automation Repository()
Pingback: Designing Data Driven Framework in QTP – Part 3 - Automation Repository - Automation Repository()
Pingback: Basics of Keyword Driven Framework in QTP - Automation Repository()
Pingback: QTP Framework Basics | QTP Framework()
Pingback: complete real time scripts of qtp « TESTING HUB()
Pingback: Designing Hybrid Framework in QTP - Part 3 - Automation Repository - Automation Repository()