Method: Capybara::Node::Finders#all

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

#all([kind = Capybara.default_selector], locator = nil, options = {}) ⇒ Capybara::Result #all([kind = Capybara.default_selector], locator = nil, options = {}) {|element| ... } ⇒ Capybara::Result Also known as: find_all

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 Capybara's waiting behavior will wait up to `Capybara.default_max_wait_time` seconds for matching elements to be available and then return an empty result if none are available. It is possible to set expectations on the number of results located and Capybara will raise an exception if the number of elements located don't satisfy the specified conditions. The expectations can be set 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.

Overloads:

  • #all([kind = Capybara.default_selector], locator = nil, options = {}) {|element| ... } ⇒ Capybara::Result

    Yield Parameters:

    Yield Returns:

    • (Boolean)

      Should the element be considered in the results?

Options Hash (options):

  • text (String, Regexp)

    Only find elements which contain this text or match this regexp

  • exact_text (String, Boolean) — default: Capybara.exact_text

    When String the elements contained text must match exactly, when Boolean controls whether the :text option must match exactly

  • visible (Boolean, Symbol)

    Only find elements with the specified visibility:

    • true - only finds visible elements.

    • false - finds invisible and visible elements.

    • :all - same as false; finds visible and invisible elements.

    • :hidden - only finds invisible elements.

    • :visible - same as true; only finds 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

  • wait (Integer, false) — default: Capybara.default_max_wait_time

    The time to wait for matching elements to become available

Raises:



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/capybara/node/finders.rb', line 244

def all(*args, **options, &optional_filter_block)
  minimum_specified = %i[count minimum between].any? { |k| options.key?(k) }
  options = { minimum: 1 }.merge(options) unless minimum_specified
  options[:session_options] = session_options
  query = Capybara::Queries::SelectorQuery.new(*args.push(options), &optional_filter_block)
  result = nil
  begin
    synchronize(query.wait) do
      result = query.resolve_for(self)
      raise Capybara::ExpectationNotMet, result.failure_message unless result.matches_count?
      result
    end
  rescue Capybara::ExpectationNotMet
    raise if minimum_specified || (result.compare_count == 1)
    Result.new([], nil)
  end
end