Module

ActionController::Assertions::SelectorAssertions

Inheritance

Adds the assert_select method for use in Rails functional test cases, which can be used to make assertions on the response HTML of a controller action. You can also call assert_select within another assert_select to make assertions on elements selected by the enclosing assertion.

Use css_select to select elements without making an assertions, either from the response HTML or elements selected by the enclosing assertion.

In addition to HTML responses, you can make the following assertions:

Also see HTML::Selector to learn how to use selectors.

Constants

Name   Description
RJS_ANY_ID = "\"([^\"])*\""
RJS_INSERTIONS = ["top", "bottom", "before", "after"]
RJS_PATTERN_HTML = "\"((\\\\\"|[^\"])*)\""
RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
RJS_STATEMENTS = { :chained_replace => "\\$\\(#{RJS_ANY_ID}\\)\\.replace\\(#{RJS_PATTERN_HTML}\\)", :chained_replace_html => "\\$\\(#{RJS_ANY_ID}\\)\\.update\\(#{RJS_PATTERN_HTML}\\)", :replace_html => "Element\\.update\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)", :replace => "Element\\.replace\\(#{RJS_ANY_ID}, #{RJS_PATTERN_HTML}\\)"

Methods

Instance

Visibility Signature
public assert_select (*args) {|matches| ...}
public assert_select_email (&block)
public assert_select_encoded (element = nil, &block)
public assert_select_rjs (*args) {|matches| ...}
public css_select (*args)
protected response_from_page_or_rjs ()
protected unescape_rjs (rjs_string)

Instance Method Detail

assert_select(selector, equality?, message?)
assert_select(element, selector, equality?, message?)

An assertion that selects elements and makes one or more equality tests.

If the first argument is an element, selects all matching elements starting from (and including) that element and all its children in depth-first order.

If no element if specified, calling assert_select selects from the response HTML unless assert_select is called from within an assert_select block.

When called with a block assert_select passes an array of selected elements to the block. Calling assert_select from the block, with no element specified, runs the assertion on the complete set of elements selected by the enclosing assertion. Alternatively the array may be iterated through so that assert_select can be called separately for each element.

Example

If the response contains two ordered lists, each with four list elements then:

  assert_select "ol" do |elements|
    elements.each do |element|
      assert_select element, "li", 4
    end
  end

will pass, as will:

  assert_select "ol" do
    assert_select "li", 8
  end

The selector may be a CSS selector expression (String), an expression with substitution values, or an HTML::Selector object.

Equality Tests

The equality test may be one of the following:

  • true - Assertion is true if at least one element selected.
  • false - Assertion is true if no element selected.
  • String/Regexp - Assertion is true if the text value of at least one element matches the string or regular expression.
  • Integer - Assertion is true if exactly that number of elements are selected.
  • Range - Assertion is true if the number of selected elements fit the range.

If no equality test specified, the assertion is true if at least one element selected.

To perform more than one equality tests, use a hash with the following keys:

  • :text - Narrow the selection to elements that have this text value (string or regexp).
  • :html - Narrow the selection to elements that have this HTML content (string or regexp).
  • :count - Assertion is true if the number of selected elements is equal to this value.
  • :minimum - Assertion is true if the number of selected elements is at least this value.
  • :maximum - Assertion is true if the number of selected elements is at most this value.

If the method is called with a block, once all equality tests are evaluated the block is called with an array of all matched elements.

Examples

  # At least one form element
  assert_select "form"

  # Form element includes four input fields
  assert_select "form input", 4

  # Page title is "Welcome"
  assert_select "title", "Welcome"

  # Page title is "Welcome" and there is only one title element
  assert_select "title", {:count=>1, :text=>"Welcome"},
      "Wrong title or more than one title element"

  # Page contains no forms
  assert_select "form", false, "This page must contain no forms"

  # Test the content and style
  assert_select "body div.header ul.menu"

  # Use substitution values
  assert_select "ol>li#?", /item-\d+/

  # All input fields in the form have a name
  assert_select "form input" do
    assert_select "[name=?]", /.+/  # Not empty
  end

assert_select_email { }

Extracts the body of an email and runs nested assertions on it.

You must enable deliveries for this assertion to work, use:

  ActionMailer::Base.perform_deliveries = true

Examples

 assert_select_email do
   assert_select "h1", "Email alert"
 end

 assert_select_email do
   items = assert_select "ol>li"
   items.each do
      # Work with items here...
   end
 end

assert_select_encoded(element?) { |elements| ... }

Extracts the content of an element, treats it as encoded HTML and runs nested assertion on it.

You typically call this method within another assertion to operate on all currently selected elements. You can also pass an element or array of elements.

The content of each element is un-encoded, and wrapped in the root element encoded. It then calls the block with all un-encoded elements.

Examples

  # Selects all bold tags from within the title of an ATOM feed's entries (perhaps to nab a section name prefix)
  assert_select_feed :atom, 1.0 do
    # Select each entry item and then the title item
    assert_select "entry>title" do
      # Run assertions on the encoded title elements
      assert_select_encoded do
        assert_select "b"
      end
    end
  end

  # Selects all paragraph tags from within the description of an RSS feed
  assert_select_feed :rss, 2.0 do
    # Select description element of each feed item.
    assert_select "channel>item>description" do
      # Run assertions on the encoded elements.
      assert_select_encoded do
        assert_select "p"
      end
    end
  end

assert_select_rjs(id?) { |elements| ... }
assert_select_rjs(statement, id?) { |elements| ... }
assert_select_rjs(:insert, position, id?) { |elements| ... }

Selects content from the RJS response.

Narrowing down

With no arguments, asserts that one or more elements are updated or inserted by RJS statements.

Use the id argument to narrow down the assertion to only statements that update or insert an element with that identifier.

Use the first argument to narrow down assertions to only statements of that type. Possible values are :replace, :replace_html, :show, :hide, :toggle, :remove and :insert_html.

Use the argument :insert followed by an insertion position to narrow down the assertion to only statements that insert elements in that position. Possible values are :top, :bottom, :before and :after.

Using the :remove statement, you will be able to pass a block, but it will be ignored as there is no HTML passed for this statement.

Using blocks

Without a block, assert_select_rjs merely asserts that the response contains one or more RJS statements that replace or update content.

With a block, assert_select_rjs also selects all elements used in these statements and passes them to the block. Nested assertions are supported.

Calling assert_select_rjs with no arguments and using nested asserts asserts that the HTML content is returned by one or more RJS statements. Using assert_select directly makes the same assertion on the content, but without distinguishing whether the content is returned in an HTML or JavaScript.

Examples

  # Replacing the element foo.
  # page.replace 'foo', ...
  assert_select_rjs :replace, "foo"

  # Replacing with the chained RJS proxy.
  # page[:foo].replace ...
  assert_select_rjs :chained_replace, 'foo'

  # Inserting into the element bar, top position.
  assert_select_rjs :insert, :top, "bar"

  # Remove the element bar
  assert_select_rjs :remove, "bar"

  # Changing the element foo, with an image.
  assert_select_rjs "foo" do
    assert_select "img[src=/images/logo.gif""
  end

  # RJS inserts or updates a list with four items.
  assert_select_rjs do
    assert_select "ol>li", 4
  end

  # The same, but shorter.
  assert_select "ol>li", 4

css_select(selector) => array
css_select(element, selector) => array

Select and return all matching elements.

If called with a single argument, uses that argument as a selector to match all elements of the current page. Returns an empty array if no match is found.

If called with two arguments, uses the first argument as the base element and the second argument as the selector. Attempts to match the base element and any of its children. Returns an empty array if no match is found.

The selector may be a CSS selector expression (String), an expression with substitution values (Array) or an HTML::Selector object.

Examples

  # Selects all div tags
  divs = css_select("div")

  # Selects all paragraph tags and does something interesting
  pars = css_select("p")
  pars.each do |par|
    # Do something fun with paragraphs here...
  end

  # Selects all list items in unordered lists
  items = css_select("ul>li")

  # Selects all form tags and then all inputs inside the form
  forms = css_select("form")
  forms.each do |form|
    inputs = css_select(form, "input")
    ...
  end

response_from_page_or_rjs()

assert_select and css_select call this to obtain the content in the HTML page, or from all the RJS statements, depending on the type of response.

unescape_rjs(rjs_string)

Unescapes a RJS string.