Forum: Ruby on Rails assert_select issue

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
428167a3ec72235ba971162924492609?d=identicon&s=25 Yehuda Katz (Guest)
on 2007-01-23 04:33
I'd like to do an assert_select where only ONE of the matching elements
needs to match the :text.

It seems counterintuitive to me to require them *all* to match, which
precludes me from testing the existence of a particular tag (h1) with
some particular text if other instances of the same tag exist.

I know that I can narrow down the field by using more detailed
selectors, but it sure would be nice to be able to optionally use :text
as an additional selector (and then be able to determine the number of
elements matching the selector and containing the text, or matching the
text RegExp).

Any ideas?
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-01-23 17:06
(Received via mailing list)
Yehuda Katz wrote:

> Any ideas?

Use assert_select to select the node around the target node you need.
Then do this:

  assert_select 'some node' do |node|
    assert_xml node.to_s
    assert_xpath 'match_me[ . = "contents" ]'

Problem: XPath can't do RegExps (last time I checked). But it can do
any other kind of query you can think of, competitive with query
systems like SQL SELECT.

The assert_xpath support code is below my sig.

  Phlip  <-- NOT a blog!!

require 'rexml/document'
include REXML

module AssertXPath

  def assert_xml(contents)
    contents.gsub!('\\\'', '&apos;')
    @xdoc =

  def assert_xpath(path, message = '')
    former = @xdoc
    @xdoc = XPath.first(@xdoc, path)

    assert_not_nil @xdoc, message +
            "\nseeking: #{path.inspect} in\n#{former.to_s}"

    yield(@xdoc) if block_given?
    @xdoc = former

This topic is locked and can not be replied to.