lib/watir/locators/element/selector_builder.rb in watir-6.16.1 vs lib/watir/locators/element/selector_builder.rb in watir-6.16.2

- old
+ new

@@ -29,29 +29,28 @@ deprecated_locators normalize_selector inspected = selector.inspect scope = @query_scope unless @selector.key?(:scope) || @query_scope.is_a?(Watir::Browser) - @built = wd_locator(@selector.keys).nil? ? build_wd_selector(@selector) : @selector + @built = wd_locators.empty? ? build_wd_selector(@selector) : @selector @built.delete(:index) if @built[:index]&.zero? @built[:scope] = scope if scope Watir.logger.info "Converted #{inspected} to #{@built.inspect}" @built end - def wd_locator(keys) - (Watir::Locators::W3C_FINDERS & keys).first + def wd_locators + Watir::Locators::W3C_FINDERS & @selector.keys end private def normalize_selector - wd_locators = @selector.keys & Watir::Locators::W3C_FINDERS raise LocatorException, "Can not locate element with #{wd_locators}" if wd_locators.size > 1 - @selector[:scope] = @query_scope.selector_builder.built if use_scope? + @selector[:scope] = @query_scope.selector_builder.built if merge_scope? if @selector.key?(:class) || @selector.key?(:class_name) classes = ([@selector[:class]].flatten + [@selector.delete(:class_name)].flatten).compact classes.each do |class_name| @@ -73,17 +72,17 @@ how, what = normalize_locator(key, @selector.delete(key)) @selector[how] = what end end - def use_scope? - return false if @query_scope.is_a?(Browser) + def merge_scope? + return false unless (Watir::Locators::W3C_FINDERS + [:adjacent] & @selector.keys).empty? - !@selector.key?(:adjacent) && - (Watir::Locators::W3C_FINDERS & @selector.keys).empty? && - !(@query_scope.is_a?(Watir::IFrame) || @query_scope.is_a?(Watir::Radio)) && - @query_scope.selector_builder.built&.size == 1 + return false if [Watir::Browser, Watir::IFrame].any? { |k| @query_scope.is_a?(k) } + + scope_invalid_locators = @query_scope.selector_builder.built.keys.reject { |key| key == wd_locator } + scope_invalid_locators.empty? end def deprecate_class_array(class_name) dep = "Using the :class locator to locate multiple classes with a String value (i.e. \"#{class_name}\")" Watir.logger.deprecate dep, @@ -140,12 +139,20 @@ return if valid_attribute?(attribute) || attribute.to_s =~ WILDCARD_ATTRIBUTE @custom_attributes << attribute.to_s end - # Implement this method when creating a different selector builder + # Extensions implement this method when creating a different selector builder + def implementation_class + Kernel.const_get("#{self.class.name}::XPath") + end + def build_wd_selector(selector) - Kernel.const_get("#{self.class.name}::XPath").new.build(selector) + implementation_class.new.build(selector) + end + + def wd_locator + implementation_class::LOCATOR end def valid_attribute?(attribute) @valid_attributes&.include?(attribute) end