Module | ActionController::Assertions::SelectorAssertions
In: | lib/action_controller/assertions/selector_assertions.rb

Adds the assert_select method for use in Rails functional test cases.

Use assert_select 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:

- assert_select_rjs — Assertions on HTML content of RJS update and

        insertion operations.

- assert_select_encoded — Assertions on HTML encoded inside XML,

        for example for dealing with feed item descriptions.

- assert_select_email — Assertions on the HTML body of an e-mail.

Also see HTML::Selector for learning how to use selectors.

### Methods

assert_select assert_select_email assert_select_encoded assert_select_rjs css_select response_from_page_or_rjs unescape_rjs

### Constants

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

### Public Instance methods

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 will select from the response HTML. Calling assert_select inside an assert_select block will run the assertion for each element selected by the enclosing assertion.

For example:

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

Or for short:

      assert_select "ol>li" do
        assert_select "li"
      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

### Example

assert_select_email do

      assert_select "h1", "Email alert"

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.

### Example

      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+ 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 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"

      # 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.

For example:

      forms = css_select("form")
      forms.each do |form|
        inputs = css_select(form, "input")
        ...
      end

### Protected Instance methods

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.

[Validate]