Module | ActionDispatch::Assertions::SelectorAssertions |
In: |
lib/action_dispatch/testing/assertions/selector.rb
|
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.
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.
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.
The equality test may be one of the following:
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:
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#?", /item-\d+/ # All input fields in the form have a name assert_select "form input" do assert_select "[name=?]", /.+/ # Not empty end
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
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
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.
# 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
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.
# 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