Forum Discussion

RUDOLF_BOTHMA's avatar
RUDOLF_BOTHMA
Community Hero
6 years ago

determine file name of downloaded file

Hi all,

 

I'm trying to confirm that the contents of my CSV exports are as expected.  Or conversely, export the data from my application to check the application data is correct by checking the CSV results.  This works better than Database checks for some situations, since the CSV eport can do all the object property lookups with multiple database calls etc.  I have all the required tools through DataDrivers/ADO to read through the csv/xls etc.  What I'm finding a challenge is finding resources that can help me find the path and filename of the last download.  I have tried solutions described in thread

 

https://community.smartbear.com/t5/TestComplete-Functional-Web/Downloading-and-looking-at-a-file/m-p/177970#M33870

 

and 

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

 

Both these resources however assume you are trying to download an existing file to your machine.  I'm trying to get a CSV/XLS that gets generated and uniquely named when you click the "Export" button.  I'm sure it's been done before.  Are there any other resources I haven't found yet you know of ?

 

Something like https://support.smartbear.com/viewarticle/51660/

 

but I do testing in IE and I'm not clear exactly how I'm going to extract the file name from there...

 

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    My guess is that it exports/downloads to the default "Downloads" folder for the currently logged in user.  So, you can do something like %AppData% to get the pathing to start with.

     

    As for the filename, It might not be a matter of filename, but more on the lines of just detecting the CSV/XLS file with the most recent date/time stamp.  Look into using aqFileSystem to determine that.  

    I would also suggest, in order to make that easier, at the end of the test, delete all CSV/XLS files from that folder or at least move them to an archive place.  That way, when you do the export, the ONLY file in that downloads folder will be the one you want... and the specific filename itself won't matter.

    • RUDOLF_BOTHMA's avatar
      RUDOLF_BOTHMA
      Community Hero

      Thanks, both

       


      You should be able to use https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/window-and-process/sa... when you export / save and specify your path and file name.

      Not going to be able to do that this time.  You never get a save as dialog for this particular application.

       

      I reckon however that between aqFile and aqFileSystem I can cobble together a wrapper script that can find the file and return the path.  If I feel like a real mister fancypants, I'll make it so it can copy the file to a convenient path while I'm at it and delete it when I'm done reading the file.  I will probably control my environment by setting up my browser to always save downloads to a specific folder I specify e.g. "C:\TC\Downloads" and storing it in a project variable in case the environment changes in the future.  I'll probably add a script I can call each time before I click that export button that just deletes the download folder contents. That way the first (only) file will alsways be the one I'm looking for. 

       


      ... and the specific filename itself won't matter.

      I'll be needing the filename if I'm going to use DataDriver/ADO, but if there's only the one file left as you suggested it will be easy to get from file info.  I'll let you know how it went...

      • RUDOLF_BOTHMA's avatar
        RUDOLF_BOTHMA
        Community Hero

        I did say I'd let you know how it went...

         

        I now have a script unit that can do all the required work to do this.  Too much code to put it here, but basically, I have two project variables.  DownloadsFolder and WorkingFolder.

         

        I change my browser to always download to the Project Variable folder (c:\TC\Downloads)

         

        There is a method that can clear folder contents, some copy/move functions and GetFile methods.

         

        Before I download, I delete the contents of both folders.  I can then use aqFileSystem.FindFiles and return the first one I find - there is handling if I don't find anything in the form of returning null. The calling function needs to check for nulls.  This function can take a search pattern, so I don't need to know the exact filename.  This caters for those downloads that have their names changing because they were downloaded at a later time.  Now, the piece that I had to put in to cater to the fact that there is no connect between clicking the save button (using UIAObjects) and the file download completing.  I have a function that waits for the file to exist.  Its more reliable and faster than a set delay. :smileytongue::

         

         

        function WaitFileExistsSearch(folderPath, searchPattern, timeout)
        {
          if(StringIsNullOrEmpty(folderPath))
          {
            Log.Error("No folder path specified");
            return false;
          }
          
        //DefaultValue is a script unit I wrote that has methods for converting null or undefined values to a default value. If it's not null, the current value is returned searchPattern = DefaultValue.GetString(searchPattern,"*.*"); timeout = DefaultValue.GetInt(timeout,Options.Run.Timeout); var startTime = GetTickCount(); var fileExists = false; LogMessageVerbose("Waiting for file to exists","FilePath : " + folderPath + "\nSearch Pattern : " + searchPattern); var waitTime = GetTickCount() - startTime; while(waitTime < timeout && fileExists==false) { if(GetFile(folderPath,searchPattern,true)!=null) { fileExists=true; } else { aqUtils.Delay(100,"Checking File Existence (" + folderPath + ")"); } waitTime = GetTickCount() - startTime; } if(!fileExists) { Log.Error("File did not exists within the specified time period","FilePath : " + folderPath + "\nSearch Pattern : " + searchPattern); } else { LogMessageVerbose("File Exists", "Wait Time : " + waitTime + "ms"); } return fileExists }

        Once I have the file in the downloads folder, I can move the file to the working folder :

         

        aqFileSystem.MoveFile(DownloadFolder,WorkingFolder,!overwriteOnCollision);

         

        Now I can reliably use my datadriver, since I control where my files are and it doesn't matter that the file name changes each time, because I can rename it to what I want before DataDrivers start using it - or I can pass the file path to the DataDriver

  • cunderw's avatar
    cunderw
    Community Hero