Read more

Allow capybara to click on labels instead of inputs for checkboxes

Max E.
June 19, 2023Software engineer at makandra GmbH

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

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

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
Posted by Max E. to makandra dev (2023-06-19 15:14)