lib/watir/locators/element/locator.rb in watir-6.0.0.beta4 vs lib/watir/locators/element/locator.rb in watir-6.0.0.beta5

- old
+ new

@@ -36,15 +36,15 @@ end def locate e = by_id and return e # short-circuit if :id is given - if @selector.size == 1 - element = find_first_by_one - else - element = find_first_by_multiple - end + element = if @selector.size == 1 + find_first_by_one + else + find_first_by_multiple + end # This actually only applies when finding by xpath/css - browser.text_field(:xpath, "//input[@type='radio']") # We don't need to validate the element if we built the xpath ourselves. # It is also used to alter behavior of methods locating more than one type of element # (e.g. text_field locates both input and textarea) @@ -62,20 +62,19 @@ end private def by_id - return unless id = @selector[:id] and id.is_a? String - selector = @selector.dup - selector.delete(:id) + id = selector.delete(:id) + return unless id.is_a? String tag_name = selector.delete(:tag_name) return unless selector.empty? # multiple attributes element = @query_scope.wd.find_element(:id, id) - return if tag_name && !element_validator.validate(element, selector) + return if tag_name && !element_validator.validate(element, {tag_name: tag_name}) element end def find_first_by_one @@ -91,22 +90,30 @@ def find_first_by_multiple selector = selector_builder.normalized_selector idx = selector.delete(:index) + visible = selector.delete(:visible) + how, what = selector_builder.build(selector) if how # could build xpath/css for selector - if idx - @query_scope.wd.find_elements(how, what)[idx] + if idx || !visible.nil? + idx ||= 0 + elements = @query_scope.wd.find_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) end else # can't use xpath, probably a regexp in there - if idx - wd_find_by_regexp_selector(selector, :select)[idx] + if idx || !visible.nil? + idx ||= 0 + elements = wd_find_by_regexp_selector(selector, :select) + elements = elements.select { |el| visible == el.displayed? } unless visible.nil? + elements[idx] unless elements.nil? else wd_find_by_regexp_selector(selector, :find) end end end