简体   繁体   中英

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:

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:

 <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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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