Allow capybara to click on labels instead of inputs for checkboxes

Posted . Visible to the public.

Within Capybara you most certainly use the #check- and #uncheck-method to (un)check checkboxes.
But there's one problem, if you want to test a custom styled checkbox, which hides its <input>-Tag:

  • The methods cannot (un)check checkboxes without an visible <input>.
  • The error message will be something like: Unable to find visible checkbox "Some label" that is not disabled

Solution 1

Use the keyword argument allow_label_click: true within the method call.
So instead of check('Some label'), use check('Some label', allow_label_click: true).

Solution 2

Make hidden inputs visible in integration tests via data-environment. Depending on your CSS framework you also need to adjust the z-index: 1.

[data-environment=test] input
 opacity: 0.1
Max E.
Last edit
Emanuel
License
Source code in this card is licensed under the MIT License.
Posted by Max E. to makandra dev (2023-06-19 13:14)