site
stats
By Anish Pillai Anish Pillai Posted under QTP Framework | QTP Tutorials for Beginners

Data Driven Framework in QTP : The Complete Guide – Part 1

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

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

QTP Data Driven Framework Approach

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.

Data Driven Framework Components 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 –

Working with Data Tables and Data Sheets in QTP


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

Data Table in QTP

Step 3: Click on the Data Table to open it. This would open the empty data table as shown in the below figure.

Data Table in QTP

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.

Changing Column Header in QTP Data Table

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.

Changing Column Header in QTP Data Table

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.

Add singe row of data in QTP Data Table

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.

Multiple Rows in 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.

Run on all rows

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.

Run Results for Multiple Iterations

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.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Primal

    Hi I like the way you are explained things…… very well said..

    • Anish10110

      Thank you Primal.. 🙂

    • really its great way to explain

      • Anish10110

        Thank you Vishnu.. 🙂

  • Pingback: QTP Frameworks – An Introduction - Automation Repository - Automation Repository()

  • ram

    awesome

  • ram

    where is the next part2

    • Anish10110

      Thanks Ram.. 🙂
      I am working on the other part also. And I will post it as soon as it is finished.

  • Amit

    Dear and Respected Anish,

    you did a great help to community. Great thanks.

    • Anish10110

      Thank you Amit.. 🙂

      • vikas

        Anish, Could you please explain me the concept of Excel handling or
        you any blog for that. Pls let me know.

        Thanks in advance
        Vikas

  • 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()

  • Abhi

    Its really great.

  • Debasis

    thanks

  • Rekha

    Excellent work, I appreciate your dedication and time to help people, who want to learn advanced QTP like Frameworks.

    Keep the good working, also do post other frameworks like Keyword and Hybrid.

    • Anish10110

      Hello Rekha, Thank you for the feedback.. I would soon try to post articles on Keyword and Hybrid Frameworks too.. 🙂

  • Pingback: Basics of Keyword Driven Framework in QTP - Automation Repository()

  • swathi

    Nice explanation
    Thanks a lot

    • Anish10110

      Thank You Swathi.. 🙂

  • Nalini

    thanks …….it is a grt help to the community

    • Anish10110

      Thank You Nalini.. 🙂

  • Praveen

    Hi,

    Very excellent explanation on frameworks. thank you for your great effort. can u mail me the code for all framework concepts?

    Thanks lot… keep it up…

  • Kapil

    Thanks Anish, Really your explanation is very helpful for me…….

    • Anish10110

      Thank you Kapil.. 🙂

  • Abhinov

    thanks Anish….could u please post the solutions for the Assignments too…

    • Anish10110

      Abhinov, I have not worked on the solutions yet. I will start working on it and will post it once it is done.

  • Abhinov

    assignment4: for the 1st case Passed:7(its counting System Util as passed) and failed :3 for 2nd case passed:2 failed :2…but i dont understand the difference..in the results..why are we getting this

    • Anish10110

      I would work on all the assignments and will update this also. Once it is done, I will update the details.. 🙂

  • subhan

    Excellent work anish. Thanks a Ton for this Valuable and easily understandable information….

    • Anish10110

      Thank You Subhan.. :):) Keep Visiting.. 🙂

  • eshan

    Hi , I couldn't understand this statement :
    Advantages

    a) The main advantage of this method is that it is very each to script for taking data from the datatable.

    Please elaborate.
    Thanks
    Eshan

    • Anish10110

      Eshan, Thats a typo.. Its 'easy' and not 'each'.

  • Pingback: QTP Framework Basics | QTP Framework()

  • guru

    Please let me know how to insert the text in the data table by scripting

    • Anish10110

      You can do it at runtime using DataTable("<columnName>", <sheetID>).Value = "some value".
      eg – DataTable("A", 1).Value = "Anish". But this data will only be available till the time the test is running. Once the test run finishes, the data also gets deleted. If you want you can export the data into an excel sheet using DataTable.Export command.

      Is this the answer you were looking for??

  • Murthy

    hi all , if you want to see hybrid driven framework. please visit <a href="http://www.pavanworld.net&quot; target="_blank">www.pavanworld.net</a>. this site explains the hybrid driven framework.

  • Forhad

    This is very help, explanation and samples are very clear and precise. Thanks again for all your help!!! great job!!

    • Anish10110

      Thank You.. 🙂

  • Pingback: complete real time scripts of qtp « TESTING HUB()

  • Dhivya

    Answer for Assignment 2:

    Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set DataTable.Value("Username", "Action1")
    Dialog("text:=Login").WinEdit("attached text:=Password:").Set DataTable.Value("Password", "Action1")

    QTP will execute the script with respect to only the particular action and not globally.

  • Dhivya

    Assignment 3:

    We can use parametrization method of QTP to retrieve data from the data table.

    Is this answer right Anish? Correct me if I am wrong.

  • Dhivya

    Assignment 4 Answer:
    In the first method, 5 iterations were run and so test results shows results iteration wise.
    In the second method, only one iteration was needed to run all the 5 rows as we used the loop concept in the script.

  • Dhivya

    Dear Anish,

    Please post the answer for assignment 1.

  • Prabhakar gudapati

    Hi Anish,

    i learn QTP long back 3 years back, now i want to brush up my QTP skills and trying but i feel every thing is new and unknown, then i tried to view some QTP blogs fortunately i found your blog, it makes me feel very good and feeling like classroom training, with your wonderful explanation and many examples, I suggested your blog for my friends who are trying to learn QTP.
    I like your patience to prepare such a great explanations and you responsibility to make viewers satisfied (with your reply for each comment)

    • Anish10110

      Thanks a lot Prabhakar for this feedback… 🙂 It really means a lot.. 🙂

  • Anitha Dwivedi

    Hello Anish

    Thank u for this kind of detailed explanations, Its really like undergoing sessions on QTP. Very very informative and easy to understand. I was working on QTP 4 yrs back, but since its really long time, i had forgot many a thing and was hesitant to face interviews with QTP openings. ur blog is an excellent one to go through and now i am feeling confident again to face interviews. Thank u Anish. Please keep ur good work going, it will help many people like me.

    Also request you to post the answers for the assignments. It will be helpful.

  • Sachidananda

    HI anish its really mindblowing explanation.i was struggling for this framework only but after going through your tutorial its all clear. Thanks.

  • shreyu

    Hi anish,
    thank you soo much for ur wonderful explination..

  • Charu

    Hi Anish

    Thanks for the post. It is really very helpful.I am a new learner of QTP and it is easily understandable.

    • Anish10110

      Cool.. 🙂

  • Sedhu

    Useful and Reader friendly artical

    • Anish10110

      Thanks.. 🙂 Keep visiting..

  • Ravi

    Thank you very much for all the explanation. Great work..

    • Anish10110

      Thank you Ravi.. 🙂

  • Pingback: Designing Hybrid Framework in QTP - Part 3 - Automation Repository - Automation Repository()

  • Nitin Sharma

    U r a great explainer . U have explained very complex thing in a simpler way.

  • jij

    This is very good..easily understand by the beginners

  • SREENATH S M

    var1=Datatable("Parameterid","sheetid")is one method.another is var2=Datatable.value("parameterid","sheetid")

  • Raj

    Awesome Anish..!! Please keep posting..!!

  • pooja

    Hi Anish,
    Can you please also post an article where the values are taken from an external excel sheet?

  • Nilesh

    Really Great work…Anish…

  • Raj

    HI Anish,

    Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(4)

    What does exist(4) means here? 4 charecters?

    -Raj

    • rajni

      It waits 4 seconds to verify window exist.

  • ImAnAfrican

    Thanks a lot really helpfull

  • Rajendra

    Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(4)

    Kindly elaborate this….specifically…Exist(4)

    • abc

      Exist(4) will wait for 4 sec to check if dialog box is open.

      • Bey Melamed

        Isn't it more accurate to say "will wait up to 4 seconds"? This was my impression (I am under the impression QTP would return immediately if element was found immediately)

  • vishal brahmwar

    hi i want a code which show popup"already in use " when multiple user access the exelsheet

  • vishal brahmwar

    hi anish i want to know the code for qtp which show popup"already in use " when multiple user access the exelsheet

  • Yogesh

    Hi Anish

    I am new to QTP. I found your post very helpful to understand Data driven Testing.
    But i have One question. Please provide answer.
    I have to automate a web application. which has below steps.
    1. Open a Url on browser.
    2. Enter Login Name and password.
    3. Click on specific page(a Link).
    4. On that page we have to enter Emp. Name, Email Id and Phone.
    5. Click on Ok button.

    Now i have to automate the steps 3, 4 and 5 stpes with different data values.
    I dont want to repeate the 1st and 2nd step. how to do that. plase give me a code for this(Loop condition).

    Thanks,
    Yogesh

  • Ulelu

    Ulelu Ulelu AAla Baala Ulelu

  • Deepti

    Thanks very much..

  • rameshbabu

    Hi can u suggest some more website for qtp learning
    my id is br24_chowdary@yahoo.co.in

    bchowdhary4@gmail.com

  • Guest07

    It's not clear why the test fails if you cannot login ("Login Unsuccessful") with invalid credentials. The system should not let you in if your password or user name are invalid. So, if it works as designed why should the test fail? Thanks

  • Srinath S Rao

    Hi, I have few queries on QTP/ UFT Frameworks. My project requirement is on multiple Signin and Signout's, where in the log-in person performs some action to take the "job" posted to the final level. I have prepared 100% descriptive program/ script in an action in order to automate. I am not much confident about "Framework Design"..

    Now, I am creating functions on the "Raw Descriptive Code" that was prepared earlier (as mentioned above). Please let me know how should I proceed further in order to meet the Standard Framework Expectations. Also, I am thinking to include:- Screen shots of error page in case of error/ deviation.

    Please help me on this and let me know how to prepare Frameworks in detail..

  • Anshuman Sharma

    from 3 ways described to export data from excel in application which method is better and why. what is diadvantage from excel to datatable and then into application in comparison of direct excel to application

    • Vish Saxena

      In my opinion, keeping the Test Data in External file and import it as UFT Datasheet is the best way to get advantage of below:
      1. By keeping Text data in external file, Tester who don't use UFT, can also update the test data.
      2. UFT calls to it's own sheet is faster compare to call external sheet, this delay become significant of multiple(say 100s+) calls.

  • Alex Makhalane

    Very Helpful and straight to the point. Thanks 🙂

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×