Verifying select field value with Cucumber and Webrat
I’ve been using Cucumber and Webrat for only a couple months now. Today I needed something new and it wasn’t obvious how to make it work.
Here’s an example of a test in Cucumber:
Given I am Sam When I follow "Foo" Then the "Bar" field should contain "Baz"
Which relies on Webrat:
Then /^the "([^"]*)" field should contain "([^"]*)"$/ do |field, value|
field_labeled(field).value.should =~ /#{value}/
end
But what if it’s not a text field? What if it’s a select? Then we need to check which value is selected.
Given Sam I am When I follow "Foo" Then "Baz" should be selected for "Bar"
Here’s my solution:
Then /^"([^"]*)" should be selected for "([^"]*)"$/ do |value, field|
field_labeled(field).element.search(".//option[@selected = 'selected']").inner_html.should =~ /#{value}/
end
Hope this helps!
