1

我正在使用 cucumber 来测试我的应用程序(在 Rails 中)。现在,我在测试下载文件时遇到问题。我正在使用主干,所以我在每个功能上都添加了@javascript 标签。我已经完成了stackoverflow中给出的解决方案,例如:黄瓜测试文件下载,但它们在机架测试中。我想知道当我们在黄瓜中有@javascript时如何点击文件下载框?我试图验证响应标头,例如:page.response_headers['Content-Type'].should == "text/csv"但它表明它在 capybara 中不受支持。

任何建议将不胜感激。

4

1 回答 1

3

我假设您使用 Selenium 作为驱动程序,这是 @javascript 的默认驱动程序。

Selenium不为此提供跨浏览器解决方案。您想测试您的应用程序,而不是浏览器的本机下载窗口,因此您应该告诉浏览器自动保存下载的文件。

如果您使用火狐

Capybara.register_driver :selenium do |app|
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile['browser.download.dir'] = "/path-to-folder/webdriver-downloads"
  profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf" # content-type of file that will be downloaded
  Capybara::Selenium::Driver.new(app, :browser => :firefox, profile: profile)
end

如果您使用谷歌浏览器

Capybara.register_driver :selenium do |app|
  profile = Selenium::WebDriver::Chrome::Profile.new
  profile['download.prompt_for_download'] = false
  profile['download.default_directory'] = "/path/to/dir"
  Capybara::Selenium::Driver.new(app, :browser => :chrome, profile: profile)
end

然后单击链接(调用文件下载)并将文件保存到指定目录。

于 2013-02-17T14:59:49.850 回答