Method: Capybara::Node::Matchers#assert_selector

Defined in:
lib/capybara/node/matchers.rb

#assert_selector(*args, &optional_filter_block) ⇒ Object

Asserts that a given selector is on the page or a descendant of the current node.

page.assert_selector('p#foo')
page.assert_selector(:xpath, './/p[@id="foo"]')
page.assert_selector(:foo)

By default it will check if the expression occurs at least once, but a different number can be specified.

page.assert_selector('p#foo', count: 4)

This will check if the expression occurs exactly 4 times. See Finders#all for other available result size options.

If a :count of 0 is specified, it will behave like #assert_no_selector; however, use of that method is preferred over this one.

It also accepts all options that Finders#all accepts, such as :text and :visible.

page.assert_selector('li', text: 'Horse', visible: true)

#assert_selector can also accept XPath expressions generated by the XPath gem:

page.assert_selector(:xpath, XPath.descendant(:p))

Parameters:

  • kind (Symbol)

    Optional selector type (:css, :xpath, :field, etc.). Defaults to default_selector.

  • locator (String)

    The locator for the specified selector

  • options (Hash)

    a customizable set of options

Raises:



109
110
111
112
113
114
115
# File 'lib/capybara/node/matchers.rb', line 109

def assert_selector(*args, &optional_filter_block)
  _verify_selector_result(args, optional_filter_block) do |result, query|
    unless result.matches_count? && (result.any? || query.expects_none?)
      raise Capybara::ExpectationNotMet, result.failure_message
    end
  end
end