How to check a radio button in cucumber?

10,067

Solution 1

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.

Solution 2

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

Share:
10,067
mb14
Author by

mb14

15 years of software engineering: mostly C++ , python currently use: Ruby, Rails, git, Mac Os interests: functional languages (especially haskell), Go (the game), DRY code and way to achieve it, maths. environment : vim, tmux, Colemak.

Updated on July 12, 2022

Comments

  • mb14
    mb14 almost 2 years

    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>