Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara have_selector is not working on checkbox

Tags:

rspec

capybara

On my view I have a checkbox

<input type="checkbox" id="modal-checkbox" class="painter-modal-checkbox" data-reactid=".1.1:1.0.0">

On my capybara code, I am trying to use the have_selector method

expect(page).to have_selector("#modal-checkbox")

This fails, when I am tracing the page with

print page.html

I can confirm that my html code is there, why does this test fails.

like image 238
user3814030 Avatar asked Dec 02 '25 16:12

user3814030


1 Answers

This is usually because the field isn't actually visible because it's hidden via CSS. With checkboxes this is often because the design calls for the label to be used to show the selected state of the checkbox while the actual checkbox is hidden. To check if visibility is the issue you can pass visible: false to have_selector, however 99% of the time you shouldn't be checking for invisible items since a user wouldn't be able to see them or interact with them

like image 110
Thomas Walpole Avatar answered Dec 06 '25 16:12

Thomas Walpole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!