我正在使用硒框架2.33測試網頁。這裏的TestCase應該驗證文件的下載。Selenium C#Firefox 22 - 「沉默保存文件」從網絡不再工作
以下代碼在Firefox 21及更早版本中都有效。自FF 22更新以來,它不再有效,我還沒有找到原因。
我用它來保存一個tar.gz文件,但txt或CSV文件也失敗。
安裝驅動程序:
FirefoxProfile profile = new FirefoxProfile();
profile.AcceptUntrustedCertificates = true;
profile.SetPreference("browser.download.dir", System.Environment.GetEnvironmentVariable("TEMP"));
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.manager.showWhenStarting", false);
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/xml, text/csv, text/plain, text/log, application/zlib, application/x-gzip, application/x-compressed, application/x-gtar, multipart/x-gzip, application/tgz, application/gnutar, application/x-tar");
profile.SetPreference("pdfjs.disabled", true);
IWebDriver webDriver = new FirefoxDriver(profile);
測試:
webDriver.Navigate().GoToUrl("https://example.com/downloadthis.txt");
注: 在Firefox 22, 「about:config中」 行 「browser.helperApps.neverAsk.saveToDisk」 與給定的參數存在。但是儘管如此,當它在保存位置檢查預期的文件時,彈出「保存文件」對話框並且測試失敗。
有沒有人有想法或遇到過嗎?
編輯:格式化
Fiddler顯示的實際MIME類型是什麼?你的代碼對於我來說可以正常使用csv,但不是txt,我的當前機器上沒有Fiddler,我需要檢查txt的實際MIMT類型。 http://www.gnu.org/licenses/gpl-3.0.txt –
Fiddler返回.tar.gz文件的MIME類型application/gzip。將這添加到選項可以解決問題。 Firefox必須將其更改爲版本22(舊MIME是application/x-gzip)。謝謝,user1177636讓我的一天更有光澤! – mojjj