DataTable in UFT One | Example of Datatable Methods

Likewise, Microsoft Excel testers can read input data dynamically at runtime from UFT DataTable to parametrize input fields. Datatable in UFT makes it very easier for testers to create data-driven test cases that can be used to run an Action multiple times. By default, each action in UFT has its own Datatable but it can be accessed from any action within a test. Datatable is loaded with amazing functionalities and it’s up to you how you leverage its benefits in your automation test scripts. There are two types of DataTable in UFT.

  • Local DataTable − Each action has its own local DataTable, which can also be accessed across actions. Whenever an action is created UFT adds a local sheet for the specific action. To know more about action please refer to my post on actions Everything about Actions in UFT.
  • Global DataTable − Each test has one global data sheet that is accessible across actions.

The DataTable is accessible from the Data Tab of the lower pane in UFT.

DataTable in UFT

The automation test script is executed as many times as many records are there in the datatable for that particular action. It means if there are 10 records in the datatable of an action of a Test, that action will be executed 10 times. Usually, we do not want this to happen. It is can be controlled by changing UFT Run Settings. Go to File -> Settings -> Run (Tab) as shown below and set run from row data row and to data row to 1.UFT Datatable Run Settings

You can write repeated steps inside a loop in the test script and iterate the datatable in UFT to read all rows.

Operations On UFT DataTable

UFT provides three types of objects to access and perform various kinds of operations on a UFT Datatable. 

UFT Datatable Operations

Sr.NoObject Type And Description
1Data Table Object.
Provides methods to perform operations on run-time data table object only.
2DTParameter Object Methods.
Provides methods to perform operations on parameter (column) in a sheet in the run-time data table.
3DTSheet Object Methods.
Provides methods to perform operations on a sheet in the run-time data table.

DataTable Object

DataTable Object Methods

The following methods are associated with Datatable Object.

Method NameDescriptionSyntax
AddSheetAdds the specified sheet to the run-time data tableDataTable.AddSheet(SheetName)
DeleteSheetDeletes the specified sheet from the run-time data tableDataTable.DeleteSheet SheetID
ExportExports the Datatable to a new file in the specified locationDataTable.Export(FileName)
ExportSheetExports a Specific Sheet of the Datatable in run-timeDataTable.ExportSheet(FileName,SheetName)
GetCurrentRowReturns the active row of the run-time data table of global sheetDataTable.GetCurrentRow
GetRowCountReturns the number of rows in the run-time data table of Global SheetDataTable.GetRowCount
GetSheetReturns the specified sheet from the run-time data table.DataTable.GetSheet(SheetID)
GetSheetCountReturns the total number of sheets in the run-time data table.DataTable.GetSheetCount
ImportImports a specific external Excel file to the run-time data table.DataTable.Import(FileName)
ImportSheetImports the specified sheet of the specific excel file to the destination sheet.DataTable.ImportSheet(FileName, SheetSource, SheetDest)
SetCurrentRowSets the Focus of the Current row to the Specified Row NumberDataTable.SetCurrentRow (RowNumber)
SetNextRowSets the focus of the next row in the run-time data tableDataTable.SetNextRow
SetPreviousRowSets the focus of the previous row in the run-time data TableDataTable.SetPrevRow

Few Examples of  Datable Object Methods

Consider the following Table for Datatable for the below examples.

UFT-One-Datatable-example

AddSheet: The method is used to add a sheet at runtime. Once the execution gets over runtime added sheets won’t be available for any use.

Output:

Sheet1 will be added to the UFT Datatable. You can use the DeleteSheet method to delete it at runtime.


GetSheet:
It returns the specified sheet from the run-time data table.

GetRowCount: It returns the total number of rows in the longest column of a sheet in the data table. The following example uses the GetRowCount method to find the total number of rows in the longest column(the column having maximum records) of the MySheet run-time datasheet and displays it on the message box.

GetSheetCount: It returns the total number of sheets in the run-time data table.

SetCurrentRow: It sets the specified row as the current (active) row in the run-time data table. The first row is numbered 1. The following example set the current row value to 2 and retrieve the value from Environment column for the second row and displays its value in the message box.

DataTable Object Properties

The following properties are associated with Datatable Object.

Property NameDescriptionSyntax
GlobalSheetReturns the first sheet of the run-time data table.DataTable.GlobalSheet
LocalSheetReturns the Active local sheet of the run-time data table.DataTable.LocalSheet
RawValue
Retrieves the raw value of the cell
DataTable.RawValue ParameterID, [SheetID]
ValueRetrieves the value of the cell in the specified parameter.DataTable.Value(ParameterID, [SheetID])
Few Examples of  Datable Object Properties

Consider the following DataTable.

Datatable.value-in-uft-one

Value: How to read data from DataTable in UFT

The following example returns the Global Sheet, sets the row value to two, and gets the value of the Amount column.

Value: How to set a value into the UFT DataTable Sheet at runtime

The following example returns the Global Sheet, sets the row value to two, and sets the value “D5340” in the ItemNo column.

RawValue: How to read raw value from DataTable in UFT

The following example retrieves the raw value of the cell in row number two in the Amount column.

DTParameter Object

DTParameter Object Properties

Property NameDescriptionSyntax
NameReturns the name of the parameter in the run-time datatable.DTParameter.Name
RawValueReturns the raw value of the cell in the current row of the runtime datatable.DTParameter.RawValue
ValueRetrieves or sets the value of the cell in the current (active) row of the parameter in the run-time datatable.DTParameter.Value
ValueByRowRetrieves the value of the cell in the specified row of the parameter in the run-time datatable.DTParameter.ValueByRow (RowNum)
Few Examples of  DTParameter Properties
Name: How to get Column name of UFT DataTable

The following example will add a parameter in the local sheet and return the value that has been given to the parameter.

ValueByRow: How to retrieve the value of the cell in the specified row using the parameter name

DTSheet Object

DTSheet Object Methods

Method NameDescriptionSyntax
AddParameterAdds the specified parameter (column) to the sheet in the run-time data table and sets the value of the first row to the specified valueDTSheet.AddParameter(ParameterName,Value)
DeleteParameterDeletes the specified parameter from the sheet in the run-time data table.DTSheet.DeleteParameter(ParameterID)
GetCurrentRowReturns the row number of the current (active) row in the run-time Data pane sheet.DTSheet.GetCurrentRow
SetCurrentRowSets the specified row as the current (active) row in the run-time data table.DTSheet.SetCurrentRow(RowNumber)
GetRowCountReturns the total number of rows in the run-time DataTableDTSheet.GetRowCount
GetParameterRetrieves the specified parameter from the run-time Data pane sheet.DTSheet.GetParameter(ParameterID)
SetNextRowSets the row after the current (active) row as the new current row in the run-time Data pane sheet.DTSheet.SetNextRow
SetPrevRowSets the row above the current (active) row as the new current (active) row in the run-time Data pane sheet.DTSheet.SetPrevRow
GetParameterCountReturns the total number of Columns in the run-time DataTable.DTSheet.GetParameterCount
Few examples of DTSheet Object Methods
AddParameter: How to add a new Column in UFT DataTable at runtime

The following example will add a new sheet MySheet and add a new parameter to it

GetParameterCount: How to count Column numbers in UFT DataTable

The following example will count the number of columns in a UFT DataTable Sheet.

DTSheet Object Properties

Property NameDescriptionSyntax
NameReturns the name of the run-time data sheet.DTSheet.Name
Example: How to get active sheet name from UFT DataTable at runtime

The following example uses the Name method to return the name of the active run-time datasheet.

Recommended Posts

Leave a Reply