views:

249

answers:

3

I have a webpage that has a form button on it called "delete", and a cuke scenario that has the line:

And I should see "delete"

When I run the scenario, I get this error:

expected the following element's content to include "delete"

...and it dumps the webrat page to stdout and the "delete" is, in fact, not there. So far so good. However, when I tell webrat to show me the page before the error happens:

Then show me the page
And I should see "delete"

...Safari fires up and shows me the page, and in Safari there's the "delete" button, totally there.

Why is webrat not finding the form button? I've also had this same problem with form fields, such as text inputs that have a value in them when the page loads, but webrat says there's nothing there. Looking at it in Safari shows, again, that the field does have the right text in it.

Is this a bug, or is webrat just not suitable for checking form elements? Is there a different way to do this? Thanks!

+2  A: 

You can check for form elements by writing your own step definitions. For example, to find a button with a value of "delete", write your feature as:

And I should see a button with a value of "delete"

And a step definition of:

Then /^I should see a button with a value of "([^\"]*)"$/ do |value|
  response.should have_selector("form input[value=#{value}]")
end

As a side note, you'll notice that if your custom step fails, it will dump the entire response, and then you'll see the form element. I'm not entirely sure why this is, but apparently Cucumber is not matching the values of form elements when you write "I should see..."

Webrat claims to support CSS3 selectors for matching HTML elements when using the have_selector syntax.

zetetic
Great answer. Thanks!
Jason
I've used your solutions for a while and it works great but I accidentally discovered that doesn't seem to work with multiple words values like "delete it", solutions? tnx.
Leonardo Dario Perna
Seems like the regexp hould work with spaces, perhaps the matcher syntax is at fault -- it would help to see the actual code you're using. Maybe you could post this as a new question?
zetetic
A: 

Check if the HTML you are testing against is valid

aussiegeek
+1  A: 

To Leonardo:

For button labels with spaces in them, you need to include the value in quotation marks, like this:

Then /^I should see a button with a value of "([^\"]*)"$/ do |value|
  response.should have_selector("form input[value='#{value}']")
end
Mr K.