TestComplete Data Driven Automated Testing – Read CSV file data.


The below function reads data from any csv file and pass array of row values to another splitting function for decomposition.  

Sample:

Function Util_Readcsv(StrFilename)

  Dim innerIndex
  Dim objFSO  
  Dim objTextFile
  Dim strnextLine
  Dim fileArray()
      

  Set objFSO = CreateObject("Scripting.FileSystemObject")
 
  If not objFSO.FileExists(StrFilename) Then

    Call Log.Error("The File Path " & StrFilename & " cannot be found.") 
  
  Else

    Set objTextFile= objFSO.OpenTextFile(StrFilename, 1)
    innerIndex = 0
    Call Log.Message("The File Path " & StrFilename & " was Found.")  
   
    Do Until objTextFile.AtEndOfStream
        Redim Preserve fileArray(innerIndex)
        strnextLine = objTextFile.Readline 
        fileArray(innerIndex)=strnextLine
        innerIndex = innerIndex + 1
   
Loop  
               
     Util_Readcsv = fileArray 
     
  End If
 
End Function
(Note: this code based on VB scripts)
Function Steps:
1.  Save file path as constant in main function and pass to above < Util_Readcsv(StrFilename)> function.

2.  Create a file object for Read Functionality.
E.g. Set objFSO = CreateObject("Scripting.FileSystemObject")

3.  Verify file object exist for Read Functionality.
e.g.

If not objFSO.FileExists(StrFilename) Then
    Call Log.Error("The File Path " & StrFilename & " cannot be found.") 
    Else

4.  Open file object and read file values starting from second line (since first line considered as “Field title” row) and split in to rows and assign to array.

5.  Now, pass all splitted row values to decomposition.

E.g. Util_Readcsv = fileArray   


6.  All row values can be decomposed to field values by using following code sample.

Sample:

      'Call Util_Readcsv function for read values from CSV file
      FileArray = Util_Readcsv(FILENAME)         
       
      'Read Array Raw Values

      For outerIndex = 1 to UBound(FileArray)  
         
      Call Util_LogFolderstack(outerIndex)
                               
      strnextLine = FileArray(outerIndex)      
splitString = Split(strnextLine, ",")  'new line as the delimiter,  Split returns an array
              
        'Assigning field values each row in CSV file to array        
         for innerIndex = 0  to UBound(splitString)
         Redim Preserve arrArray(innerIndex)
         strText  = splitString(innerIndex)
         arrArray(innerIndex) = strText
next
       

Comments

0 Responses to "TestComplete Data Driven Automated Testing – Read CSV file data."

Post a Comment