Make `first` use `all` behind the scenes
The performance gain is not worth the added complexity
This commit is contained in:
parent
3e76ffd8e5
commit
5def28a7b5
|
@ -122,9 +122,6 @@ module Capybara
|
||||||
# Find the first element on the page matching the given selector
|
# Find the first element on the page matching the given selector
|
||||||
# and options, or nil if no element matches.
|
# and options, or nil if no element matches.
|
||||||
#
|
#
|
||||||
# When only the first matching element is needed, this method can
|
|
||||||
# be faster than all(*args).first.
|
|
||||||
#
|
|
||||||
# @overload first([kind], locator, options)
|
# @overload first([kind], locator, options)
|
||||||
# @param [:css, :xpath] kind The type of selector
|
# @param [:css, :xpath] kind The type of selector
|
||||||
# @param [String] locator The selector
|
# @param [String] locator The selector
|
||||||
|
@ -132,19 +129,12 @@ module Capybara
|
||||||
# @return [Capybara::Element] The found element or nil
|
# @return [Capybara::Element] The found element or nil
|
||||||
#
|
#
|
||||||
def first(*args)
|
def first(*args)
|
||||||
args, options = extract_normalized_options(args)
|
results = all(*args)
|
||||||
found_elements = []
|
if Capybara.prefer_visible_elements
|
||||||
|
results.find(&:visible?) or results.first
|
||||||
selector = Capybara::Selector.normalize(*args)
|
else
|
||||||
selector.xpaths.each do |path|
|
results.first
|
||||||
find_in_base(selector, path).each do |node|
|
|
||||||
if selector.matches_filters?(node)
|
|
||||||
found_elements << node
|
|
||||||
return found_elements.last if not Capybara.prefer_visible_elements or node.visible?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
found_elements.first
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
Loading…
Reference in New Issue