lib/testable/locator.rb in testable-0.10.0 vs lib/testable/locator.rb in testable-1.0.0

- old
+ new

@@ -43,21 +43,63 @@ # # A check is made if an UnknownObjectException occurs due to a ready # validation. That's necessary because a ready validation has to find # an element in order to determine the ready state, but that element # might not be present. - def access_element(element, locators, _qualifiers) - if element == "element".to_sym - @browser.element(locators).to_subtype + + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/PerceivedComplexity + # rubocop:disable Metrics/MethodLength + 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 - @browser.__send__(element, locators) + # 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 + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/PerceivedComplexity + # rubocop:enable Metrics/MethodLength end end end