Saturday, February 9, 2013

Working With Data Table

AddSheet: This method is used to add a new sheet to the data table.
Syntax: DataTable.AddSheet(“Sheet Name”)
Eg: DataTable.AddSheet(“MySheet”)
Note: All the methods of data table object will have impact only during Runtime

DeleteSheet: This method is used to delete a sheet from the data table.

Syntax: DataTable.DeleteSheet(“” or )
Eg: DataTable.DeleteSheet(“Global”)
(OR) DataTable.DeleteSheet(1)

Import: This method is used to import the contents of an excel file into data table. While importing from an excel file we need to ensure the following things:
1.The number of sheets in excel file & in data table should be equal or more.
2.The sheet names may not be same. (We can modify the sheet names after importing)
3.The parameters names should be same.
Syntax: DataTable.Import(“”)
Eg: DataTable.Import(“D:\QTP\input.xls”)
Steps to import the data manually:
1.Right click --> Import from file --> Click on OK
3.Select the file name from which the data needs to be imported
4.Click on Open

ImportSheet: This method is used to import the data from the specific sheet of the excel file to the specific sheet of the data table.
Syntax: DataTable.ImportSheet “”,
Eg: DataTable.ImportSheet “D:\QTP\input.xls”,2,1

Steps to import manually:
1.Right click in the required sheet of data table
2.Select Sheet --> Import --> From File --> Click on OK
3.Select the required file from which the data needs to be imported
4.Select the required sheet name from the dropdown
5.Click on OK

Export: This method is used to export the contents of the data table to an excel file. Even if the file is not available, it will create the file & export. If the file is available it will over ride.
Syntax: DataTable.Export(“”)
Eg: DataTable.Export(“D:\QTP\output.xls”)

Steps to export manually:
1.Right click in the data table
2.Select File --> Export
3.Provide the File Name --> Click on Save

ExportSheet: 
This method is used to export the contents of a specific sheet of the data table to the excel file.
Syntax: DataTable.ExportSheet “”,
Eg: DataTable. ExportSheet “D:\QTP\output.xls”,2

Steps to import manually:
1.Right click in the required sheet of data table
2.Select Sheet --> Export
3.Provide the File Name --> Click on Save

GetCurrentRow: This method is used to identify the current active row number
Eg: ActRow=DataTable.GetCurrentRow
msgbox ActRow

GetRowCount: This method is used to get the number of records available in the data table.
Eg: RowCnt=DataTable.GetRowCount
msgbox RowCnt
Note: Methods like GetCurrentRow, GetRowCount, etc defaultly works on global sheets.


LocalSheet: 
This method is used to work with the corresponding local sheet from which action we execute the script.
Eg: RowCnt=DataTable.LocalSheet.GetRowCount
msgbox RowCnt

GetSheetCount: This method is used to get the number of sheets available in the data table.
Eg: SheetCnt=DataTable.GetSheetCount
msgbox SheetCnt

SetCurrentRow: This method is used to set the focus to the specified row of the ‘data table-global sheet’.
Syntax: Datatable.SetCurrentRow()
Eg: Datatable.SetCurrentRow(17)

SetNextRow: This method is used to set the focus to the immediate next row of the current active row.
Syntax: DataTable.SetNextRow

SetPrevRow: This method is used to set the focus to the previous row of the current active row.
Syntax: DataTable.SetPrevRow

Value: This method is used for 2 purposes.
1. To read the value from the data table.
Syntax: Val=DataTable.Value(“”,“”)
Eg: Val=DataTable.Value(“Num1”,2)
msgbox Val
Note: We will not define the row number. By default it will pick the value of the current focus row.

2. To write the value into the data table.
Syntax: DataTable.Value(“”,“”)= “Value”
Eg: DataTable.Value(“Result”,”Action1”)=“Pass”
(OR) DataTable.Value(3,2)=“Pass”

Script to perform DDT manually:

DataTable.Import("D:\Practise\QTP Testing\input.xls")
RC=DataTable.GetRowCount
msgbox RC
For i=1 to RC
DataTable.SetCurrentRow(i)
VbWindow(“VbWindow”).VbEdit(“VbEdit”).SetDataTable.Value (“num1”,1)
VbWindow(“VbWindow”).VbEdit(“VbEdit_2”).SetDataTable.Value(“num2”,dtGlobalSheet)
VbWindow(“VbWindow”).VbButton(“Add”).Click
ExpVal=Datatable.Value(“ExpRes”,1)
ActVal=VbWindow(“VbWindow”).VbEdit(“VbEdit_3”).GetROProperty(“text”)
DataTable.Value(“ActRes”,1)=ActVal
If ExpVal=ActVal Then
DataTable.Value(“Result”,1)=”Pass”
Else
DataTable.Value(“Result”,1)=”Fail”
End If
Next
DataTable.Export(“D:\Practice\QTP Testing\output.xls”)

1 comment: