QTP tutorials & interview questions


Search more on QTP here

QTP Data Table Basics

Definition of QTP Data Table

The Data Table is a spreadsheet-like sheet with columns and rows representing the data applicable to your test.

Types of Data Table

Design-Time Data Table - The data your test uses is stored in the design-time Data Table, which is displayed in the Data Table pane while you insert and edit steps.

Run-Time Data Table - During the run session, QuickTest creates a run-time Data Table — a live version of the Data Table associated with your test. Data entered in the run-time Data Table during the run session is not saved with the test. The final data from the run-time Data Table is displayed in the Run-Time Data Table in the Test Results window.

Data Table has two types of data sheets — Global & Action

You store data in the Global tab when you want it to be available to all actions in your test and you want the data to control the number of test iterations.

You store data in the action's tab when you want to use the data in Data Table parameters for that action only and you want the data to control the number of action iterations.

Use of Data Table

The Data Table assists you in parameterizing your test.

Using Data Table parameters and/or output values in a test enables you to create a data-driven test or action that runs several times using the data you supply. In each repetition, or iteration, QuickTest uses a different value from the Data Table.

Where it is located when saved?

When working with tests, the Data Table is saved with your test by default. You can save the Data Table in another location and instruct the test to use this Data Table when running a test. You specify a name and location for the Data Table in the Resources pane of the Test Settings dialog box. You configure the location of the Data Table in Resources pane of the Test Settings dialog box (File > Settings > Resources node).

Other important points related to Data Table

The Data Table contains one Global tab plus an additional tab for each action in your test.

If you select an external file as your Data Table, you must make sure that the column names in the external Data Table match the parameter names in the test and that the sheets in the external Data Table match the action names in the test.

You can also import data into data table saved in Microsoft Excel, tabbed text file (.txt), or ASCII format. You can import data from a database by selecting a query from Microsoft Query or by manually specifying an SQL statement.

Is there any Data Table Object Model?

In QTP, three objects - DataTable (The run-time Data Table), DTParameter (A parameter (column) in a sheet in the run-time Data Table) and DTSheet (A sheet in the run-time Data Table) have different properties and methods to help you further manipulate the data in any cell.

[All methods apply to the run-time DataTable object only. Changes to the run-time DataTable object are reflected in the test results, but the design time Data Table is not affected.]

DataTable Object - Methods

AddSheet Method
Adds the specified sheet to the run-time Data Table.
e.g. Variable=DataTable.AddSheet ("MySheet").AddParameter("Time", "8:00")

DeleteSheet Method
Deletes the specified sheet from the run-time Data Table.
e.g. DataTable.DeleteSheet "MySheet"

Export Method
Saves a copy of the run-time Data Table in the specified location.
e.g. DataTable.Export ("C:\flights.xls")

ExportSheet Method

Exports a specified sheet of the run-time Data Table to the specified file.
e.g. DataTable.ExportSheet "C:\name.xls" ,1

GetCurrentRow Method
Returns the current (active) row in the first sheet in the run-time Data Table (global sheet).
e.g.
row = DataTable.GetCurrentRow
Reporter.ReportEvent 1, "Row Number", row

GetRowCount Method
Returns the total number of rows in the longest column in the first sheet in the run-time Data Table (global sheet).
e.g.
rowcount = DataTable.GetSheet("MySheet").GetRowCount
Reporter.ReportEvent 2, "There are " &rowcount, "rows in the data sheet."

GetSheet Method
Returns the specified sheet from the run-time Data Table.
e.g. DataTable.GetSheet ("MySheet").AddParameter "Time", "8:00"

GetSheetCount Method
Returns the total number of sheets in the run-time Data Table.
e.g.
sheetcount = DataTable.GetSheetCount
Reporter.ReportEvent 0, "Sheet number", "There are " & sheetcount & " sheets in the Data Table."

Import Method
Imports the specified Microsoft Excel file to the run-time Data Table.
e.g. DataTable.Import ("C:\flights.xls")

ImportSheet Method
Imports a sheet of a specified file to a specified sheet in the run-time Data Table. The data in the imported sheet replaces the data in the destination sheet (see SheetDest argument).
e.g. DataTable.ImportSheet "C:\name.xls" ,1 ,"name"

SetCurrentRow Method
Sets the specified row as the current (active) row in the run-time Data Table.
e.g. DataTable.SetCurrentRow (2)

SetNextRow Method

Sets the row after the current (active) row as the new current row in the run-time Data Table.
e.g. DataTable.GetSheet("MySheet").SetNextRow

