views:

704

answers:

4

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.

+2  A: 
input("#my_box").should be_checked
+1  A: 

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"

Jesper Rønn-Jensen
A: 

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
Geoffrey Grosenbach
A: 
    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.

ramonrails