site
stats
By Anish Pillai Anish Pillai Posted under QTP Concepts | QTP Framework

Designing Data Driven Framework in QTP – Part 2

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

Background


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.



Before we begin with designing data driven framework in this article, you may want to check articles this and this for understanding the basic concepts of a data driven framework such as –

  • – What is a Data Driven Framework.
  • – What is the structure or Architecture of a Data Driven Framework.
  • – What are the different Data Sources that you can use with a Data Driven Framework in QTP.
  • – How can you use some common data sources in QTP.
  • – Sample scripts that show how you can create a basic data driven framework in QTP using QTP Data Tables.


What we will be doing in this article

1) As part of this article, we will be creating 3 test scripts that will work on Google and GMail.

2) All these 3 test cases will need some sort of data that will be populated in the application (Google or GMail). Instead of directly hard-coding the data in the test scripts itself (as we had done in QTP Linear Framework and Modular Framework Tutorial), we will save all the test data in the excel sheets and then write some custom functions that will pick up the required data from the excel sheets and then use them in the application.

3) In this way, we will be creating a basic data driven framework in QTP that will comprise of 3 test cases and will retrieve the data from the excel sheets.

Identifying Test Cases for this Framework

The first step for creating the framework is to identify the test cases that we will be automating as part of this framework. Below is the list of the test cases that we will be working on here.

1) TC_01_GmailInbox In this test script, we will log in to GMail and then will find out the number of unread emails in Inbox.

2) TC_02_GMailLabel In this test case, we will again log in to GMail and then create a new Label in GMail.

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.

Identifying the format of the Excel Sheet

After you have identified the test cases, you will need to do some analysis to find out what all data would be needed for these test cases and then figure out the appropriate data sheet format best suited for these test cases.

For this article we will be working on the data sheet format where each test case will have its own excel sheet. Since we have 3 test cases here, we will be using 3 excel sheets all saved in a single excel workbook. In short – 1 excel workbook having 3 sheets.

Identifying how you will fetch data from Excel Sheet

Once you have identified the format of the data sheet, you need to decide on the method that you will use to fetch data from the excel sheet. The method that we will use as part of this article is –

Step 1: QTP opens the excel workbook and copies the required data sheet.

Step 2: QTP then pastes the contents of this sheet into QTP Data Table.

Step 3: QTP uses its own functions to read the data from the Data Table.

How to Fetch Data from Data Sheet

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

The relatively tougher part here is to write the first script or module and then modify it as per your framework. Once you have created your first script (as per framework) it becomes very each to write the remaining modules or test scripts.

This is because most of the important code such as connecting your test case with the data sheet and fetching the data would already have been done in the beginning itself. Once it is done, you only need to call these functions wherever required.

As an example, we will cover the Log in portion of GMail in detail here. Once you understand the concept, you can then build the rest of the code easily 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, you can first write the script with hard-coded data. Then you run this code to verify that the functionality is working fine. Once you confirm that the functionality is working fine, you can write the code to that would fetch data from the data sheet.

Step 3: Create an Object Repository and associate it with your test case. Add the required objects and then write the code that will log in to GMail. 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 for this test case. To do this, create a new excel workbook and save it as DataSheet.xls inside your framework folder. (you can give any meaningful name to the Workbook)

Step 5: Open the Workbook. You would find that there are 3 sheets in the Workbook – Sheet1, Sheet2 and Sheet3. You need to provide some meaningful name to the sheet that you are going to use. As a best practice, you can keep the same name for the test case and the excel sheet.

Since we are writing code for TC_01_GmailInbox, you can rename Sheet1 as TC_01_GmailInbox as shown below.

Adding Data Sheet in Excel Workbook

Step 6: The next step is to add data to ‘TC_01_GmailInbox’ sheet. From the above code you can notice that we have hard-coded 3 parameters in the script. These are GMail URL, User ID and Password. We would now add all these parameters in the data sheet. Add the data in the data sheet as shown in the below image.

Adding Data to Excel Data Shee

Step 7: Now you have to write the code that will fetch data from the data sheet and then put it in the application. Check the below function that copies the excel data into QTP Data Table and then returns the value based on the column name.

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

How QTP Fetches Data from Data Sheet

Step 8: Since this is a reusable function that will be re-used across all the 3 test cases, you should add this function in an external function library and then associate that function library to your test case. (For more info on this, read the 4th method in how to associate function library to a QTP script).

Step 9: Once you have associated the function library to your script, you can modify the main script so that now it takes data from the data sheet. The modified code is shown below –

'Get the Test Case Name that will be used in fnGetDataTableValue function
sTestCaseName = Environment.Value("TestName")

'Open GMail
sUrl = fnGetDataTableValue("URL")
SystemUtil.Run "iexplore.exe", sUrl
'Page Sync
Browser("Gmail").Page("Gmail").Sync

