Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a radio button in cucumber?

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>
like image 630
mb14 Avatar asked Dec 22 '22 06:12

mb14


2 Answers

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.

like image 190
mb14 Avatar answered Jan 02 '23 00:01

mb14


In your step definition add line:

choose('A Radio Button')

Cucumber uses Capybara, you can read more about it here: https://github.com/jnicklas/capybara

like image 35
Michał Czapko Avatar answered Jan 02 '23 00:01

Michał Czapko