Saturday, August 1, 2015

FileDownload Using Firefox Profile

We know how to create Firefox profile using Selenium WebDriver code and what it is. We also download a file using using AutoIt tool. Now here we try to learn how to download file using using Firefox profile.

For downloading a file using Selenium WebDriver we need to set some properties in Firefox profile.In below we know what is the meaning of that properties.

Step 1.

        FirefoxProfile profile = new FirefoxProfile();
This is required to create a Firefox profile form Selenium WebDriver,

Step 2.
        profile.setPreference("browser.download.folderList", 2); 
Default Value of  browser.download.folderList is 1. We can set it as 0, 1, or 2.
          0  =  Firefox will save all files downloaded via the browser on the user's Desktop.
          1  =  Downloads are stored in the Downloads folder.
          2  =  Location specified for the most recent download is utilized again . 

Step 3.
        profile.setPreference("browser.download.manager.showWhenStarting", false); 
Default Value of browser.download.manager.showWhenStarting is true. This option means whether or not the Download Manager window is displayed when a file download is initiated or not. If we make it as false then Window should not be appeared.

Step 4.
        profile.setPreference("browser.download.dir", downloadLocation); 
browser. download. dir properties is required for last directory used for saving a file from the "What should (browser) do with this file?"

Step 5.
        profile.setPreference("browser.helperApps.neverAsk.openFile",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
 


This property browser. helperApps. neverAsk. openFile A comma-separated list of MIME types to open directly without asking for confirmation. Default value is an empty string.

Step 6. 
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");


This property browser. helperApps. neverAsk. saveToDisk A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default value is an empty string.

Step 7.
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
This property browser. helperApps. alwaysAsk. force 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.

Step 8.
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
This Property browser.download.manager.alertOnEXEOpen True (default): When it is true then warn the user attempting to open an executable from the Download Manager False: display 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.

Step 9.
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);

This Properties browser. download. manager. focusWhenStarting True: Set the Download Manager window as active when starting a download False (default): Leave the window in the background when starting a download.

Step 10.
        profile.setPreference("browser.download.manager.closeWhenDone", false);
This Property browser. download. manager. closeWhenDone True: Close the Download Manager when all downloads are complete False (default): Opposite of the.

N.B  For detail list of MIME Type is available here
  1. http://www.freeformatter.com/mime-types-list.html
  2. http://www.sitepoint.com/web-foundations/mime-types-complete-list/
Complete Example :


package filedownload;

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class FileDownload {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver(new FirefoxBinary(new File("G:/Program Files/Mozilla Firefox/firefox.exe")),
getFireFoxProfile("G:\\Test"));
        driver.get("http://www.google.com");
    }

    public static FirefoxProfile getFireFoxProfile(String downloadLocation) {
        FirefoxProfile profile = new FirefoxProfile();
        /**
         * Default Value: 1 The value of browser.download.folderList
         * can be set to either 0, 1, or 2.
         * 0 = Firefox will save all files downloaded via the browser on the user's desktop.
         * 1 = these downloads are stored in the Downloads folder.
         * 2 = location specified for the most recent download is utilized again .
         */

        profile.setPreference("browser.download.folderList", 2);
        /**
         * Default Value: true The browser.download.manager.showWhenStarting Preference in Firefox's
         *about:config interface allows the user to specify
         * whether or not the Download Manager window is displayed when a file download is initiated.
         */

        profile.setPreference("browser.download.manager.showWhenStarting", false);
        /**
         * browser. download. dir The last directory used for saving a file from the
         * "What should (browser) do with this file?" dialog.
         */

        profile.setPreference("browser.download.dir", downloadLocation);
        /**
         * browser. helperApps. neverAsk. openFile A comma-separated list of MIME types to
         * open directly without asking for confirmation. Default value is an empty string.
         */

        profile.setPreference("browser.helperApps.neverAsk.openFile",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
         /**
         * browser. helperApps. neverAsk. saveToDisk A comma-separated list of MIME types
         * to save to disk without asking what to use to open the file. Default value
         * is an empty string.
         */

        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        /**
         * browser. helperApps. alwaysAsk. force 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
         */

        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        /**
         * browser.download.manager.alertOnEXEOpen True (default):
         * warn the user attempting to open an executable from the
         * Download Manager False: display 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.
         */

        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        /**
         * browser. download. manager. focusWhenStarting
         * True: Set the Download Manager window as active when starting a download
         * False (default): Leave the window in the background when starting a download
         */

        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);       profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
        profile.setPreference("pdfjs.disabled", true);
        /**
         * browser. download. manager. closeWhenDone
         * True: Close the Download Manager when all downloads are complete
         * False (default): Opposite of the
         */

        profile.setPreference("browser.download.manager.closeWhenDone", false);
        return profile;
    }
}

 

No comments:

Post a Comment