SetPrevRow Method
Sets the row above the current (active) row as the new current (active) row in the run-time Data Table.
e.g. DataTable.SetPrevRow

DataTable Object - Properties


GlobalSheet Property
Returns the first sheet in the run-time Data Table (global sheet).
e.g. DataTable.GlobalSheet.AddParameter "Time", "5:45"

LocalSheet Property
Returns the current (active) local sheet of the run-time Data Table.
e.g. MyParam=DataTable.LocalSheet.AddParameter("Time", "5:45")

RawValue Property
Retrieves the raw value of the cell in the specified parameter and the current row of the run-time Data Table. The raw value is the actual string written in a cell before the cell has been computed, such as the actual text from a formula.
e.g. FormulaVal=DataTable.RawValue ("Date", "ActionA")

Value Property

DataTable default property. Retrieves or sets the value of the cell in the specified parameter and the current row of the run-time Data Table.
e.g. DataTable.Value (2,3)="New York"


DTParameter - Methods

DTSheet.AddParameter
'add a new column
e.g. DataTable.GetSheet("dtGlobalSheet").AddParameter "NewColumn","Row1Value"

DTSheet.GetParameter
Retrieves the specified parameter from the run-time Data Table sheet.
e.g. DataTable.GetSheet("MySheet").GetParameter("Destination")

DTParameter - Properties

Name Property
The name of the parameter (column) in the run-time Data Table.
e.g. paramname = DataTable.LocalSheet.AddParameter("Food", "pizza").Name
Reporter.ReportEvent 1, "The New Parameter name is", paramname

RawValue Property

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 a cell before the cell has been computed, such as the actual text from a formula.
The following example uses the RawValue property to find the formula used in the current row of the Date column in the ActionA sheet of the run-time Data Table. The statement below returns the value: =NOW()

FormulaVal=DataTable.GetSheet("ActionA").GetParameter("Date").RawValue

Value Property
Parameter default property. Retrieves or sets the value of the cell in the current (active) row of the parameter in the run-time Data Table.
e.g. 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.
e.g. DataTable.GetSheet("ActionA").GetParameter("Destination").ValueByRow(4)


DTSheet - Methods


AddParameter Method

Adds the specified parameter (column) to the sheet in the 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 the new parameter in the same statement.
e.g. Variable=DataTable.AddSheet ("MySheet").AddParameter("Time", "8:00")

DeleteParameter Method
Deletes the specified parameter from the sheet in the run-time Data Table.
The following example uses the DeleteParameter method to delete the parameter, "Arrival" from the "MySheet" sheet of the run-time Data Table.

DataTable.GetSheet("MySheet").DeleteParameter("Arrival")

GetCurrentRow Method
Returns the row number of the current (active) row in the run-time Data Table sheet.
e.g. row = DataTable.GetSheet("MySheet").GetCurrentRow
Reporter.ReportEvent 1, "Row Number", row

GetParameter Method
Retrieves the specified parameter from the run-time Data Table sheet.
e.g. DataTable.GetSheet("MySheet").GetParameter("Destination")

GetParameterCount Method
Returns the total number of parameters (columns) in the run-time Data Table sheet.
e.g. paramcount = DataTable.GetSheet("MySheet").GetParameterCount
Reporter.ReportEvent 2, "There are " &paramcount, "columns in the data sheet."

GetRowCount Method
Returns the total number of rows in the longest column in the run-time Data Table sheet.
e.g. rowcount = DataTable.GetSheet("MySheet").GetRowCount
Reporter.ReportEvent 2, "There are " &rowcount, "rows in the data sheet."

SetCurrentRow Method
Sets the specified row as the current (active) row in the run-time Data Table.
e.g. DataTable.GetSheet("MySheet").SetCurrentRow(2)

SetNextRow Method
Sets the row after the current (active) row as the new current row in the run-time Data Table sheet.
e.g. DataTable.GetSheet("MySheet").SetNextRow

SetPrevRow Method
Sets the row above the current (active) row as the new current (active) row in the run-time Data Table sheet.
e.g. DataTable.GetSheet("MySheet").SetPrevRow

DTSheet - Properties


Name Property
Returns the name of the run-time data sheet.
e.g. Sheetname = DataTable.LocalSheet.Name
Reporter.ReportEvent 1, "The Active Sheet is", Sheetname


(Source: QTP Guide)



Also See:

Reading values From Data Table

Writing Values To Data Table


How to import excel sheet into data table in QTP


Accessing Data Table through scripts

Multiple Choice Questions on Data Table

Table Checkpoints Multiple Choice Questions

Data Table Menu Shortcut Keys