我正在从csv文件导入一些数据,这里是数据:
*file.csv
UserName, EmailId, PhoneNumber
Antonio, anto@gmail.com, 1234567890
Oscar, osc@yahoo.com, 9999999999
Luis,lu@hotmail.com,8888888
我有一个函数来调用这个文件:
'************************************************************
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
'************************************************************
效果很好,但信息不稳定,我无法管理或使用数据 . 有人知道如何保留数据表中的数据,尽管离开UFT?
1 回答
Design Time DataTable位于Default.xls中 . 打开测试用例或从UFT手动编辑时会加载 . 如果您想以编程方式刷新它,请使用上面的代码片段 . 导出然后使用AUtomation对象导入 .
当然把它放到一个方法中并从任何方便你的地方打电话 .
如果您希望UFT自动处理它,请创建一个新类及其单例实例 .
实现类的Class_Terminate方法并将代码放在那里 . 无论何时由于崩溃或者名义测试运行结束UFT退出,它都会尝试清理运行时创建的所有对象 . 此对象将在其中,作为自动清理过程的一部分,您将运行时数据表保存到设计时(Default.xls),然后重新加载它 .