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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)