lib/watir/locators/element/locator.rb in watir-6.8.0 vs lib/watir/locators/element/locator.rb in watir-6.8.1

- old
+ new

@@ -94,25 +94,21 @@ how, what = selector_builder.build(selector) if how # could build xpath/css for selector - if idx || !visible.nil? - idx ||= 0 + if idx && idx != 0 || !visible.nil? elements = locate_elements(how, what) - elements = elements.select { |el| visible == el.displayed? } unless visible.nil? - elements[idx] unless elements.nil? + filter_elements elements, visible, idx, :single else locate_element(how, what) end else # can't use xpath, probably a regexp in there - if idx || !visible.nil? - idx ||= 0 + if idx && idx != 0 || !visible.nil? elements = wd_find_by_regexp_selector(selector, :select) - elements = elements.select { |el| visible == el.displayed? } unless visible.nil? - elements[idx] unless elements.nil? + filter_elements elements, visible, idx, :single else wd_find_by_regexp_selector(selector, :find) end end end @@ -141,12 +137,11 @@ found = if how locate_elements(how, what) else wd_find_by_regexp_selector(selector, :select) end - found.select! { |el| el.displayed? == visible } unless visible.nil? - found + filter_elements found, visible, nil, :multiple end def wd_find_all_by(how, what) if what.is_a? String locate_elements(how, what) @@ -207,10 +202,19 @@ what = "(#{what})[#{predicates.join(' and ')}]" unless predicates.empty? end end elements = locate_elements(how, what, query_scope) - elements.__send__(method) { |el| matches_selector?(el, rx_selector) } + filter_elements_by_regex(elements, rx_selector, method) + end + + def filter_elements elements, visible, idx, number + elements.select! { |el| visible == el.displayed? } unless visible.nil? + number == :single ? elements[idx || 0] : elements + end + + def filter_elements_by_regex(elements, selector, method) + elements.__send__(method) { |el| matches_selector?(el, selector) } end def delete_regexps_from(selector) rx_selector = {}