[ACCEPTED]-How can I determine if a radio button is checked already?-checked

Accepted answer
Score: 19
expect(find_field("radio_button_name")).to be_checked


Score: 9
input("#my_box").should be_checked


Score: 6

There are cases when you cannot rely on 7 checkboxes having ids or labels or where 6 label text changes. In this case you can 5 use the have_selector method from webrat.

From my working 4 code (where I do not have ids on checkboxes).

response_body.should have_selector 'input[type=radio][checked=checked][value=information]'

Explanation: test 3 will return true if body of document contains 2 an radio button (input[type=radio]) which is checked and 1 that has the value "information"

Score: 2

Just changed a web_step checkbox to radio 3 button

Add the following step to web_steps.rb

Then /^the "([^"]*)" radio_button(?: within "([^"]*)")? should be checked$/ do |label, selector|
  with_scope(selector) do
    field_checked = find_field(label)['checked']
    if field_checked.respond_to? :should
      field_checked.should be_true
      assert field_checked

And 2 you can write the following to check whether 1 the given raido button is checked or not

And the "Bacon" radio_button within "div.radio_container" should be checked
Score: 1

You can use the built-in checkbox matcher 9 in web_steps.rb:

And the "Bacon" checkbox should be checked

However, you'll need to 8 have a label on your checkbox that matches 7 the ID of the corresponding checkbox input 6 field. The f.label helper in Rails takes 5 a string to use as the ID in the first argument. You 4 may have to build a string that includes 3 the field name and the checkbox name:

f.label "lunch_#{food_name}, food_name
f.radio_button :lunch, food_name

In 2 any case, use this directive to see that 1 you've got the HTML correct:

Then show me the page
Score: 1

Wrapped Jesper Rønn-Jensen his function 1 + added name which is used by rails:

Then /^I should see that "([^"]*)" is checked from "([^"]*)"$/ do |value, name|
  page.should have_selector "input[type='radio'][checked='checked'][value='#{value}'][name='#{name}']"
Score: 0
    And the "Obvious choice" checkbox should be checked

Although it might be a radio button, but 2 the code will work. It is just checking 1 for a fields labelled with that text.

Score: 0

You can use method checked? on your field

expect(find_field("radio_button_id").checked?).to eq(true)


More Related questions