AllenC
AllenC

Reputation: 2754

Clicking checkbox using capybara

Currently I have a checkbox wrapped by a label.

<label for="exercise_form_division_ids_34">
       <input class="check_boxes optional division-checkboxes" type="checkbox" value="34" name="form[division_ids][]" id="exercise_form_division_ids_34"> Technology
</label>

In my integration test I tried to use

within '.organizations' do
  find("label[for='exercise_form_division_ids_34").click
end

OR

check "exercise_form_division_ids_#{department.id}", allow_label_click: true

But I still get this nable to find visible checkbox "calltree_exercise_form_division_ids_2" that is not disabled Unable to find visible checkbox "exercise_form_division_ids_" that is not disabled

Upvotes: 2

Views: 2881

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

With the limited info provided you have a few potential possibilities.

  1. The label/checkbox aren't actually inside an element with the class of the organizations on the page.

  2. The error Unable to find visible checkbox "exercise_form_division_ids_" that is not disabled shows that no id is actually getting inserted into your selector which would tend to indicate that department isn't actually persisted in your test.

  3. You may be assuming 34 is the correct id based on what it is in your dev environment but that may not be what it is in your test environment.

To narrow down the possibilities the first thing to do would be to grab a screenshot in your with test with page.save_and_open_screenshot (assuming you're using a driver which supports screenshots) and make sure there is actually a visible checkbox on the page. If not, you're probably not creating the required objects in the DB prior to your test starting.

Secondly look at the page in your browser and make sure the elements visible on the screen are actually the checkbox and/or the label. If both label & checkbox are being hidden and replaced with some JS widget then you'd need to interact with whatever elements the widget creates in the page (just like a user would). If only the checkbox is being hidden via JS/CSS but the label is visible then

check('Technology', allow_label_click: true) # check matching on label text

should work.

Upvotes: 4

Related Questions