RSS Feed

QTP – Concepts Of DataTables

Posted on

DataTables in QTP
When you test your applications, you may want to check how the application performs the same operations with multiple sets of data.
Example:
suppose you want to check how your Web site responds to ten separate sets of data. You could create ten separate tests, each with its own set of data. But that isn’t feasible and code length is going to very large having approximately 1000 values. So writing same code lines for each value of test data is not a good practice.
Alternatively: You can create Data Table parameters so that your test runs ten times, each time using a different set of data.
DataTable is just like Microsoft Excel file. The DataTable contains a global sheet and a separate sheet for each action in the test. So we two types of sheets in data table.
1.) Global sheet
2.) Local sheet
Global sheet is available to all actions in the test and local sheet is available to the corresponding action.  The best practice is to use local data sheet while parameterizing the test.
Practical example of Data table in QTP
I am going to give you step by step procedure from scratch to end for how to use data table in your application, so you will be more clearer about data table in QTP.
Please go through following procedure.
1. Press “Record” button.
2. Go to start->run write http://google.com and click OK.
3. close the internet explorer window, you will have code like following.
Coding:
SystemUtil.Run “http://google.com”,””,””,””
Browser(“Google”).Page(“Google”).sync
Browser(“Google”).Close
4. Now put cursor on second sentence and do right click on that sentence.
5. Select “Insert output value”, you will have window like following select all three properties and click ok. By default all the values will go to global sheet. You can modify this setting by pressing “modify” button and selecting local sheet.
(Note: You can also redirect these values to environment table and you can access it. know more about environment variable in qtp and how to load and export environment variable in qtp ).
6. So you will have code like following.
Coding:
SystemUtil.Run “http://google.com”,””,””,””
Browser(“Google”).Page(“Google”).Output CheckPoint(“Google”)
Browser(“Google”).Page(“Google”).sync
Browser(“Google”).Close
7. So when you run the above script you can view three parameter and its value in global data sheet. If you are not able to view, please go to view->Data Table.
8. You can view values during test run, you can’t view that value after test is over.
9. You can export these values in external file with help of datatable.export command.
Put following sentence at last in your script
Datatable.export(“c:\qtp.xls”)
10. You can also use datatable.import command to import external excel file to your test in the start of test.
Types of DataTable:
Design Time DataTable:While creating editing the test scripts, you enter data directly in the data table  displayed there. You can view data table by selecting View >> Data Table option. This data table is called Design Time DataTable.

Runtime DataTable:

The data table used while running the test is called Runtime DataTable. The data in the runtime data table may be same as design time data table and/or can be entered by importing data from excel sheet, text file etc. The runtime data table can be seen in the Test Results window when run session is ended.

 DataTable Parameter:
Each column in global/local sheet of datatable is called a Parameter. We can rename the parameter just by double clicking on the column header and giving the name to it. Data can be entered in the column/parameter simply by clicking on the cell and entering the value.
Parameterizing the test:
Instead of using hardcoded values in the script, we can store the test data in the data table and give reference of same in the script which help perform the same operations on the AUT with multiple sets of data. This all is done through data table methods and properties.
DataTable Methods:
These methods are applied on either DataTable object or DTSheet object or DTParameter object. Here are the methods and their properties:
METHODS:
Add Sheet:
Adds the specified sheet to the run time data table.
DataTable.AddSheet(“My Sheet”)

DeleteSheet:
Deletes the specified sheet from the run time data table.
DataTable.DeleteSheet “My Sheet”
Export:
Exports the complete data table to the specified path.
DataTable.Export(“C:\abc.xls”)
ExportSheet:
Exports the current sheet to the specifed path.
DataTable.ExportSheet “C:\abc.xls”,1
GetCurrentRow: (for global sheet)
Retrieves the current row number of the global data sheet.
Row=DataTable.GetCurrentRow
Reporter.ReportEvent 1, “Row Number”, row
GetRowCount: (for global sheet)
Retrieves the number of rows in the longest column in the first sheet(global data sheet) in the run time data table.
Rowcount=DataTable.Get Sheet(“My Sheet”).GetRowCount
Reporter.ReportEvent 2, “There are” &rowcount, “rows in the data sheet”
GetSheet:
Retrieves the specified sheet in the run time data table.
DataTable.Getsheet(“My Sheet”).AddParameter(“Time”,”08:00”)
GetSheetCount:
Retrieves the total number of sheets in the run time data table.
Sheetcount=DataTable.GetSheetCount
Reporter.ReportEvent 0, “sheet number”, “There are” &sheetcount “sheets in data table”
Import:
Import the specified Microsoft Excel file to the run time data table.
DataTable.Import(“C:\abc.xls”)
ImportSheet:
Imports the specified sheet of Microsoft Excel file to the run time data table.
DataTable.ImportSheet”C:\abc.xls”, 1, ”Name”
SetCurrentRow:
Sets the specified row as the current(active) row in the run time data table.
DataTable.SetCurrentRow(2)
SetNextRow:
Sets the row next to the current row as the new current row in the run time data table sheet.

