Capybara :: ElementNotFound:无法找到文件字段“ file”; [英] Capybara::ElementNotFound: Unable to find file field "file"

查看:97
本文介绍了Capybara :: ElementNotFound:无法找到文件字段“ file”;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试文件上传(即CSV)。在我的代码和浏览器HTML中,我找到了文件字段,但是在测试水豚时无法找到文件字段。我尝试了不同的方法,但是无法解决问题。这里部分看起来像这样:

I am testing file upload i.e CSV. In my code as well as browser HTML I found file field but while testing the capybara is unable to find the file field. I tried hard and different approaches but unable to fix the problem. Here partial look like this:

#add_file_box
  %div.msg
  %h1.page-header
    = "Upload a CSV"
  %h4.title

  = form_tag dummy_path, multipart: true, class: "upload_csv" do
    = hidden_field_tag :dmp_id, @dmp.id
    .form-group
      .input-group
        %span.input-group-btn
          %span.btn.btn-primary.btn-file
            Choose file
            = file_field_tag :file, style: 'line-height: normal',  accept: "text/csv", class: "file_input"
        %input.form-control.input-custom{:readonly => "", :type => "text"}
    .form-group
      = submit_tag "Upload CSV", class: "btn btn-primary", id: "upload_csv"

水豚测试类似

 within '.upload_csv' do
     page.attach_file('file', "#{Rails.root}/spec/acceptance/test_uploads/input_output.csv")
     click_button 'Upload'
   end

如果您能帮助我解决此问题,我将不胜感激?

I will be thankful if you can help me fixing this problem?

推荐答案

Capybara 2x( capybara问题)不能

Capybara 2x (capybara issue) doesn't find hidden elements by default.

您可以将ignore_hidden_​​elements设置为false:

You can either set ignore_hidden_elements to false:

Capybara.ignore_hidden_elements = false

或者只需在方法中添加:visible选项:

Or simply add :visible option to your method:

within '.upload_csv' do
  attach_file('file', "#{Rails.root}/spec/acceptance/test_uploads/input_output.csv", visible: false)
    click_button 'Upload'
end

这解决了我的问题。

注意::visible 选项也受大多数内部使用水豚::查询(例如查找全部 has_css? have_selector 等)

Note: :visible option is also supported by most of Capybara methods that internally work with Capybara::Query (like find, all, has_css?, have_selector etc.)

这篇关于Capybara :: ElementNotFound:无法找到文件字段“ file”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