Flowdock
method

assert_tag

Importance_4
v1.1.1 - Show latest stable - 2 notes - Class: Test::Unit::Assertions
assert_tag(*opts) public

Asserts that there is a tag/node/element in the body of the response that meets all of the given conditions. The conditions parameter must be a hash of any of the following keys (all are optional):

  • :tag: the node type must match the corresponding value
  • :attributes: a hash. The node’s attributes must match the corresponding values in the hash.
  • :parent: a hash. The node’s parent must match the corresponding hash.
  • :child: a hash. At least one of the node’s immediate children must meet the criteria described by the hash.
  • :ancestor: a hash. At least one of the node’s ancestors must meet the criteria described by the hash.
  • :descendant: a hash. At least one of the node’s descendants must meet the criteria described by the hash.
  • :sibling: a hash. At least one of the node’s siblings must meet the criteria described by the hash.
  • :after: a hash. The node must be after any sibling meeting the criteria described by the hash, and at least one sibling must match.
  • :before: a hash. The node must be before any sibling meeting the criteria described by the hash, and at least one sibling must match.
  • :children: a hash, for counting children of a node. Accepts the keys:
    • :count: either a number or a range which must equal (or include) the number of children that match.
    • :less_than: the number of matching children must be less than this number.
    • :greater_than: the number of matching children must be greater than this number.
    • :only: another hash consisting of the keys to use to match on the children, and only matching children will be counted.
  • :content: the textual content of the node must match the
      given value. This will not match HTML tags in the body of a
      tag--only text.
    

Conditions are matched using the following algorithm:

  • if the condition is a string, it must be a substring of the value.
  • if the condition is a regexp, it must match the value.
  • if the condition is a number, the value must match number.to_s.
  • if the condition is true, the value must not be nil.
  • if the condition is false or nil, the value must be nil.

Usage:

  # assert that there is a "span" tag
  assert_tag :tag => "span"

  # assert that there is a "span" tag with id="x"
  assert_tag :tag => "span", :attributes => { :id => "x" }

  # assert that there is a "span" tag using the short-hand
  assert_tag :span

  # assert that there is a "span" tag with id="x" using the short-hand
  assert_tag :span, :attributes => { :id => "x" }

  # assert that there is a "span" inside of a "div"
  assert_tag :tag => "span", :parent => { :tag => "div" }

  # assert that there is a "span" somewhere inside a table
  assert_tag :tag => "span", :ancestor => { :tag => "table" }

  # assert that there is a "span" with at least one "em" child
  assert_tag :tag => "span", :child => { :tag => "em" }

  # assert that there is a "span" containing a (possibly nested)
  # "strong" tag.
  assert_tag :tag => "span", :descendant => { :tag => "strong" }

  # assert that there is a "span" containing between 2 and 4 "em" tags
  # as immediate children
  assert_tag :tag => "span",
             :children => { :count => 2..4, :only => { :tag => "em" } }

  # get funky: assert that there is a "div", with an "ul" ancestor
  # and an "li" parent (with "class" = "enum"), and containing a
  # "span" descendant that contains text matching /hello world/
  assert_tag :tag => "div",
             :ancestor => { :tag => "ul" },
             :parent => { :tag => "li",
                          :attributes => { :class => "enum" } },
             :descendant => { :tag => "span",
                              :child => /hello world/ }

<strong>Please note</strong: #assert_tag and #assert_no_tag only work with well-formed XHTML. They recognize a few tags as implicitly self-closing (like br and hr and such) but will not work correctly with tags that allow optional closing tags (p, li, td). You must explicitly close all of your tags to use these assertions.

Show source
Register or log in to add new notes.
September 23, 2008
1 thank

Assert empty option in select tag

assert_tag :tag => “select”, :attributes => {:id => “to_airport_id”},

:child => {:tag => "option", :attributes => {:value => ""}, :content => "--select--"},
:children => {:count => 4}
October 11, 2008 - (v2.0.0 - v2.0.3)
1 thank

Assert tag children

Code

#<div id="example">
#  <ul>
#    <li class="btn_1"><a href="#">link1</a></li>
#    <li class="btn_2"><a href="#">link2</a></li>
#  </ul>
#</div>

assert_tag :tag => 'div',
  :attributes => {:id => "example"},
  :child => {
    :tag => 'ul',
    :child => {
      :tag => 'li',
      :attributes => {:class => 'btn_1'},
      :child => {
        :tag => 'a',
        :attributes => {:href => '#'},
        :content => 'link1'
      },
      :sibling => {
        :tag => 'li',
        :attributes => {:class => 'btn_2'},
        :child => {
          :tag => 'a',
          :attributes => {:href => '#'},
          :content => /link/
        }
      }
    }
  }