Header Ads

How To Work With Datatable In QTP

If we are working on QTP for quite a few time the immediate question comes like how to parametrize my test? How to check a scenario with multiple sets of data.Unlike other automation tools QTP also provide data driven or data oriented concept called data-table to parametrize our tests.
So data book or databank is used to pass the inputs to the script and also to capture output or live data from the script during run time.so basically using of data table is required when we want to run a test multiple time with multiple data.
the basic example could be to test login module. In login we will provide several user id and password and test if it is working fine or not.

For every test by default there would be 2 sheets
1.Global
2.Action

just by clicking the different sheets we can acess these tables.
Global :-
Global sheet is neutral for all actions. If we want some data to be available all actions in our test, we will be using Global data.This is a default data table. So if you create your test and create your test data in excel, you can rename the test data excel file as global and paste it in the data table folder, those values will populate.
There are other ways too...
Action:
If we are using different actions,We can store data in the action's tab but scope is for current action only.
if your underlying data source is Excel , there might be some formula inbuilt in it. QTP has an ability to learn those formula and value from it.
So based on our scope for testing we might pass different datasource.

Datatables are of 2 types in Quick Test
1.Design Time Datatable:-The table that is associated with the test before and after the test execution. During execution this got transformed to runtime data
2.RunTime Datatable:- The table that is associated with the test during the test execution and will be present in the "Results" after test execution.A live dataset.

by default datatable will be stored by QTP under testname-->Datatable folder. If we want to use some other source , we need to code it accordingly.Where it is applicable?
say I am testing two different versions of the same application like-Webversion and Windows version. So the application data might be same but we need to use it from two different location.
Again say I am testing localization testing  of an application, So based on the location I need to pick some other datatable.Such cases this case comes handy.

Supported versions of data souce:
Excel-95
Excel-97
Excel-2000
Text files(.txt,.csv)
ASCII format
Even the zip files--
File-->import Test from Zip

Datatable object methods:   
AddSheet:-Adds the specified sheet to the run-time Data Table and it returns the sheet so that we can add properties to the sheet in the same statement.
Syntax:
datatable.addsheet("sheetname")
Example
datatable.AddSheet "sheet1"
How to set parameter/properties to it?
Syntax:
datatable.addsheet("sheetname").Addparameter(Col_Value,Value)
Example
datatable.GetSheet("Sheet1").AddParameter "Fly From",""
Next method will be deletesheet
This method will delete a specific sheet from the data table during run time.
Syntax:
datatable.DeleteSheet sheetName
Example
datatable.DeleteSheet"sheet1"
Export :-Saves the copy of the run-time datable in the specified location.
Syntax:-
datatable.Export ("location along with Excel name")
Eg:-
datatable.Export "d:\animesh.xls"
Note :- if the file is not created /present QTP will create the file at runtime and exports the specified sheet.


ExportSheet :- exports the specific sheet in the run-time datatable to An Excel file .
Syntax :-
DataTable.ExportSheet "location along filename", "sourcesheet"
Three things to know:
1. if the file is not present or created QTP will create the file at runtime and exports the specified sheet.
2. If the excel file is already created but there is no sheet associated with it. QTP will open this file and add the sheet.
3. Say if the file is already created and the sheet is already there, QTP will overwrite the sheets.

Example:

DataTable.ExportSheet "C:\animesh.xls","flight"
if we have a created a sheet called "flight"
also
DataTable.ExportSheet "C:\animesh.xls",1
if we want to export the first sheet


Import :- imports the specified Excel file to the run time Data Table
Syntax:-
DataTable.import(location+filename)
Example:
DataTable.import("C:\Test\animesh.xls")

Import method deletes/replaces all table available in datatable during run with its sheets.Remember sheet1 will be copied to Global,Sheet2 will be copied to Action. If you have more sheets in the source excel there will no Excel tab in QTP.So we need to create those tab by addSheet then only we will be able to get those extra sheets else those data is not accessable using this method.

It is a best practice to know the numbers of sheets presents in the source file and try to add those sheets during runtime.Then try to get those sheets in QTP.


Import Sheet :- To import the specific sheet in the run time data table.The data in the imported sheet replaces the data in the destination sheet
Syntax:-
DataTable.ImportSheet ("location+filename", "Source sheet",destination sheet) 
Example :-
DataTable.ImportSheet "c:\animesh.xls","flight","Global"
here also you need to create a sheet using datatable.addsheet,then only we will be able to put the flight to run time data.
GetCurrentRow :- returns the current row in the first sheet in the run-time Data-Table(Global sheet).if you are using BPT framework or business component driven approach

Syntax :
var x = DataTable.getCurrentRow
Example:
datatable.Import "c:\test\animesh.xls"
msgbox datatable.GetCurrentRow

GetSheetCount:-returns the total number of sheets in the run-time Data Table
Syntax:-
Var x = datatable.getsheetcount

GetSheet :-returns the specifed sheet from run-time Data Table
Syntax:- DataTable.GetSheet("abc")
GetRowCount :-Returns the total number of rows in the longest column in the first sheet in the run-time sheet in the data table(Global sheet)
Syntax:- DataTable.GetSheet("abc").getRowCount
datatable.AddSheet "abc"
'this will add the temp sheet during run time
datatable.ImportSheet "c:\test\animesh.xls","flight","abc"
'this will import the specific sheet flight to abc
msgbox datatable.GetSheet("abc").getRowcount
'this will give the row count of abc
SetCurrentRow :-set the specified row as the current(active) row in the run-time Data Table
Syntax:-
DataTable.SetCurrentRow(value)
example:-
DataTable.setCurrentRow(2)
For the above written cases datatable needs to have atleast 1 row. It will try to set that is having a valid data row
SetNextRow:-Set the row after the current(active) row as the new current row in the run-time Data-Table
Syntax:-
DataTable.GetSheet("abc").setNextRow
setPrevRow:-set the row above the current(active) row as the new current(active)row in the run-time Data Table
Syntax:-
DataTable.SetPrevRow




Powered by Blogger.