module Testable::Element::Locator

Private Instance Methods

access_element(element, locators, qualifiers) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/MethodLength

# File lib/testable/locator.rb, line 53
def access_element(element, locators, qualifiers)
  if qualifiers.empty?
    if element == "element".to_sym
      @browser.element(locators).to_subtype
    else
      region_element.__send__(element, locators)
    end
  else
    # If the qualifiers are not empty, then that means the framework
    # has to consider a given set of elements so that it can check
    # the qualifier against them.
    plural = Testable.plural?(element)
    element = Testable.pluralize(element) unless plural

    elements = region_element.__send__(element, locators)

    # Consider the following element definition:
    #
    # select_list :car_make, name: 'car', selected: 'Audi', enabled: true
    #
    # In this case, the qualifiers will be `selected` (with a value of)
    # "Audi") and `enabled` (with a value of true. The arity of the
    # `selected` method would be 1 while the arity of the `enabled`
    # method would be 0.
    qualifiers.each do |qualifier, value|
      elements.to_a.select! do |ele|
        if ele.public_method(:"#{qualifier}?").arity.zero?
          ele.__send__(:"#{qualifier}?") == value
        else
          ele.__send__(:"#{qualifier}?", value)
        end
      end
    end

    # If the locator passed in was plural, then any elements matching
    # the locator and qualifier have to be returned. Otherwise, it will
    # just be the first item of the elements found.
    plural ? elements : elements.first
  end
rescue Watir::Exception::UnknownObjectException
  return false if caller_locations.any? do |str|
    str.to_s.match?("ready_validations_pass?")
  end

  raise
end