水豚因 NotSupportedByDriverError 失败

Capybara fails with NotSupportedByDriverError

我正在尝试验证下载 csv 在我的 rails 应用程序中是否有效。但它抛出错误 Capybara::NotSupportedByDriverError: Capybara::Driver::Base#response_headers

  it 'exports as CSV' do
    visit_and_login
    agree_to_tos

    click_link 'Download to CSV'

    page.response_headers['Content-Type'].should include 'text/csv'
  end

selenium driver 不提供对响应 headers(也不是状态代码)的访问。你有几个选择

  1. 只需验证 href 和属性(下载等)或 link 是否正确
  2. 配置 driver 以实际下载文件,然后打开并验证它是否正确。

阅读http://ardesco.lazerycode.com/testing/webdriver/2012/07/25/how-to-download-files-with-selenium-and-why-you-shouldnt.html然后决定你想做哪些。如果#1 那么它很简单

expect(page).to have_link('Download to CSV', href: 'http://blahblah' )

if #2 然后查看 Capybara 测试套件,了解如何配置 selenium driver 以实际下载文件 - https://github.com/teamcapybara/capybara/blob/master/spec/selenium_spec_chrome.rb#L14