Firefox download NEW

阅读更多
import java.io.File;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;


public class WebDriverProducerFactory {

public static FirefoxDriver getFireFoxDriver() throws InterruptedException{
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","C:\\QA Team Files\\ESGManager\\download\\FireFoxDownload\\");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/pdf");
firefoxProfile.setEnableNativeEvents(true);
File fin = new File("C:\\QA Team Files\\ESGManager\\download\\FireFoxDownload\\");
    File[] finlist = fin.listFiles();      
    for (int n = 0; n < finlist.length; n++) {
        if (finlist[n].isFile()) {
        System.gc();
        Thread.sleep(2000);
            finlist[n].delete();
        }
    }  
File pathToBinary = new File("C:/Program Files (x86)/FrontMotion/Firefox/firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
ESGManagerFireFoxWebDriver esgManagerFireFoxWebDriver = new ESGManagerFireFoxWebDriver(ffBinary,firefoxProfile);
esgManagerFireFoxWebDriver.manage().window().setPosition(new Point(0,0));
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
    esgManagerFireFoxWebDriver.manage().window().setSize(dim);
return esgManagerFireFoxWebDriver;
}

public static FirefoxDriver getFireFoxPathDownloadDriver(String path) throws InterruptedException{
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);

//Anyway, in order to make Firefox saving pdf files to a pre-defined folder as the default behaviour, you might want to try the following code, where setting pdfjs.disabled to true will prevent Firefox previewing the files.
profile.setPreference("pdfjs.disabled", true);
// Use this to disable Acrobat plugin for previewing PDFs in Firefox (if you have Adobe reader installed on your computer)
profile.setPreference("plugin.scan.Acrobat", "99.0");
profile.setPreference("plugin.scan.plid.all", false);

profile.setPreference("browser.download.dir",path);

String file_types = "application/acrobat, application/nappdf, application/x-pdf, application/vnd.pdf, text/pdf, text/x-pdf, application/vnd.sealedmedia.softseal.pdf" +
"text/comma-separated-values,text/csv, text/pdf, application/pdf, application/x-msdos-program, application/x-unknown-application-octet-stream,"
             + "application/vnd.ms-powerpoint, application/vnd.ms-publisher, application/x-unknown-message-rfc822, application/vnd.ms-excel,"
             + "application/msword, application/x-mspublisher, application/x-tar, application/zip, application/x-gzip, application/x-stuffit,"
             +"application/vnd.ms-works, application/powerpoint, application/rtf, application/postscript, application/x-gtar,"
             + "video/quicktime, video/x-msvideo, video/mpeg, audio/x-wav, audio/x-midi, audio/x-aiff, text/plain, application/vnd.ms-excel [official],"
             + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/msexcel, application/x-msexcel,"
             + "application/x-excel, application/excel, application/x-ms-excel, application/x-dos_ms_excel,"
             + "text/csv, text/comma-separated-values, application/octet-stream, application/haansoftxls    application/xml, application/xhtml+xml,"
             + "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf,"
             + "application/vnd.ms-excel (official),    application/msexcel,    application/x-msexcel,    application/x-ms-excel,    application/x-excel,    application/x-dos_ms_excel,    application/xls,    application/x-xls,    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (xlsx),"
             + "application/softgrid-xls, x-softmaker-pm, text/tab-separated-values, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel (official)";

// String file_types = "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf";

profile.setPreference("browser.helperApps.neverAsk.openFile", file_types);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", file_types);
// profile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf,application/vnd.adobe.xfdf,application/vnd.fdf,application/vnd.‌​adobe.xdp+xml");

File fin = new File(path);
  if (!fin.exists()) {   
  fin.mkdirs();   
          }  

File pathToBinary = new File("C:/Program Files (x86)/FrontMotion/Firefox/firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
ESGManagerFireFoxWebDriver esgManagerFireFoxWebDriver = new ESGManagerFireFoxWebDriver(ffBinary,profile);
esgManagerFireFoxWebDriver.manage().window().setPosition(new Point(0,0));
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
    esgManagerFireFoxWebDriver.manage().window().setSize(dim);
return esgManagerFireFoxWebDriver;
}

public static RemoteWebDriver getWebDriver(String browserType){
if (browserType.toLowerCase().contains(Constant.FIREFOX)){
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","C:\\QA Team Files\\ESGManager\\download\\FireFoxDownload\\");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/pdf");
firefoxProfile.setEnableNativeEvents(true);
File pathToBinary = new File("C:/Program Files (x86)/FrontMotion/Firefox/firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
ESGManagerFireFoxWebDriver esgManagerFireFoxWebDriver = new ESGManagerFireFoxWebDriver(ffBinary,firefoxProfile);
esgManagerFireFoxWebDriver.manage().window().setPosition(new Point(0,0));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
esgManagerFireFoxWebDriver.manage().window().setSize(dim);
return esgManagerFireFoxWebDriver;
}
if (browserType.toLowerCase().contains(Constant.IEXPLORER)){
System.setProperty("webdriver.ie.driver","C:\\bby\\selenium\\2.32\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
ESGManagerInternetExprolerWebDriver esgManagerInternetExplorerWebDriver =
new ESGManagerInternetExprolerWebDriver(ieCapabilities);
esgManagerInternetExplorerWebDriver.manage().window().setPosition(new Point(0,0));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
esgManagerInternetExplorerWebDriver.manage().window().setSize(dim);
return esgManagerInternetExplorerWebDriver ;
}
return null;
}
}


你可能感兴趣的:(firefox)