Checking a HTML element does not exist

6 views
Skip to first unread message

steven_noble

unread,
Oct 29, 2009, 6:24:41 AM10/29/09
to webrat
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
&quot;Concept&quot;:

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.

steven_noble

unread,
Oct 29, 2009, 7:08:09 AM10/29/09
to webrat
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.

Damian Janowski

unread,
Oct 29, 2009, 8:45:31 AM10/29/09
to web...@googlegroups.com
On Thu, Oct 29, 2009 at 7:24 AM, steven_noble <ste...@snoble.net> wrote:
>
> 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|

^^^^^^^^

It probably has nothing to do with the error, but note you're calling
have_selector on the response, not the selected ul.

>        li.should have_selector("a") do |a|
>          a.should assert_not_contain(name)
>        end
>      end
>    end
>  end
> end

I think the error is that you're mixing up the testing frameworks by
calling .should assert_not_contain :) I haven't done any RSpec in a
while, but I think it should be: a.should not_contain(name).

D.

David Chelimsky

unread,
Oct 31, 2009, 9:25:49 AM10/31/09
to webrat
a.should_not contain(name)

HTH,
David

>
> D.
Reply all
Reply to author
Forward
0 new messages