Testing the view of an application with functional tests, how would one
go about checking that there are a certain number of elements
present on a page, which all belong to a certain class.
I tried:
assert_select "form" do
assert_select "span", :class=> "required_field", :count => 13
end
But this just found all of the span elements on the page and returned
false.
But this just found all of the span elements on the page and returned
false.
At a guess…
assert_xhtml do
form :xpath! => ‘count(.//span[ @class = “required_field” ]) = 13’
end
If that doesn’t work, look up the XPath syntax for count(). If that still
doesn’t work, throw away assert_xhtml, and drop down raw XPath thru
Nokogiri to
do it.
(gem install rkelly nokogiri assert2, and require ‘assert2/xhtml’, to
get
assert_xhtml, IIRC…)
To do what you want with assert_select you can use
assert_select “form>span[title=required field]”, 13
That is there should be 13 span elements with given title inside form
elements
or
assert_select “form” do
assert_select “span[title=required field]”, 13
end
if you want to insist that all 13 are in the same form
I just looked again at the html rails is generating and in fact the
element I’m looking for looks like this:
*
not:
*
Would I test for this like so:
assert_select "form" do
assert_select("span.title.required field", :count => 13)
end
When I run it, I get the error message:
Expected at least 13 elements matching “span.required_field”, found 0.
I installed the recommended gem and using that the test works just fine:
assert_xhtml do
form :xpath! => 'count(.//span[ @title= "required field" ]) =
13’
end
One question however, is there any way to turn off the “…in this
sample…” aspect of the output, as if the does find any errors, it
returns my entire html page.