-
Notifications
You must be signed in to change notification settings - Fork 131
Downloading files
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.
- 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"/>
...
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.
- Home
- Getting started
- Parallel tests execution
- MsTest DataDriven tests from Xml and CSV files
- NUnit DataDriven tests from Xml, CSV and Excel files
- Comparing files by NUnit DataDriven tests
- Visual Testing
- Screen shots: full desktop, selenium. PageSource saving
- Verify-asserts without stop tests
- Downloading files
- Helpers
- Override browser profile preferences, install browser extensions, Headless mode
- Debugging Test.Automation framework
- Logging
- Performance measures
- Webdriver Extends
- More common locators
- Selenium-Grid-support
- Advanced Browser Capabilities and Options
- AngularJS synchronization
- Update App.config or appsettings.json
- Cross browser parallel test execution with testing-Cloud-Providers\SeleniumGrid
- Verifying Javascript Errors from browser
- Enabling Performance Log for Chrome
- Azure DevOps Support
- Edge browser Support
- Downloading and running Selenium Grid with Powershell
- Run Ocaramba tests with Docker container
- HTTP auth in Internet explorer
- ExtentReports Support