views:

192

answers:

3

The following works just fine:

Then /^I should see a button called "([^\"]*)"$/ do |name|
  response.should have_selector("li") do |li|
    li.should have_selector("a") do |a|
      a.should contain(name)
    end
  end
end

Now I need to complement it with a step like this:

Then /^I should not see a button called "([^\"]*)"$/ do |name|
   [snip...]
end

The standard "And I should not see" is not satisfactory, as it will pick up the target phrase anywhere in the page -- not just within buttons. I specifically need to check that no button with the target text are present.

My first instinct was to try something like this:

Then /^I should see a button called "([^\"]*)"$/ do |name|
  response.should have_selector("li") do |li|
    li.should have_selector("a") do |a|
      a.should_not contain(name)
    end
  end
end

But of course that will pass as long as there's any button on the page that does not include the target text, even if there are also one or more buttons that do include the target text.

Your thoughts?

Many thanks,

Steven.

+2  A: 

You can use assert_not_contain (and maybe not_contain but I doubt it as it's not in the rdoc).

a.should assert_not_contain(name)
Damien MATHIEU
yep, assert_not_contain(name) is the method you want if you take this approach.
Rodreegez
It worked. Much appreciated.
steven_noble
Actually, not so quickly...but I can't paste code into a comment box, so I'm continuing over in the WebRat Google Group.
steven_noble
Well you can update your question with the additionnal informations.
Damien MATHIEU
A: 

Are your buttons actually links? For buttons (where there is a submit value), I use:

assert_have_selector "input[type=submit][value=\"#{text}\"]"

and:

assert_have_no_selector "input[type=submit][value=\"#{text}\"]"

hth.

Rodreegez
Yes, you are right. Technically they are links. I'm just calling them buttons because that's how they appear in the interface.
steven_noble
You got me thinking. I've just changed "button" to "control" in all my tests.
steven_noble
+1  A: 

Problem solved! I gave every control a class of "control", then wrote this step definition:

Then /^I should not see a control called "([^\"]*)"$/ do |name|
  response.should have_selector("a.control", :content => name, :count
=> 0)
end

Of course, this presumes I'm happy giving them all a class of "control"...

s.

steven_noble
glad you solved it!
Rodreegez