Skip to content
Jakub Raczek edited this page May 24, 2021 · 31 revisions

Our test framework supports downloading files, in Firefox and Chrome browsers is perform by instructing the browser to download files to a specific location without being prompted. It's done by configuring a browser profiles in DriverContext.cs file:

If downloading files doesn't work for you, check PromptForDownloadLocation policy in Chrome (e.g. chrome://policy/) or in Firefox. Policy should be set to false.

  • Firefox
        private FirefoxProfile FirefoxProfile
        {
            get
            {
                var profile = new FirefoxProfile();
                ...
                // preference for downloading files
                profile.SetPreference("browser.download.dir", this.DownloadFolder);
                profile.SetPreference("browser.download.folderList", 2);
                profile.SetPreference("browser.download.managershowWhenStarting", false);
                profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel, application/x-msexcel, application/pdf, text/csv, text/html, application/octet-stream");
                
                // disable Firefox's built-in PDF viewer
                profile.SetPreference("pdfjs.disabled", true);

                // disable Adobe Acrobat PDF preview plugin
                profile.SetPreference("plugin.scan.Acrobat", "99.0");
                profile.SetPreference("plugin.scan.plid.all", false);
                ...
                return profile;
                    }
                }
  • Chrome
 private ChromeOptions ChromeProfile
        {
            get
            {
                ChromeOptions options = new ChromeOptions();
                options.AddUserProfilePreference("profile.default_content_settings.popups", 0);
                options.AddUserProfilePreference("download.default_directory", this.DownloadFolder);
                options.AddUserProfilePreference("download.prompt_for_download", false);
                ...
                return options;
            }
        }

Please notice that you can override browser profile preferences or add additional, more info here.

We implemented several methods in class FileHelper allow you to wait in your test until file will be downloaded.

The location for downloading files can be set in app.config file:

  • by setting flag UseCurrentDirectory to true, the location will be the subfolder of the folder with you test project library (relative path).
<?xml version="1.0" encoding="utf-8"?>
...
    <!--Downloaded files, screenshots and page source location-->
    <add key="UseCurrentDirectory" value="true"/>
    <add key="DownloadFolder" value="\TestOutput"/>
...
</configuration>
  • you can set flag UseCurrentDirectory to false and set DownloadFolder folder to absolute path
<?xml version="1.0" encoding="utf-8"?>
...
    <!--Downloaded files, screenshots and page source location-->
    <add key="UseCurrentDirectory" value="false"/>
    <add key="DownloadFolder" value="c:\Tests\TestOutput"/>
...

You can wait for file on three ways:

Framework waits for file with default timeout LongTimeout or you can set your own timeout in seconds. If file is not downloaded in given time the exception is thrown.

  • when you know the name of downloaded file, wait for file with default LongTimeout:
this.Driver.GetElement(this.fileLink.Format(fileName), "Click on file").Click();
FilesHelper.WaitForFileOfGivenName(fileName, this.DriverContext.DownloadFolder);

or set your own timeout, e.g. 5 seconds

this.Driver.GetElement(this.fileLink.Format(fileName), "Click on file").Click();
FilesHelper.WaitForFileOfGivenName(5, fileName, this.DriverContext.DownloadFolder);
  • when you know the type of downloaded file (extension), count all files of that type in download location, download your file and wait till number of files of that type will increase by one, use:
var filesNumber = FilesHelper.CountFiles(this.DriverContext.DownloadFolder, FileType.Txt);
this.Driver.GetElement(this.fileLink.Format("some-file.txt")).Click();
FilesHelper.WaitForFileOfGivenType(FileType.Txt, filesNumber, this.DriverContext.DownloadFolder);
FileInfo file = FilesHelper.GetLastFile(this.DriverContext.DownloadFolder, FileType.Txt);
  • when you don't know the name and type of downloaded file (extension), count all files in download location, download your file and wait till number of files will increase by one, use:
var filesNumber = FilesHelper.CountFiles(this.DriverContext.DownloadFolder);
this.Driver.GetElement(this.fileLink.Format("some-file.txt")).Click();
FilesHelper.WaitForFile(filesNumber, this.DriverContext.DownloadFolder);
FileInfo file = FilesHelper.GetLastFile(this.DriverContext.DownloadFolder)

Please notice that all FilesHelper.WaitForFile... methods verify also if size of downloaded file is bigger than 0 bytes.

More examples of downloading files can be found here DownloadPage.cs and SecureFileDownloadPage.cs.

With MsTest unit tests additionally you have to use a *.runsettings file or *.testsettings. More details here.

Clone this wiki locally