lib/watir/locators/element/locator.rb in watir-6.7.3 vs lib/watir/locators/element/locator.rb in watir-6.8.0
- old
+ new
@@ -67,11 +67,11 @@
return if !id.is_a?(String) || selector[:adjacent]
tag_name = selector.delete(:tag_name)
return unless selector.empty? # multiple attributes
- element = @query_scope.wd.find_element(:id, id)
+ element = locate_element(:id, id)
return if tag_name && !element_validator.validate(element, {tag_name: tag_name})
element
end
@@ -96,15 +96,15 @@
if how
# could build xpath/css for selector
if idx || !visible.nil?
idx ||= 0
- elements = @query_scope.wd.find_elements(how, what)
+ elements = locate_elements(how, what)
elements = elements.select { |el| visible == el.displayed? } unless visible.nil?
elements[idx] unless elements.nil?
else
- @query_scope.wd.find_element(how, what)
+ locate_element(how, what)
end
else
# can't use xpath, probably a regexp in there
if idx || !visible.nil?
idx ||= 0
@@ -117,10 +117,11 @@
end
end
def find_all_by_one
how, what = @selector.to_a.first
+ return [what] if how == :element
selector_builder.check_type how, what
if WD_FINDERS.include?(how)
wd_find_all_by(how, what)
else
@@ -136,21 +137,21 @@
raise ArgumentError, "can't locate all elements by :index"
end
how, what = selector_builder.build(selector)
found = if how
- @query_scope.wd.find_elements(how, what)
+ locate_elements(how, what)
else
wd_find_by_regexp_selector(selector, :select)
end
found.select! { |el| el.displayed? == visible } unless visible.nil?
found
end
def wd_find_all_by(how, what)
if what.is_a? String
- @query_scope.wd.find_elements(how, what)
+ locate_elements(how, what)
else
all_elements.select { |element| fetch_value(element, how) =~ what }
end
end
@@ -166,23 +167,23 @@
element.attribute(how.to_s.tr("_", "-").to_sym)
end
end
def all_elements
- @query_scope.wd.find_elements(xpath: ".//*")
+ locate_elements(:xpath, ".//*")
end
def wd_find_first_by(how, what)
if what.is_a? String
- @query_scope.wd.find_element(how, what)
+ locate_element(how, what)
else
all_elements.find { |element| fetch_value(element, how) =~ what }
end
end
def wd_find_by_regexp_selector(selector, method = :find)
- query_scope = @query_scope.wd
+ query_scope = ensure_scope_context
rx_selector = delete_regexps_from(selector)
if rx_selector.key?(:label) && selector_builder.should_use_label_element?
label = label_from_text(rx_selector.delete(:label)) || return
if (id = label.attribute(:for))
@@ -205,11 +206,11 @@
predicates = regexp_selector_to_predicates(key, value)
what = "(#{what})[#{predicates.join(' and ')}]" unless predicates.empty?
end
end
- elements = query_scope.find_elements(how, what)
+ elements = locate_elements(how, what, query_scope)
elements.__send__(method) { |el| matches_selector?(el, rx_selector) }
end
def delete_regexps_from(selector)
rx_selector = {}
@@ -223,11 +224,11 @@
rx_selector
end
def label_from_text(label_exp)
# TODO: this won't work correctly if @wd is a sub-element
- @query_scope.wd.find_elements(:tag_name, 'label').find do |el|
+ locate_elements(:tag_name, 'label').find do |el|
matches_selector?(el, text: label_exp)
end
end
def matches_selector?(element, selector)
@@ -249,9 +250,22 @@
lhs = selector_builder.xpath_builder.lhs_for(nil, key)
match.captures.reject(&:empty?).map do |literals|
"contains(#{lhs}, #{XpathSupport.escape(literals)})"
end
end
+
+ def ensure_scope_context
+ @query_scope.wd
+ end
+
+ def locate_element(how, what)
+ @query_scope.wd.find_element(how, what)
+ end
+
+ def locate_elements(how, what, scope = @query_scope.wd)
+ scope.find_elements(how, what)
+ end
+
end
end
end
end