How can I determine if a radio button is checked already?
Asked Answered
N

8

12

I'm using webrat with cucumber and I would like to test if a radio button is checked already when I am on a page. How can I do that ? I didn't find any step in webrat which can do that.

Nape answered 23/7, 2009 at 19:43 Comment(0)
F
21
expect(find_field("radio_button_name")).to be_checked
Flews answered 22/2, 2014 at 1:37 Comment(0)
K
9
input("#my_box").should be_checked
Ki answered 14/8, 2009 at 1:19 Comment(2)
Seems this method no longer works. I'm using Capybara 2.4.1. The find_field method suggested by @Flews did the trick.Dayton
I would suggest not using "should be_checked" but instead: expect(find_field("my_boxt")).to be_checkedScholz
R
6

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

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

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

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

Runic answered 16/12, 2009 at 13:54 Comment(0)
M
2

Just changed a web_step checkbox to radio 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
    else
      assert field_checked
    end
  end
end

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

And the "Bacon" radio_button within "div.radio_container" should be checked
Mattoid answered 10/3, 2011 at 11:4 Comment(0)
L
1

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

And the "Bacon" checkbox should be checked

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

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

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

Then show me the page
Lotte answered 27/1, 2010 at 21:28 Comment(0)
H
1

Wrapped Jesper Rønn-Jensen his function + 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}']"
end
Housewarming answered 4/10, 2011 at 13:1 Comment(0)
R
0
    And the "Obvious choice" checkbox should be checked

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

Rissole answered 13/4, 2010 at 20:21 Comment(0)
C
0

You can use method checked? on your field

expect(find_field("radio_button_id").checked?).to eq(true)
Carotid answered 12/7, 2018 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.