method

assert_select

Importance_5
Ruby on Rails latest stable (v7.1.3.2) - 8 notes - Class: ActionController::Assertions::SelectorAssertions

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

assert_select(*args, &block) public

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
Show source
Register or log in to add new notes.
January 20, 2010
4 thanks

Using html text instead of default response

If you have a string containing html and want to assert_select against it, as the doc states you have to pass in an element (HTML::Node) as the first argument. You can do something like this:

doc = HTML::Document.new('<p><span>example</span></p>')
assert_select doc.root, 'span'
January 20, 2010
2 thanks

Escape brackets in selector

If you need to escape brackets in a selector, this is the way to do it:

assert_select "input[type=hidden][name='user[role_ids][]']"
April 12, 2009
1 thank

To find a tag with an id or class

assert_select ‘td.highlight’, { :count => 2 }

finds 2 td tags with the highlight class.

assert_select ‘div#special’

finds div with id=special

August 21, 2009
1 thank

How not to find an element

assert_select ‘div’, :count => 0

September 14, 2011
0 thanks

Find a Selected Option in a Drop Down.

You can find the selected option (or options for multiple select inputs) with the following:

assert_select('select#membership_year option[selected]').first['value']
July 16, 2009
0 thanks

Also works with other markup

such as XML, not only HTML as suggested in the text.

August 2, 2010
0 thanks

assert_select negative with regex

(also, you can use instance vars)

assert_select "div#event_#{assigns[event].id}", { :count => 0, :html => /something/ }
August 15, 2009
0 thanks

To find element ant not to find element

If you want to see administration panel:

assert_select "div.admin-panel"

But if you want to NOT see administration panel just write:

assert_no_tag 'div', :attributes => {:class => 'admin-panel'}