I have the following step definitions:
Then /^I should see a control called "([^\"]*)"$/ do |name|
response.should have_selector("div", :class => "rightsidebar") do |
div|
div.should have_selector("ul") do |ul|
ul.should have_selector("li") do |li|
li.should have_selector("a") do |a|
a.should contain(name)
end
end
end
end
end
Then /^I should not see a control called "([^\"]*)"$/ do |name|
response.should have_selector("div", :class => "rightsidebar") do |
div|
div.should have_selector("ul") do |ul|
response.should have_selector("li") do |li|
li.should have_selector("a") do |a|
a.should assert_not_contain(name)
end
end
end
end
end
The following step passes...
And I should see a control called "Concept"
...when the output HTML contains this snippet:
<div class='rightsidebar'>
<ul>
<li>
<a href="foo" id="foo"><img alt="New" src="foo" /
>Concept</a>
</li>
</ul>
</div>
The following step passes...
And I should not see a control called "Concept"
...when the output HTML does not contain that snippet. But it fails
when the HTML contains this:
<div class="foo">
<ul>
<li class="foo" id="foo">Your Concepts</li>
</ul>
</div>
This seems crazy to me. That HTML shouldn't match the step definition.
The <div> is a different class to the one specified in the step
definition, and the <li> doesn't contains an <a>.
The specific error is:
expected the following element's content to not include
"Concept":
The above HTML is the only part of the output to contain the word
"Concept" which is why I believe it's what's matching the step
definition.
What can I do?
PS, I initially posted this to Stack Overflow (http://
stackoverflow.com/questions/1630052/checking-an-element-is-absent-
using-webrat) but you can't format code in comments on that site which
has killed the conversation, which is why I'm continuing it here.