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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)