简体   繁体   English

使用Capybara进行测试找不到使用简单表单关联创建的复选框

[英]Test with Capybara cannot find a checkbox created using Simple Form association

I have a form created using Simple Form, as such 我有一个使用简单表单创建的表单,例如

<%= simple_form_for @organisation do |f| %>
  <div class="form-inputs">
    <%= f.association :causes, as: :check_boxes %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

The page works fine when I use a browser, but when I try to check this with Capybara, such as: 使用浏览器时,页面工作正常,但是当我尝试使用Capybara进行检查时,例如:

check('organisation_cause_ids_1')

And have tried many variations of this eg 并尝试了这种变化,例如

find(:xpath , '//*[@id="organisation_cause_ids_1"]').set(true)
find("organisation_cause_ids_1").check

These always give an error: 这些总是给出一个错误:

 Failure/Error: check('organisation_cause_ids_1')
 Capybara::ElementNotFound:
   Unable to find checkbox "organisation_cause_ids_1"

The HTML generated by Simple Form is: 简单表单生成的HTML为:

 <div class="input check_boxes optional organisation_causes">
   <label class="check_boxes optional">Causes</label>
   <span class="checkbox">
     <label for="organisation_cause_ids_1" name="organisation[cause_ids]">
     <input class="check_boxes optional" id="organisation_cause_ids_1" name="organisation[cause_ids][]" type="checkbox" value="1" />Cause A</label>
   </span>
   <span class="checkbox">
     <label for="organisation_cause_ids_2" name="organisation[cause_ids]">
     <input class="check_boxes optional" id="organisation_cause_ids_2" name="organisation[cause_ids][]" type="checkbox" value="2" />Hunger</label>
   </span>
  ...

Edit: The problem was due to the lazy loading of the 'Causes' I created with the factories. 编辑:问题是由于我用工厂创建的“原因”的延迟加载造成的。 They weren't being created so the page had no checkboxes. 尚未创建它们,因此页面上没有复选框。

Try with this 试试这个

find_by_id('organisation_cause_ids_1').find("checkbox[value='1']").select_option

or maybe with this 也许与此

find(:css, ".check_boxes[value='1']").set(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM