#274 ✓resolved

choose does not select radio button

Reported by jazen | August 5th, 2009 @ 07:18 AM

I have several radio buttons like this:

<input id="user_class" name="user[class"] value="PrivateUser" />
<input id="user_class" name="user[class"] value="CompanyUser" />

Selecting the second one using

choose "CompanyUser"

does nothing. After digging into the source the problem seems to be
the Locater which doesn't check the radio-button's value but it's
name instead.

I'm using latest cucumber/webrat gems.

I temporarily fixed this issue by overwriting the webrat step with:

When /^I choose "([^\"]*)"$/ do |field|
  dom = webrat.current_dom
  radio_button = Webrat::XML.xpath_search(dom, ".//input[@type='radio']").detect do |element|
    Webrat::XML.attribute(element, "value") == field
  Webrat::Field.load(webrat, radio_button).choose

This works as expected. :)

Comments and changes to this ticket

  • jazen

    jazen August 5th, 2009 @ 07:22 AM

    Forgot formatting, sent update-email to lighthouse..

  • Marc-André Lafortune

    Marc-André Lafortune October 26th, 2009 @ 11:29 AM

    +1: how are we supposed to do that?

  • Marc-André Lafortune

    Marc-André Lafortune October 26th, 2009 @ 04:04 PM

    -1: sorry, got confused here. choose works for me (although I wish there was a way to specify the name in addition to the value)

  • Larry Siden

    Larry Siden November 15th, 2009 @ 12:04 PM

    I had the same problem. I have two different sets of radio buttons whose values are "yes" and "no". I had to resort to Jazen's method, which works, but is quite verbose!

  • Bryan Helmkamp

    Bryan Helmkamp November 15th, 2009 @ 04:17 PM

    • State changed from “new” to “resolved”

    Generally I prefer to locate radio buttons by a label instead of the value. This also allows the application code to change it's representation while keeping the test code passing, so long as the UI stays constant.

    Would this work for you?

    It seems to make sense that choose would locate by value as well, but right now I'm considering the locators to be "frozen" until they can be reviewed and updated together once before Webrat 1.0. When this happens, I think that's a good change to consider.

  • Caya

    Caya February 3rd, 2011 @ 12:11 PM

    • Milestone order changed from “0” to “0”

    Hello everybody,

    I'd like to reopen this ticket. The solution shown by jazen doesn't work for me (Webrat::XML.xpath_search and Webrat::XML.attribute lead to "undefined method" errors).

    My problem is that I need to choose a radiobutton by its id - unfortunately there's no other way to do it in this case - label, name and value aren't suitable.

    I've already tried all kinds of code snippets, updated my Webrat, searched the web for half a day and asked my most experienced colleagues for help... nothing. I'm sure I'm not the first one to have this problem, so if anybody knows a way to get this to work, please help!

    Thanks in advance,

  • jazen

    jazen February 3rd, 2011 @ 12:37 PM

    Webrat's internals have changed since then - i use now (== the last time i used webrat, it's a while ago)

    When /^(?:|I )choose "([^\"]*)"$/ do |value|
      button = webrat.current_dom.search(".//input[@type='radio']").find do |element|
        element.attribute("value").value == value
      Webrat::Field.load(webrat, button).choose

    This checks the value not the id but should give you a hint how to proceed :)

  • Caya

    Caya February 3rd, 2011 @ 01:22 PM

    Hi jazen,

    first of all, thanks for your incredibly fast answer! I wouldn't have expected that after such a long time!

    Now, the first part where the button is chosen works fine. It picks the right one, yet the choose part still doesn't have an effect. Maybe there's something wrong with my radio buttons...? I'll post some of the page code, maybe something wrong sticks out to you:

    This is a part of the table that contains the radio buttons (I removed the uninteresting parts). It forms a kind of "matrix structure" on the page.

    As you can see, those in the first row are checked by default. What I'm trying to do is check the button with id "addrshipping2" in row 2.

        <input checked="checked" id="addrinvoice1" name="customer[billing_address_id]" type="radio" value="145">
      <td class="radio" headers="addrth2 addrtd1">
        <input checked="checked" id="addrshipping1" name="customer[delivery_address_id]" type="radio" value="145">
        <label for="addrshipping1">
          ... (lots of stuff in here, a whole address, to be precise)
        <input id="addrinvoice2" name="customer[billing_address_id]" type="radio" value="146">
        <input id="addrshipping2" name="customer[delivery_address_id]" type="radio" value="146">
        <label for="addrshipping2">
          ... (same, address with multiple lines in here)

    Do you think something about that structure could be the problem?

    Thanks a lot,

  • jeronimo

    jeronimo February 28th, 2011 @ 08:09 PM

    I'm having the same problem.

    jazen's suggestion didn't fix my problem either.

    I'd like to select by id as well.

    I tried posting on StackOverflow, but I'm not getting a response.

    How come no one else is having this problem?

    If you solve your problem, please post online.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

People watching this ticket