DataTable.GetSheet(“My Sheet”).SetNextRow

SetPrevRow:
Sets the row above the current row as the new current row in the run time data table sheet.
DataTable.SetPrevRow
PROPERTIES:
GlobalSheet Property:
Description: Returns the first sheet in the datat table.

DataTable.GlobalSheet.AddParameter “Time”,”08:00”

LocalSheet Property:
Description: Returns the current (active) sheet of the data table.
MyParam=DataTable.LocalSheet.AddParameter “Time”,”09:00”
RawValue Property:
Description: Returns the raw value of the cell in the specified parameter of the current row of the data table. The raw value is the actual string written in the cell before the cell is computed, such as the actual text from a formula.
FormulaVal=DataTable.RawValue(“Date”,”ActionA”)
Value Property:
Description: Data table default property. Retrieves or sets the value of the cell in specified parameter and the current row of the data table.
DataTable.Value(2,3)=”New York”
DTParameter – Methods

DtSheet.AddParameter:   (Adds a new column.)

DataTable.GetSheet(“dtGlobalSheet”).AddParameter “NewColumnName”,”FirstRowVaule”
DtSheet.GetParameter:
Description:  Retrieves the specified parameter from the run time data table sheet.
DataTable.GetSheet(“My Sheet”).GetParameter(“Destination”)
DtParameter – Properties
Name Property:
Description: Retrieves the name of the parameter in run time data table.
ParamName=DataTable.LocalSheet.AddParameter(“NewColumnName”,”Row1Value”).Name
RawValue Property:
Description: The raw value of the cell in the current row of the parameter in the run time data table. The raw value is the actual string written in the cell before the cell is computed, such as the actual text from a formula.
The following example uses the RawValue property to find the formula used in current row of the Date column in the ActionA sheet of the run time data table.
The statement below runs the Value:=Now()
FormulaVal=DataTable.GetSheet(“ActionA”).GetParameter(“Date”).RawValue
Value Property:
Parameter default property. Retrieves or sets the value of cell in the current row of parameter in the run time data table.
DataTable.GetSheet(“ActionA”).GetParameter(“Destination”).Value=”New York”
ValueByRow Property:
Retrieves the value of the cell in the specified row of the parameter in the run time data table.
DataTable.GetSheet(“ActionA”).GetParameter(“Destination”).ValueByRow(4)
DTSheet Methods
AddParameter Method:
Add the specific parameter(column) to the sheet in Run Time data table, sets the value of the first row to the specified value, and returns the parameter so that you can directly set or retrieve properties of new parameter in same statement.
Variable=DataTable.AddSheet(“My Sheet”).AddParameter(“Time”,”08:00”)
DeleteParameter Method:
Deletes the specified parameter from the sheet in run time data table.
DataTable.GetSheet(“My Sheet”).DeleteParameter(“Time”)
GetCurrentRow Method:
Returns the row number of the current(active) row in the run time data table sheet.
Rownum=DataTable.GetSheet(“My Sheet”).GetCurrentRow
Reporter.ReportEvent 1, “Row Number”, rownum
GetParameter Method:
Returns the specified parameter from the run time data table sheet.
DataTable.GetSheet(“My Sheet”).GetParameter
GetParameterCount Method:
Returns the total number of parameters(columns) from the run time data table sheet.
Paramcount=DataTable.GetSheet(“My Sheet”).GetParameterCount
Reporter.ReportEvent 2, “There are”, &paramcount, “columns in this sheet”
GetRowCount Method:
Returns the total number of rows in the longest column in the run time data table sheet.
rowcount=DataTable.GetSheet(“My Sheet”).GetRowCount
Reporter.ReportEvent 2, “There are”, &rowcount, “active rows in this sheet”
SetCurrentRow Method:
Sets the specified row as the current row in the run time data table sheet.
DataTable.GetSheet(“My Sheet”).SetCurrentRow(2)
SetNextRow Method:
Sets the row next to the current row as the new current row in the run time data table sheet.
DataTable.GetSheet(“My Sheet”).SetNextRow
SetPrevRow Method:
Sets the row above the current row as the new current row in the run time data table sheet.
DataTable.GetSheet(“My Sheet”).SetPrevRow
DTSheet Properties:
Name Property:
Returns the name of the run time data table sheet.
Sheetname=DataTable.LocalSheet.Name
Reporter.ReportEvent 1, “The active sheet is”, sheetname
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: