Method: Rails::Dom::Testing::Assertions::SelectorAssertions#assert_select

Defined in:
lib/rails/dom/testing/assertions/selector_assertions.rb

#assert_select(*args, &block) ⇒ Object

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 is specified assert_select selects from the element returned in document_root_element unless assert_select is called from within an assert_select block. Override document_root_element to tell assert_select what to select from. The default implementation raises an exception explaining this.

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) or an expression with substitution values (Array). Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution. assert_select returns nil if called with an invalid css selector.

assert_select “div:match('id', ?)”, /d+/

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.

# 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:match('id', ?)", /item-\d+/

# All input fields in the form have a name
assert_select "form input" do
  assert_select ":match('name', ?)", /.+/  # Not empty
end


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rails/dom/testing/assertions/selector_assertions.rb', line 164

def assert_select(*args, &block)
  @selected ||= nil

  selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }

  if selecting_no_body?(selector)
    assert true
    return
  end

  selector.select.tap do |matches|
    assert_size_match!(matches.size, selector.tests, selector.selector, selector.message)

    nest_selection(matches, &block) if block_given? && !matches.empty?
  end
rescue Nokogiri::CSS::SyntaxError => e
  ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
  return
end