Forum Discussion

WillClarke's avatar
WillClarke
Occasional Contributor
6 years ago
Solved

Downloading and looking at a file

I am having difficulties attempting to script running a report, then downloading the report from the test site in order to veridy a set of values is on the file. Does anyone have a best practice or a...
  • TanyaYatskovska's avatar
    6 years ago

    Hi WillClarke,

     

    The following article contains an example of how you can download a file from the Internet:

    https://support.smartbear.com/viewarticle/8999/

     

    As the formats of the file are different, you will need to create (or use existing) different approaches to read the files as there is no one common way. Just review the following articles demonstrating how you can work with txt, xml and excel file from TestComplete:

    Working With Microsoft Excel Files

    Working With XML Files From Scripts

    Reading Text Files

     

    The pseudocode will look like:

    //download the file
    
    //check its format and read
    switch (<File_Format>){
      case "xml": 
        // read an xml file
      case "txt":
        // read a txt file
    ....
    }
    
    
  • WillClarke's avatar
    WillClarke
    6 years ago

    Thanks for the answers, just putting this code snippet here incase someone wants to not have to look through multiple links

     

    This is the code I used to check to see if a csv file has a value for a specific column. Luckily with the update to TC 14, the CSVDriver was super easy to use, Also you will most likely need to download 

    Microsoft Access Database Engine 2010 Redistributable here -> https://www.microsoft.com/en-us/download/details.aspx?id=13255

    function GEN_FILE_TestCSV(FilePath, ColumnName, ColumnValue)
    {
      var HasValue;
      HasValue = false;
      DDT.CSVDriver(FilePath);
      // Iterates through records
      while (! DDT.CurrentDriver.EOF()) {
        if (DDT.CurrentDriver.Value(ColumnName) == ColumnValue) {
          HasValue = true;
        }
        DDT.CurrentDriver.Next(); 
      }
      // Closes the driver
      DDT.CloseDriver(DDT.CurrentDriver.Name);
      if(HasValue == false) {
        //Posts an error to the test log. 
        Log.Error("The Expected Value looked for in the file is not there", ""); 
      } else {
        Log.Message("The Expected Value looked for in the file is there", ""); 
      }
    }