class Watir::Locators::Element::SelectorBuilder

Constants

VALID_WHATS
WILDCARD_ATTRIBUTE

Public Class Methods

new(parent, selector, valid_attributes) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 11
def initialize(parent, selector, valid_attributes)
  @parent = parent # either element or browser
  @selector = selector
  @valid_attributes = valid_attributes
end

Public Instance Methods

build(selector) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 47
def build(selector)
  given_xpath_or_css(selector) || build_wd_selector(selector)
end
check_type(how, what) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 30
def check_type(how, what)
  case how
  when :index
    unless what.is_a?(Fixnum)
      raise TypeError, "expected Fixnum, got #{what.inspect}:#{what.class}"
    end
  else
    unless VALID_WHATS.any? { |t| what.is_a? t }
      raise TypeError, "expected one of #{VALID_WHATS.inspect}, got #{what.inspect}:#{what.class}"
    end
  end
end
normalized_selector() click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 17
def normalized_selector
  selector = {}

  @selector.each do |how, what|
    check_type(how, what)

    how, what = normalize_selector(how, what)
    selector[how] = what
  end

  selector
end
should_use_label_element?() click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 43
def should_use_label_element?
  !valid_attribute?(:label)
end
xpath_builder() click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 51
def xpath_builder
  @xpath_builder ||= xpath_builder_class.new(should_use_label_element?)
end

Private Instance Methods

assert_valid_as_attribute(attribute) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 73
def assert_valid_as_attribute(attribute)
  return if valid_attribute?(attribute) || attribute.to_s =~ WILDCARD_ATTRIBUTE
  raise Exception::MissingWayOfFindingObjectException, "invalid attribute: #{attribute.inspect}"
end
build_css(selectors) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 125
def build_css(selectors)
  css_builder.build(selectors)
end
build_wd_selector(selectors) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 100
def build_wd_selector(selectors)
  unless selectors.values.any? { |e| e.is_a? Regexp }
    build_css(selectors) || build_xpath(selectors)
  end
end
build_xpath(selectors) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 121
def build_xpath(selectors)
  xpath_builder.build(selectors)
end
can_be_combined_with_xpath_or_css?(selector) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 110
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

  false
end
css_builder() click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 135
def css_builder
  @css_builder ||= css_builder_class.new
end
css_builder_class() click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 139
def css_builder_class
  Kernel.const_get("#{self.class.name}::CSS")
rescue
  CSS
end
given_xpath_or_css(selector) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 78
def given_xpath_or_css(selector)
  xpath = selector.delete(:xpath)
  css   = selector.delete(:css)
  return unless xpath || css

  if xpath && css
    raise ArgumentError, ":xpath and :css cannot be combined (#{selector.inspect})"
  end

  how, what = if xpath
                [:xpath, xpath]
              elsif css
                [:css, css]
              end

  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]
end
normalize_selector(how, what) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 57
def normalize_selector(how, what)
  case how
  when :tag_name, :text, :xpath, :index, :class, :label, :css
    # include :class since the valid attribute is 'class_name'
    # include :for since the valid attribute is 'html_for'
    [how, what]
  when :class_name
    [:class, what]
  when :caption
    [:text, what]
  else
    assert_valid_as_attribute how
    [how, what]
  end
end
valid_attribute?(attribute) click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 106
def valid_attribute?(attribute)
  @valid_attributes && @valid_attributes.include?(attribute)
end
xpath_builder_class() click to toggle source
# File lib/watir-webdriver/locators/element/selector_builder.rb, line 129
def xpath_builder_class
  Kernel.const_get("#{self.class.name}::XPath")
rescue
  XPath
end