Ruby-on-rails – How to check a radio button in cucumber

capybaracucumberruby-on-rails

I'm using cucumber with RoR (with either webrat or capybara)

How can I write a step to check a radio button? I've tried "choose" or "select" but it can't find my radio button.
I'm not sure what to do, as I have in fact 2 inputs with the same name (the 2 radio buttons belonging to the same "group")

Thanks

Example of html

<form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">

<input type="radio" name="group1" value="Milk"> Milk<br>
<input type="radio" name="group1" value="Butter" checked> Butter<br>
<input type="radio" name="group1" value="Cheese"> Cheese

</form>

Best Answer

The answer is to choose the id (generated by Rails) of the radio button.

 <form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">

    <input type="radio" name="group1" value="Milk" id="group1_milk"> Milk<br>
    <input type="radio" name="group1" value="Butter" checked id="group1_butter"> Butter<br>
    <input type="radio" name="group1" value="Cheese" id="group1_cheese"> Cheese

  </form>

and do

choose("group1_milk").

That will work even if more radio buttons have the same options.

Related Topic