'Login to Gmail
sUserName = fnGetDataTableValue("UserID")
Browser("Gmail").Page("Gmail").WebEdit("UserName").Set sUserName
sPassword = fnGetDataTableValue("Password")
Browser("Gmail").Page("Gmail").WebEdit("Password").Set sPassword
Browser("Gmail").Page("Gmail").WebButton("SignIn").Click

'Page Sync
Browser("Inbox").Page("Inbox").Sync

The above code is the most basic way you can implement this concept. There is still room for a lot of improvement in the code. If you notice the code, you would see that we are first capturing the value from the Data Table in a variable and then we are then using it with the Set function (Lines 11-12 & Lines 13-14). This is not a good programming practice because we are using 2 lines of code to perform a small action.

So to make the code bit more readable, you can create a custom Set function, that will first fetch the value from the Data Table and then it will put it in the application. This can be achieved by using the concept of function overriding in QTP. The custom Set function would look something like this.

Function fnSetValue(objControl, strColumnName)

  Dim sReqdValue

  'Get value from DataTable
  sReqdValue = fnGetDataTableValue(strColumnName)

  objControl.Set sReqdValue

End Function

Since this function is also a reusable function, you have to add it to the function library and bind it to the WebEdit control. Your final code (for the login portion of the script) should look something like this.

'Get the Test Case Name that will be used in fnGetDataTableValue function
sTestCaseName = Environment.Value("TestName")

'Open GMail
sUrl = fnGetDataTableValue("URL")
SystemUtil.Run "iexplore.exe", sUrl
'Page Sync
Browser("Gmail").Page("Gmail").Sync

'Login to Gmail
Browser("Gmail").Page("Gmail").WebEdit("UserName").fnSetValue "UserID"
Browser("Gmail").Page("Gmail").WebEdit("Password").fnSetValue "Password"
Browser("Gmail").Page("Gmail").WebButton("SignIn").Click

'Page Sync
Browser("Inbox").Page("Inbox").Sync


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.

