As we know we cannot access operating system’s native windows with Selenium. We use AutoIt tool to upload documents. We have discussed uploading a file using AutoIT Tool in earlier tutorials. In this post we will learn How to Download Files using Selenium?
Problem Statement
Sometimes in the journey of automation, it is required to download files from web application like MS Excel file, MS Word File, Zip file, PDF file, CSV file, Text file, ect. Just like with uploading files we hit the same issue with downloading them. A dialog box just out of Selenium’s reach.
Once click on the link of the file, one dialog box or confirmation window will appears and ask to save the file. If you try to inspect the element of the window, you will notice that it is not the browser HTML window but a system window, which can not be handled by the Selenium. As selenium is just a Web Browser Automation Tool.
Different browsers can have different behaviours. For example just go to this demo practice page : http://original.toolsqa.com/automation-practice-form/ , you will see a link of Selenium Automation Hybrid Framework. Just try clicking on the link with different browsers.
Chrome Behaviour: It will start downloading the file by default, as soon as the user click on the link of any file.
IE Behaviour: IE displays a bar at the bottom of the window and displays the option to Save or Cancel the file download.
FireFox Behaviour: This will display an dialog box window and displays the option to Save or Cancel the file download.
Solution
Selenium WebDriver gives the capability to the test to handle the Dialog Box and enables downloading different files. This can be achieved with the help of FireFox Profile. But before moving forward it is good to understand the concept of MIME types.
What is MIME Type?
MIME stands for Multi-purpose Internet Mail Extensions. MIME types form a standard way of classifying file types on the Internet. Internet programs such as Web servers and browsers all have a list of MIME types, so that they can transfer files of the same type in the same way, no matter what operating system they are working in.
A MIME type has two parts: a type and a subtype. They are separated by a slash (/). For example, the MIME type for Microsoft Word files is application and the subtype is msword. Together, the complete MIME type is application/msword. Here is the path to find the MIME of the different files.
MIME for most common files are :
- Text File (.txt) – text/plain
- PDF File (.pdf) – application/pdf
- CSV File (.csv) – text/csv
- MS Excel File (.xlsx) – application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
- MS word File (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document
Before downloading any file, it is required to pass the MIME type of the file to the FireFox profile, so that it understand and help the test to achieve the given task.
How to Download files using Selenium?
1) Create a FireFox Profile.
2) Set Preferences as per requirement.
3) Open Firefox with FireFox Profile.
Let us implement the same through Script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package seleniumPrograms; import org.openqa.selenium.By; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; public class DownloadFiles_FireFoxProfile { public static void main(String[] args) throws InterruptedException { //Create FireFox Profile object FirefoxProfile profile = new FirefoxProfile(); //Set Location to store files after downloading. profile.setPreference("browser.download.dir", "D:\\WebDriverDownloads"); profile.setPreference("browser.download.folderList", 2); //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types. profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); profile.setPreference( "browser.download.manager.showWhenStarting", false ); profile.setPreference( "pdfjs.disabled", true ); //Pass FProfile parameter In webdriver to use preferences to download file. FirefoxDriver driver = new FirefoxDriver(profile); // Open APP to download application driver.get("http://original.toolsqa.com/automation-practice-form/"); // Click to download driver.findElement(By.linkText("Test File to Download")).click(); //Halting the execution for 5 secs to donwload the file completely Thread.sleep(5000); driver.close(); } } |
Run the above code and check the specified location for the existence of the downloaded file at D:\\WebDriverDownloads.
Different Preference Settings
1) setPreference(“browser.download.folderList”, 2);
Default Value: 0
This can be set to either 0, 1, or 2. When set to 0, Firefox will save all files on the user’s desktop. 1 saves the files in the Downloads folder and 2 saves file at the location specified for the most recent download.
2) setPreference(“browser.download.manager.showWhenStarting”, bool);
Default Value: true
It allows the user to specify whether or not the Download Manager window is displayed when a file download is initiated.
3) setPreference(“browser.helperApps.alwaysAsk.force”, bool);
Default Value: true
Always ask what to do with an unknown MIME type, and disable option to remember what to open it with False (default): Opposite of above
4) setPreference(“browser.helperApps.neverAsk.saveToDisk”,value);
Default Value: empty string
A comma-separated list of MIME types to save to disk without asking what to use to open the file.
5) setPreference(“browser.helperApps.neverAsk.openFile”,value);
Default Value: empty string
A comma-separated list of MIME types to open directly without asking for confirmation.
6) setPreference(“browser.download.dir”,path);
Default Value: empty string
The directory to save the file.
7) setPreference(“browser.download.manager.alertOnEXEOpen”,bool);
Default Value: true
Warn the user attempting to open an executable from the Download Manager. False displays no warning and allow executable to be run.
Note: In Firefox, this can be changed by checking the “Don’t ask me this again” box when you encounter the alert.
8) setPreference(“browser.download.manager.closeWhenDone”,bool);
Default Value: false
True closes the Download Manager when all downloads are complete and False is opposite.
9) setPreference(“browser.download.manager.focusWhenStarting”,bool);
Default Value: false
True set the Download Manager window as active when starting a download and False leaves the window in the background when starting a download.
How to set FireFox Profile settings manually to Download files using Selenium?
1) Open Firefox browser and in url box type about:config and press enter
2) In Search bar type neverask and enter, here you will find some settings(refer below screenshot).
3) Notice that Value column is blank, so it is required to set the desired property by specifying file type. So that the FireFox browser will not ask for permission to download files.
Make sure you open the same profile, as Selenium always picks up the default profile. Learn more on How to set FireFox Profile.