Monday 21 October 2013

QTP DataTable



->This is one of the resources for passibg dynamic values into QTP parameters


Global Datasheet -> The variable value which is used for all Actions

Action Datasheet -> The variable value which is used for only seperate Actions.

Rename Datatable Parameter name -> select the column which we want to change the parameter name->and double click for change the name

Datatable type :

-> Design time datatable  -> we give the input to the input parameter
-> Runtime datatable -> we can see the ouput

once the QTP run ends, its stored the results in Runtime datatable. So if we want to see the result goto
'Automation -> Results -> Run time datatable - to see the reults

The run time table is not saved in our design time datatable. we can see the result in our local system in below path = E:\arunraj\qtp\datatable1\Res3\Report\Default.xls




Script for Adding two values from datatable

datatable.GetSheet(1)        'here Sheet id 1 for global, 2 for Action 1 and so on.
GetRowcount1 = datatable.GetRowCount
For i=1 to GetRowcount1
    datatable.SetCurrentRow(i)
    in1=datatable.Value(1,1)         ' Parameter ID = 1 mean for input 1, and Parameter id =2 for input 2 and so on.  Sheet ie =1 for Global
    in2 = datatable.Value(2,1)
    datatable.Value(3,1)=cint(in1)+cint(in2)     ' Value from datatable is String, so we convert into Integer using cint function
Next

' The results are stored in runtime data table. its not stored in design time datable in below. So if we want to see the result goto
'Automation -> Results -> Run time datatable - to see the reults








Result from run time table ( Automation -> Results -> Run time datatable - to see the reults)









Script for Read the values from Excel and Store the values into the Excel

DataTable.Import("File Name") = Read the Values from Excel

DataTable.Export("File Name") = Insert the Values into the  Excel


Note: The Excel file should have the older version (.xls). The new version is not support under QTP.


Create the new Excel and save into "C:\Users\user\Desktop\Action Image\Datatable1.xls"




Extract all rows from above Excel and insert into datatable in QTP.then below method are same as which we are using in datatable


The Script is

datatable.Import("C:\Users\user\Desktop\Action Image\Datatable1.xls")  'Extract all rows and insert into datatable , then below method are same as which we are using in datatable
datatable.GetSheet(1)
GetRow = datatable.GetRowCount
For i=1 to GetRow
    datatable.SetCurrentRow
    in1=datatable.Value(1,1)
    in2=datatable.Value(2,1)
    datatable.Value(3,1)=cint(in1)+cint(in2)
Next
datatable.Export("C:\Users\user\Desktop\Action Image\Datatable2.xls)




Then the results are stored into datatable2 Excel by using Export method


  



Delete the row from datatable

-> IF we select row and press delete button, the row from datatable is not permanantly deleted. The rows are still in datatable
-> if we want to delete the row from datatable use below method
    a) select row and press CTRL+K
    b) select row -> Edit-> delete


Working with datatable

QTP supports 3 objects named

-> Datatable
-> DTParameter
-> DTSheet

Script 1:

write program to import excel sheet to data table

Datatable.import("D:\EmpData.xls")


Script 2:

Write pgm to read data from the first parameter of global sheet

datatable.Import("E:\arunraj\notes\QTP\Example.xls")
Set dsGlobal = Datatable.GetSheet("Global")
set pmfirst = dsGlobal.GetParameter()
ds_Global_Row_Count = dsGlobal.GetRowCount()
print(pmfirst.Name)
for i=1 to ds_Global_Row_Count
print(pmfirst.ValueByRow(i))
Next



Script 3

Write pgm to read data from all sheets and all parameter of excel file

datatable.Import("E:\arunraj\notes\QTP\Example.xls")
Set countsheetobj=datatable.GetSheetCount()
For sheet=1 to countsheet1
    Set currentsheetobj = datatable.GetSheet(sheet)
    Set paramcount=currentsheetobj.GetParameterCount
    Set rowcount = currentsheetobj.GetRowCount
    print (currentsheetobj.Name)
    For row=1 to rowcount
        rowdata=""
        For param=1 to paramcount
            Set currentparam=currentsheetobj.GetParameter(param)
            rowdata= rowdata & vbtab & currentparam.ValueByRow(row)
        Next
    print(rowdata)
    Next
 Next



Script 4

Write pgm to insert data into the sheet in excel.

datatable.Import("E:\arunraj\notes\QTP\Example.xls")
Function PlaceDate(SheetName,ParamName,RowNumber,DataToAdd)
Set ParamObj= Datatable.GetSheet(SheetName).GetParameter(ParamName)
paramobj.ValueByRow(RowNumber)=DataTOAdd
End Function

PlaceData("Action1","Sheet1","5,"Demo USer")




Datatable using Hybrid Approach :(Using the DataTable by coding)

We are mixing the Readymade environment provided by QTP and Programmetic procedure



Datatable type :

-> Design time datatable  -> we give the input to the input parameter
-> Runtime datatable -> we can see the ouput. This is carbon copy of Design time dataTable


Script


1) Create Sheet at run time dable.

 By default Runtime DataTable have two sheets Global and Action1. So if we are add any sheet it placed from third sheet.

Syntax: DataTable.AddSheet "Sheet Name"

DataTable.AddSheet "login"

The sheet login placed in third place.

2) Import test data from an External File

Syntax  :  DataTable.importSheet "file name",Sheet from local Excel Source file, Sheet number from Destination DataTable Run time table

DataTable.importSheet "c:\login.xls",1,3

File Name = c:\login.xls
1 = Sheet number from Source File
3= Sheet number from Destination DataTable Run time table.



3) Syntax for DataTable ()

Syntax: DatatTable(Column number of Sheet from datatable, Sheet number of Datatable)

Ex: DataTable(2,3)

We can access the data from Third Sheet and all data from Second column in Data Table



2 comments:

  1. spectacular....
    datatable topics is drafted and organised very well..
    thx a ton..

    ReplyDelete
  2. Thanks for a marvelous posting! Want to improve your spacebar clicking speed? Visit this profile on space bar clicker. Spacebar clicker helps you count your spacebar clicking speed in a given time.

    ReplyDelete