Skip to main content

How to Download File using WebDriver

When we download file from a web page, it asks for confirmation to download along with the download location. For automating this download file process, we have to set some preferences based on the browser used.
In this article, we will see file download for Firefox and Chrome separately.

Download File using FirefoxDriver

For firefox, we have to set the below firefox profile preferences.
  • browser.download.dir – The download directory
  • browser.download.folderList – It has to be set to 2 to make firefox use custom download directory (the above given download directory). If value is 0, it downloads the file to user’s desktop and if its 1, it uses the default Downloads folder.
  • browser.download.manager.showWhenStarting – Whether to show the download manager when starting download
  • browser.helperApps.neverAsk.saveToDisk – Never ask for saving to disk for the given file types.
Using the above preferences, we can create the profile and initialize the FirefoxDriver.
1
2
3
4
5
6
7
8
9
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "D:\\Custom\\Download\\Directory");
profile.setPreference("browser.download.folderList", 2);
// File type of the downloaded file
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "pdfjs.disabled", true );
// Initialize webdriver using the profile
WebDriver driver = new FirefoxDriver(profile);

Download File using Chrome

The method is similar for chromedriver in which we set the ExperimentalOptions preferences in ChromeOptions as shown below.
Preferences Set
  • download.default_directory – The download directory
  • profile.default_content_settings.popups – To disallow popups
1
2
3
4
5
6
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", "D:\\Downloads\\Directory");
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
We will be covering How to Import Files using WebDriver in a future post.

Comments

Popular posts from this blog

Testing Types

         Installation testing Main article: Installation testing An installation test assures that the system is installed correctly and working at actual customer's hardware. Compatibility testing Main article: Compatibility testing A common cause of software failure (real or perceived) is a lack of its compatibility with other application software, operating systems (or operating system versions, old or new), or target environments that differ greatly from the original (such as a terminal or GUI application intended to be run on the desktop now being required to become a web application, which must render in a web browser). For example, in the case of a lack of backward compatibility, this can occur because the programmers develop and test software only on the latest version of the target environment, which not all users may be running. This results in the unintended cons...

Install Jenkins and configure it to Run Maven with TestNg Selenium

Steps to Install Jenkins and configure it to Run Maven with TestNg Selenium Installation Step 1)  Go to  http://jenkins-ci.org/ and download correct package for your OS. Install Jenkins. Step 2)  Unzip Jenkins to specified folder. Run exe file as shown in following screenshot: Step 3)  In  Jenkins 1.607 Setup  window click on  Next  button. Step 4)  Click on  Install  button in the end. Step 5)  Once installation is done, navigate to the Jenkins Dashboard (http://localhost:8080 by default) in the browser window. Step 6)  Click on the  New Item  link to create a CI job. Step 7)  Select the Maven project radio button as shown in the following screenshot: Using the Build a  Maven Project  option, Jenkins supports building and testing Maven projects. Step 6)  Click on OK button. A new job with name "WebdriverTest" is created in Jenkins Dashboard. Step 7) ...

How to Compare Two Images in Selenium

How to Compare Two Images in Selenium Some time in testing we have to do image comparision as verification point. So, in this blog I expalin how to compare two images in selenium. I have created one function for compare two images. you can use directly into your framework. first of all you have to import below packages into your code. import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; Now, you can use below function for comparison of two images. 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 public boolean mCompareImages ( BufferedImage img1 , BufferedImage img2 ) { boolean bCompareImage = true ; try { if ( img1 . getWidth () == img2 . getWidth () && img1 . getHeight () == img2 . getHeight ()) { for ( int i = 0 ; i < img1 . getWidth (); i ++) { for ( int j = 0 ; j < img1 . getHeight (); j ++) { if ( img1 ....