If you have any feedback or comments about this article, do let us know about it using 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 ×
  • Pingback: Designing Data Driven Framework in QTP – Part 3 - Automation Repository - Automation Repository()

  • Pingback: QTP Framework Tutorials - Framework Types, Examples & Code Snippets - Automation Repository()

  • Dolly

    Hi Anish,

    The articles are very informative and helpful.

    The value of Environment.Value("TestName") is giving the xls name when placed a msgbox in the script.The value of sTestCaseName in the function was showing a blank value. So, it was giving an error for me.

    So, I have changed the variable "sTestCaseName" in the function "fnGetDataTableValue" with "TC_01_GmailInbox" while importing data from Excel sheet(DataTable.ImportSheet) and it worked fine.

    Can you please let me know if there is any other way to get the Test Sheet name in the above mentioned step or if i have missed anything?

    Thanks.

    • Anish10110

      Hello Dolly,

      Environment.Value("TestName") is giving the xls name. That is fine because both the sheet name and the test case name are same. So its showing the correct thing only.

      Value of sTestCaseName should show the correct value in the function. Don't know why it is behaving that way. I will check it once from my end. Also if possible, can you please email me with the screenshot of the exact place where this error is occurring? You can email me at anish[at]automationrepository[dot]com.

      Also, if you take replace the value "sTestCaseName" in "fnGetDataTableValue" with "TC_01_GmailInbox", whats actually happening is you are hardcoding a value in the function library. And this function will be used in all the test cases. Now if you want to run the 2nd test case, you would have to again change the test case name in the function, else it will fail.

      So the only option is to pass the value of sTestCaseName from the function itself. Try debugging the code and see at what place the value gets blank. You can also email me or drop in a comment here in case it is not working.

  • Abhinov

    hi Anish,
    while importing sheet ..i m getting error as:

    The DataTable.ImportSheet operation failed. Invalid file.

    Line (7): "DataTable.ImportSheet "C:UsersAbhiDesktopdata.xlsx", sTestCaseName, "dtDataSheet"".
    I have tried hardcoding the Sheet name as suggested by Dolly ..but its not helping..

    • Anish10110

      Hi Abhinov,

      Try to use the excel sheet in xls format. Save the data sheet in Excel 97-2003 format and then use data.xls in your code. Try this method out and see if it works. Let me know if it still doesn't work.

  • Abhinov

    Hi Anish,
    I have tried both the methods..but still no success…

    • Anish10110

      Hi Abhinov,

      Would it be possible for you to email me your queries? Because I can try to explain things to you using images and all (if needed). This can't be done here in comments. You can email me at anish@automationrepository.com

  • Abhinov

    I have deleted the file and created a new file and it worked..
    Thanks

    • Anish10110

      Cool.. 🙂

  • Abhinov

    HI Anish,
    sorry for Trouble again….
    ..I m now try to to Associate Function Libraries to your QTP Scripts..i m using the 1st method for the same..I have created a TXt file containg the function:fnGetDataTableValue,but I m getting error…question is here …how will
    sTestCaseName = Environment.Value("TestName")

    sTestCaseName be passed to the function ….
    when I hard coded the value of sTestCAseName..it worked for me…Plz help…Hope u got my point..

  • Anish10110

    Abhinov,

    For the error, can you drop in an email to me with the screenshot?? My email id is anish@automationrepository.com

    Regarding how sTestCaseName will be passed to the function, well this can be done when you declare the variable in the beginning of the function library. When you do so, the variable will 'get' a global scope and thus will be available to all the functions in the test case and function library. Please let me know if you have any more doubts regarding this.

  • Minu

    If somebody ask me how you are using DDF in your project then what i have to say ?Plz help me

    • Anish10110

      Hi Chetna,

      Basically we have to tell how data driven framework concepts are actually implemented in the framework. For example, you can

      1) give an overview about how the data is being organized in excel sheets. Is all the data saved in a single sheet or multiple sheets, whether test data is grouped based upon functions or based upon test cases etc.

      2) How can also mention how the data is being fetched. Like whether is it taken directly from excel sheets. Or first imported into QTP Data Table and then used in the application.

      3) You can also add where you are actually storing data.. in excel sheets, txt files, some database etc. Also the reason for doing so.

  • Arun Balachandran

    After running the below code

    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

    i am getting the following error

    The retrieve DataTable.Value operation failed. The <URL> column does not exist.

    Function file: C:QTP_SAMPLE_SCRIPTS_Nov3_12keyword driven frameworkexcelTest1retrieve value.qfl
    Line (11): " fnGetDataTableValue = DataTable.Value(strColumnName, "dtDataSheet")".

    But i have already made URL column in the excel file.

    same error is repeated for other colums also

    Can you please help to solve the above probelm

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

  • Anuj Malik

    I am also getting run time error below

    The retrieve DataTable.Value operation failed. The <URL> column does not exist.
    Function file: C:TutorialGetDataTableValue.qfl
    Line (15): " fnGetDataTableValue = DataTable.Value(strColumnName, "dtDataSheet")".

    I basically downloaded your code and made changes to it based on login information for Gmail. Can you please help….I will also send you the code to your email address.

  • Anil

    I am getting the following run time error

    Run Error
    The Data Table.ImportSheet operation failed. Specify the sheet using a text string or numerical index value.

    I am using HP Unified Functional Testing 11.5.
    Please help me with the error

  • Sanjukta

    Nice article. How do i modify the function fnGetDataTableValue to fetch data from excel if there is more than 1 row of data in the excel?Presently with the existing function QTP runs two iterations (2 rows of data in excel) but only with the first row of data.

  • Amrutha Suneesh

    I am getting an error while running the command DataTable.ImportSheet sExcelLocation, "Sheet1", "dtSheet". The error is General error occurred while importing sheet dtSheet from file D:VBDemoFlowsheet.xls to sheet Sheet1.
    I am using HP Unified Functional Testing 11.5 and Excel2007. Please help me with this error

  • Naresh

    Hi

    Anish ,

    I am getting datatable problem .i wrote some data in excel file and importing into internal excel of QTP by using Datatable.importsheet .but no data is copying into that file .what isthe problem???? i am writing code as
    Option explicit

    Dim oex,owb,ows

    Set oex=createobject("excel.application")

    oex.Visible =true

    Set owb=oex.Workbooks.Add

    Set ows=owb.Worksheets("sheet1")

    ows.name="data"

    ows.cells(1,1)="username"

    ows.cells(1,2)="password"

    ows.cells(2,1)="naresh"

    ows.cells(2,2)="mercury"

    owb.SaveAs("C:data.xls")

    datatable.ImportSheet "C:data.xls","data",1

    …….But no data is copying into the internal excel where was the problem please respond to this msg …

    • sandeep

      is the destination datatable sheet name is 1 ???

  • Sanj

    For me function was not getting sTestCaseName value, so i added code "sTestCaseName = Environment.Value("TestName")" in the function itself then its working.
    Is it correct?

  • Pingback: Designing Data Driven Framework in QTP – Part 3 | Tech Lessons()

  • Guest07

    fnGetDataTableValue is used 3 times in the above script. fnGetDataTableValue "copies the excel data into QTP Data Table and then returns the value based on the column name." Does that mean that the above script
    "copies the excel data into QTP Data Table" 3 times?

  • Guest07

    Are there any advantages of coping data into QTP Data Table
    comparing to getting data directly from excel file? Thanks.

  • amey24

    Hi Anish,

    I didnt understood

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

    what is sTestCaseName here

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