lib/watir/locators/element/selector_builder.rb in watir-7.1.0 vs lib/watir/locators/element/selector_builder.rb in watir-7.2.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Watir module Locators class Element class SelectorBuilder include Exception @@ -20,24 +22,34 @@ @valid_attributes = valid_attributes @custom_attributes = [] @query_scope = query_scope end + # rubocop:disable Metrics/PerceivedComplexity: def build(selector) @selector = selector normalize_selector inspected = selector.inspect scope = @query_scope unless @selector.key?(:scope) || @query_scope.is_a?(Watir::Browser) - @built = wd_locators.empty? ? build_wd_selector(@selector) : @selector + @built = + if scope.is_a? Watir::ShadowRoot + raise ArgumentError, ':xpath not supported when locating elements from shadow root' if @selector[:xpath] + + @selector[:css] ||= '*' + @selector + else + wd_locators.empty? ? build_wd_selector(@selector) : @selector + end @built.delete(:index) if @built[:index]&.zero? @built[:scope] = scope if scope Watir.logger.info "Converted #{inspected} to #{@built.inspect}" @built end + # rubocop:enable Metrics/PerceivedComplexity: def wd_locators Watir::Locators::W3C_FINDERS & @selector.keys end @@ -64,13 +76,13 @@ @selector[how] = what end end def merge_scope? - return false unless (Watir::Locators::W3C_FINDERS + [:adjacent] & @selector.keys).empty? + return false unless ((Watir::Locators::W3C_FINDERS + [:adjacent]) & @selector.keys).empty? - return false if [Watir::Browser, Watir::IFrame].any? { |k| @query_scope.is_a?(k) } + return false if [Watir::Browser, Watir::IFrame, Watir::ShadowRoot].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 @@ -134,13 +146,11 @@ def combine_with_xpath_or_css?(selector) keys = selector.keys keys.reject! { |key| %i[visible visible_text index].include? key } if (keys - [:tag_name]).empty? true - elsif selector[:tag_name] == 'input' && keys == %i[tag_name type] - true else - false + selector[:tag_name] == 'input' && keys == %i[tag_name type] end end def raise_unless(what, types) return if types.include?(what.class)