Method: Capybara::Node::Finders#find_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

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 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 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)

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time this method will wait is controlled through default_max_wait_time.

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?

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

Returns:

Raises:


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/capybara/node/finders.rb', line 275

def all(*args, allow_reload: false, **options, &optional_filter_block)
  minimum_specified = options_include_minimum?(options)
  options = { minimum: 1 }.merge(options) unless minimum_specified
  options[:session_options] = session_options
  query = Capybara::Queries::SelectorQuery.new(*args, **options, &optional_filter_block)
  result = nil
  begin
    synchronize(query.wait) do
      result = query.resolve_for(self)
      result.allow_reload! if allow_reload
      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