lib/watir/locators/element/selector_builder.rb in watir-6.13.0 vs lib/watir/locators/element/selector_builder.rb in watir-6.14.0
- old
+ new
@@ -28,41 +28,34 @@
end
def check_type(how, what)
case how
when :index
- unless what.is_a?(Integer)
- raise TypeError, "expected Integer, got #{what.inspect}:#{what.class}"
- end
+ raise_unless_int(what)
when :visible
- unless what.is_a?(TrueClass) || what.is_a?(FalseClass)
- raise TypeError, "expected TrueClass or FalseClass, got #{what.inspect}:#{what.class}"
- end
+ raise_unless_boolean(what)
when :visible_text
- unless what.is_a?(String) || what.is_a?(Regexp)
- raise TypeError, "expected String or Regexp, got #{what.inspect}:#{what.class}"
- end
+ raise_unless_str_regex(what)
else
- if what.is_a?(Array) && how != :class && how != :class_name
- raise TypeError, "Only :class locator can have a value of an Array"
+ if what.is_a?(Array) && !%i[class class_name].include?(how)
+ raise TypeError, 'Only :class locator can have a value of an Array'
end
- if what.is_a?(Symbol) && how != :adjacent
- raise TypeError, "Symbol is not a valid value"
- end
- unless VALID_WHATS.any? { |t| what.is_a? t }
- raise TypeError, "expected one of #{VALID_WHATS.inspect}, got #{what.inspect}:#{what.class}"
- end
+ raise TypeError, 'Symbol is not a valid value' if what.is_a?(Symbol) && how != :adjacent
+ return if VALID_WHATS.any? { |t| what.is_a? t }
+
+ raise TypeError, "expected one of #{VALID_WHATS.inspect}, got #{what.inspect}:#{what.class}"
end
end
def should_use_label_element?
!valid_attribute?(:label)
end
def build(selector)
inspect = selector.inspect
return given_xpath_or_css(selector) if selector.key?(:xpath) || selector.key?(:css)
+
built = build_wd_selector(selector)
Watir.logger.debug "Converted #{inspect} to #{built}"
built
end
@@ -88,64 +81,73 @@
end
end
def check_custom_attribute(attribute)
return if valid_attribute?(attribute) || attribute.to_s =~ WILDCARD_ATTRIBUTE
+
@custom_attributes << attribute.to_s
end
def given_xpath_or_css(selector)
- xpath = selector.delete(:xpath)
- css = selector.delete(:css)
- return unless xpath || css
+ locator = {}
+ locator[:xpath] = selector.delete(:xpath) if selector.key?(:xpath)
+ locator[:css] = selector.delete(:css) if selector.key?(:css)
- if xpath && css
- raise ArgumentError, ":xpath and :css cannot be combined (#{selector.inspect})"
- end
+ return if locator.empty?
+ raise ArgumentError, ":xpath and :css cannot be combined (#{selector.inspect})" if locator.size > 1
- how, what = if xpath
- [:xpath, xpath]
- elsif css
- [:css, css]
- end
+ return locator.first unless selector.any? && !can_be_combined_with_xpath_or_css?(selector)
- if selector.any? && !can_be_combined_with_xpath_or_css?(selector)
- raise ArgumentError, "#{how} cannot be combined with other selectors (#{selector.inspect})"
- end
-
- [how, what]
+ msg = "#{locator.keys.first} cannot be combined with other selectors (#{selector.inspect})"
+ raise ArgumentError, msg
end
def build_wd_selector(selectors)
return if selectors.values.any? { |e| e.is_a? Regexp }
+
build_xpath(selectors)
end
def valid_attribute?(attribute)
- @valid_attributes && @valid_attributes.include?(attribute)
+ @valid_attributes&.include?(attribute)
end
def can_be_combined_with_xpath_or_css?(selector)
keys = selector.keys
return true if keys == [:tag_name]
- if selector[:tag_name] == "input"
- return keys.sort == [:tag_name, :type]
- end
+ return keys.sort == %i[tag_name type] if selector[:tag_name] == 'input'
false
end
def build_xpath(selectors)
xpath_builder.build(selectors)
end
def xpath_builder_class
Kernel.const_get("#{self.class.name}::XPath")
- rescue
+ rescue StandardError
XPath
end
+ def raise_unless_int(what)
+ return if what.is_a?(Integer)
+
+ raise TypeError, "expected Integer, got #{what.inspect}:#{what.class}"
+ end
+
+ def raise_unless_boolean(what)
+ return if what.is_a?(TrueClass) || what.is_a?(FalseClass)
+
+ raise TypeError, "expected TrueClass or FalseClass, got #{what.inspect}:#{what.class}"
+ end
+
+ def raise_unless_str_regex(what)
+ return if what.is_a?(String) || what.is_a?(Regexp)
+
+ raise TypeError, "expected String or Regexp, got #{what.inspect}:#{what.class}"
+ end
end
end
end
end