Forum Discussion

halovski's avatar
halovski
Contributor
10 months ago

Downloading and opening files from a browser

What is the best way to open files that are downloaded during a test? Is it manually clicking in explorer or using the "OnWebDownloadFinished" event listener?
  • rraghvani's avatar
    rraghvani
    10 months ago

    I've provided some code snippets - you will have to create a function to construct a filename based on date and time, search this file in the downloaded folder, and then open the file.

    function CodeSnippet()
    {
        // User download folder
        var filepath = aqEnvironment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads\\";
        Log.Message(filepath);
        
        // Construct filename using date and time of now
        var datestr = aqConvert.DateTimeToFormatStr(aqDateTime.Now(), "%Y-%m-%dT%H%M%S");
        var filename = "report - " + datestr + "*.pdf"
        Log.Message(filename);
        
        // Search for the filename in the filepath
        var aFile
        var foundFiles = aqFileSystem.FindFiles(filepath, filename);
        if (!strictEqual(foundFiles, null))
            while (foundFiles.HasNext())
            {
                aFile = foundFiles.Next();
                Log.Message(aFile.Name);
            }
        else
            Log.Message("No files were found.");
        
        // Open the file
        WshShell.Run("<path to chrome.exe> " + aFile);
    }
    

    For this to work correctly, ensure there's no other files in the downloaded folder in the format of "report - [date & time].pdf". You might have to adjust the search pattern as I'm excluding milliseconds