0

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

*file.csv
UserName, EmailId, PhoneNumber
Antonio, anto@gmail.com, 1234567890
Oscar, osc@yahoo.com, 9999999999
Luis,lu@hotmail.com,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?

Dave
  • 4,328
  • 2
  • 24
  • 33
  • What are you actually trying to do with the data? What do you mean you can't use the data? - and how is that "working well"? There are several ways to open a CSV. If you open it manually does it work? – ashleedawg Jul 05 '18 at 23:17
  • So is it VBA or VBS? Did `DataTable` (whatever it is) changed at the end? Functions are designed to return something. – PatricK Jul 06 '18 at 06:38
  • Once you have loaded all the values into your `Datatable` if you want them to persist outside the script, you need to `Datatable.Export` them to a file. – Dave Jul 06 '18 at 08:12
  • Thanks @Dave, i´am now trying to import the CSV into the datatable, i can use the data but... when the iteration close, all the data is erased. – Antonio Barazarte Jul 06 '18 at 16:01
  • Yes, because it's a runtime data table. If you export it as I mentioned, it will be available to be loaded or reviewed outside of UFT – Dave Jul 06 '18 at 16:05
  • Ok perfect, thanks a lot @Dave and God bless you! I'm having Other problem, the script load the data into a datasheet different to global perfectly, but when i load the data to Global datasheet this do 4 iterations, and i don´t understand why. – Antonio Barazarte Jul 06 '18 at 16:09
  • Your test will be configured to run for all iterations of the datatable. Go into your test properties, select Run properties and choose "one iteration only". And technically, you should have asked a further question for that rather than adding it to this one... – Dave Jul 06 '18 at 20:02
  • Thanks @Dave, I did it!!! and my apologies for the question inside the comment. – Antonio Barazarte Jul 06 '18 at 20:39

1 Answers1

0
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.

Bela Tamas Jozsa
  • 714
  • 5
  • 10