Search code examples
vbscriptqtphp-uftuft14

HP UFT/QTP 14.00, import CSV and maintain the values in data sheet


i'm importing some data from a csv file, here is the data:

*file.csv
UserName, EmailId, PhoneNumber
Antonio, [email protected], 1234567890
Oscar, [email protected], 9999999999
Luis,[email protected],8888888

I have a Function to call this file:

'************************************************************
Function ImportCsvFiletoDatatable(CsvFilePath,SheetName,HeaderDelimiter)
Dim filePath
Dim fso
Dim f
Dim fData
Dim arrData
Dim CsvValue
Dim CsvSheet
Dim CsvFirstLine
Dim CsvColumns
Dim ColumnIndex
Dim rIndex
Dim cIndex

filePath=CsvFilePath    'Specify file Path

'Open CSV File using File System Object
Set fso=createobject("scripting.filesystemobject")
Set f  = fso.OpenTextFile(filePath)

CsvFirstLine=f.readline    'Treating like first line is the column names

CsvColumns=split(CsvFirstLine,HeaderDelimiter)    'Split the line using HeaderDelimiter

Set CsvSheet=DataTable.GetSheet(SheetName)    'Get the Specified sheet

'Add the splitted values as Datatable Columns
For ColumnIndex=lbound(CsvColumns)  to ubound(CsvColumns)
CsvSheet.addparameter CsvColumns(ColumnIndex),""
Next


While not f.AtEndOfStream

rIndex=f.Line-1    'Specify Row index
fData=f.ReadLine    ' Read CSV File Line
arrData=split(fData,",")    'Split the line
cIndex=1    'Specify Column Index
CsvSheet.SetCurrentRow(rIndex)    'Set Row in the Datatable

' Add values in Datatable
For Each CsvValue In arrData
CsvSheet.getparameter(cIndex).value=CsvValue
cIndex=cIndex+1
Next

Wend

f.Close
Set fso=Nothing

End Function
'************************************************************

And works well, but the information is volatile, and i can't manage, or use the data. Someone know how to keep the data in the data sheet, although leave UFT?


Solution

  • Dim objQtApp, strXlsPath
    strXlsPath = Environment("TestDir") & "\Default.xls"
    Set objQtApp = CreateObject("QuickTest.Application")
    DataTable.Export strXlsPath
    objQtApp.Test.DataTable.Import strXlsPath
    Set objQtApp = Nothing
    

    The Design Time DataTable is found in the Default.xls. This is loaded when you open a test case or if you edit it manually from UFT. In case you want to refresh it programmatically use the code-snippet above. Export and then with AUtomation Object Import.

    Of course put it into a method and call from whatever place is convenient for you.

    If you want UFT to take care of it automatically, Create a new Class and a singleton Instance of it.

    Implement the Class_Terminate method of the class and put the code there. WHenever UFT exits either because of a crash or or a nomral test run ends, it will try to clean up all Objects created while running. This object will be among them, and as a part of the automatic cleanup process you will save your runtime datatable into the design-time one(Default.xls) and then reload it.