UFT – how to read CSV file stored in QC Test Resources

  • Questions
  • UFT - how to read CSV file stored in QC Test Resources
Question ID: 105307

We do not have MS Excel on the systems where we execute our QTP automated tests (IT won’t give us Office licenses). We’ve decided instead to use CSV files to data drive our tests. That’s not an issue using the Filesystem object. However, we’d like to store these CSV files in QC’s Test Resources module and access them at test runtime. How do I programmatically open a file from within my QTP test when that file is stored the QC Test Resources module?

Marked as spam
Posted by (Questions: 25, Answers: 12)
Asked on February 11, 2014 9:18 pm
Answers (2)
Private answer

OK, I found some code on the Internet that uses Quality Center's OTA API, but it doesn't seem to be working. Below is the code I am running in UFT 11.53. Attached is a screenshot of what I have in QC Test Resources module. When I run this, no errors are being thrown, but the file never gets copied into C:Temp. The problem appears to be that the filter returns 0 items. Can anyone spot what the issue is?

Function QCGetResource(resourceName,saveTo)
Set qcConn = QCUtil.QCConnection
Set oResource = qcConn.QCResourceFactory
Set oFilter = oResource.Filter
oFilter.Filter(''RSC_FILE_NAME'') = resourceName
Set oResourceList = oFilter.NewList
If oResourceList.Count = 1 Then
Set oFile = oResourceList.Item(1)
oFile.FileName = resourceName
oFile.DownloadResource saveTo, True
End If
Set qcConn = Nothing
Set oResource = Nothing
Set oFilter = Nothing
Set oFlieList = Nothing
Set oFile = Nothing
End Function

QCGetResource ''csvfile'',''C:Temp''

![alt text][1]

[1]: /storage/temp/173-2014-02-11_20-39-02.png

Marked as spam
Posted by (Questions: 25, Answers: 12)
Answered on February 12, 2014 2:45 am
Private answer

The code above works, but file name must be the same as the File name as listed on the Resource Viewer tab in ALM.

Here's the rest of the code to get the .csv contents into the DataTable (borrowed from http://www.qtpsudhakar.com/2009/07/import-csv-file-data-in-to-qtp.html):

ImportCsvFiletoDatatable ''c:Tempcsvfile.csv'',''Action1'','',''

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),''''

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
Set fso=Nothing
End Function

Marked as spam
Posted by (Questions: 16, Answers: 749)
Answered on February 12, 2014 1:45 pm