Method: Capybara::Node::Finders#all

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

- (Capybara::Result) all([kind], locator, options)

Find all elements on the page matching the given selector and options.

Both XPath and CSS expressions are supported, but Capybara does not try to automatically distinguish between them. The following statements are equivalent:

page.all(:css, 'a#person_123')
page.all(:xpath, '//a[@id="person_123"]')

If the type of selector is left out, Capybara uses Capybara.default_selector. It's set to :css by default.

page.all("a#person_123")

Capybara.default_selector = :xpath
page.all('//a[@id="person_123"]')

The set of found elements can further be restricted by specifying options. It's possible to select elements by their text or visibility:

page.all('a', :text => 'Home')
page.all('#menu li', :visible => true)

By default if no elements are found, an empty array is returned; however, expectations can be set on the number of elements to be found using:

page.assert_selector('p#foo', :count => 4)
page.assert_selector('p#foo', :maximum => 10)
page.assert_selector('p#foo', :minimum => 1)
page.assert_selector('p#foo', :between => 1..10)

See Helpers#matches_count? for additional information about count matching.

Parameters:

  • kind (:css, :xpath)

    The type of selector

  • locator (String)

    The selector

Options Hash (options):

  • text (String, Regexp)

    Only find elements which contain this text or match this regexp

  • visible (Boolean)

    Only find elements that are visible on the page. Setting this to false finds invisible and visible elements.

  • count (Integer)

    Exact number of matches that are expected to be found

  • maximum (Integer)

    Maximum number of matches that are expected to be found

  • minimum (Integer)

    Minimum number of matches that are expected to be found

  • between (Range)

    Number of matches found must be within the given range

  • exact (Boolean)

    Control whether `is` expressions in the given XPath match exactly or partially

Returns:



144
145
146
147
148
149
150
151
# File 'lib/capybara/node/finders.rb', line 144

def all(*args)
  query = Capybara::Query.new(*args)
  synchronize(query.wait) do
    result = query.resolve_for(self)
    raise Capybara::ExpectationNotMet, result.failure_message unless result.matches_count?
    result
  end
end

Comments