在Firefox中自动下载pdf文件

时间:2013-03-08 11:30:40

标签: java firefox selenium-webdriver junit4

我正在使用Selenium和JUnit进行测试。 我正在尝试导出报告(pdf和xls)。 使用Selenium,我单击下载链接,然后执行verifcar断言文件位于正确的文件夹中。

我能够使用xls测试,但Firefox总是询问我想用PDF做什么。

Firefox的配置,Selenium是这样的:

String downloadPath = new File("src/test/resources/firefox_download").getAbsolutePath();
firefoxProfile.setPreference("browser.download.dir", downloadPath);
        firefoxProfile.setPreference("browser.download.lastDir", downloadPath);
        firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
        firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf,application/vnd.fdf");

        firefoxProfile.setPreference("browser.download.folderList", 2);
        firefoxProfile.setPreference("browser.download.defaultFolder", downloadPath);
        firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/vnd.fdf, application/x-msdos-program, application/x-unknown-application-octet-stream, application/vnd.ms-powerpoint, application/excel, 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"); // tipo específico
        firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);

我可以手动更改,在这里:

Manual 1

或在这里:

Manual 2

但我需要以编程方式执行此操作,否则测试将在其他地方失败(例如服务器/声纳)

PS。:Firefox 19

1 个答案:

答案 0 :(得分:1)

您可以尝试将MIME类型设置为application/octet-stream,这样如果浏览器不知道如何处理它,它会弹出save as对话框