Allow capybara to click on labels instead of inputs for checkboxes

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. 11 months ago