Search code examples
rspeccapybaracapybara-webkitpoltergeist

Capybara: get selected radio button using webkit or poltergeist


It's easy enough to get the selected radio button with Capybara using the rack_test driver.

# with rack_test
page.set('input_id')
# => "checked"
page.find('[checked]')
# => #<Capybara::Node::Element tag="input" path="/html/body/p[1]/label[1]/input">

However, this doesn't work with webkit or poltergeist.

# with webkit or poltergeist
page.set('input_id')
# => ""
page.find('[checked]')
Capybara::ElementNotFound: Unable to find css "[checked]"

I've also tried using the #selected? method, but it doesn't seem to be working for the radio button.

# with any driver
page.set('input_id')
page.all('input').select(&:selected?)
# => []

How can I get the checked radio button with Capybara in webkit or poltergeist?


Solution

  • You are running into the difference between attributes and properties in JS supporting browsers. What you've done in rack-test works because it only knows about attributes. To find a checked input in the other browsers you could do

    find('input:checked')
    

    or you could do things like

    find(:checkbox, 'input_id', checked: true)
    find(:radio_button, 'input_id', checked: true)
    find(:field, 'input_id', type: 'checkbox', checked: true)
    

    etc...