'How to get value of checkbox in capybara?

i have code html like this

<input type="checkbox" class="class-of-checkbox" value="facebook">

how to get the value (facebook) ?



Solution 1:[1]

You first need to uniquely find the element and then call value on it. How you find the element uniquely can be highly dependent on the structure of the rest of the page, but based on just the single element HTML you've provided (and you not actually knowing what the value already is) either of the following could be a starting place

find('.class-of-checkbox').value
find_field(type: 'checkbox', class: 'class-of-checkbox').value

If you just want to verify the value of an element with an expectation then you could do (assuming RSpec with cucumber)

expect(page).to have_field(type: 'checkbox', with: 'facebook', class: 'class-of-checkbox')  # you can also pass :checked option if you want to verify it is checked - or use have_checked_field/have_unchecked_field

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1