Skip to content

Commit

Permalink
rewrite of Element Matching
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Dec 5, 2018
1 parent be5ffef commit 1e1f7f8
Show file tree
Hide file tree
Showing 31 changed files with 991 additions and 2,289 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ Metrics/MethodLength:

# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 95
Max: 93
Exclude:
- 'lib/watir/capabilities.rb'
- 'lib/watir/locators/element/locator.rb'
- 'lib/watir/locators/element/matcher.rb'
- 'lib/watir/locators/element/selector_builder.rb'
- 'lib/watir/locators/element/selector_builder/xpath.rb'
- 'lib/watir/browser.rb'
Expand Down
17 changes: 11 additions & 6 deletions lib/watir/element_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ def to_a
element = element_class.new(@query_scope, selector)
element.cache = el
if [HTMLElement, Input].include? element.class
tag_name = @selector[:tag_name] || element.tag_name
hash[tag_name] ||= 0
hash[tag_name] += 1
selector[:index] = hash[tag_name] - 1
selector[:tag_name] = tag_name
Watir.element_class_for(tag_name).new(@query_scope, selector)
construct_subtype(element, hash)
else
element
end
Expand Down Expand Up @@ -175,5 +170,15 @@ def locate_all
def element_class
Kernel.const_get(self.class.name.sub(/Collection$/, ''))
end

def construct_subtype(element, hash)
selector = element.selector
tag_name = selector[:tag_name] || element.tag_name
hash[tag_name] ||= 0
hash[tag_name] += 1
selector[:index] = hash[tag_name] - 1
selector[:tag_name] = tag_name
Watir.element_class_for(tag_name).new(@query_scope, selector)
end
end # ElementCollection
end # Watir
24 changes: 10 additions & 14 deletions lib/watir/locators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@
require 'watir/locators/element/selector_builder/xpath_support'
require 'watir/locators/element/selector_builder/regexp_disassembler'
require 'watir/locators/element/selector_builder/xpath'
require 'watir/locators/element/validator'
require 'watir/locators/element/matcher'

require 'watir/locators/anchor/selector_builder'

require 'watir/locators/button/locator'
require 'watir/locators/button/selector_builder'
require 'watir/locators/button/selector_builder/xpath'
require 'watir/locators/button/validator'
require 'watir/locators/button/matcher'

require 'watir/locators/cell/locator'
require 'watir/locators/cell/selector_builder'
require 'watir/locators/cell/selector_builder/xpath'

require 'watir/locators/row/locator'
require 'watir/locators/row/selector_builder'
require 'watir/locators/row/selector_builder/xpath'

require 'watir/locators/text_area/selector_builder'
require 'watir/locators/text_area/selector_builder/xpath'

require 'watir/locators/text_field/locator'
require 'watir/locators/text_field/selector_builder'
require 'watir/locators/text_field/selector_builder/xpath'
require 'watir/locators/text_field/validator'
require 'watir/locators/text_field/matcher'

module Watir
module Locators
Expand All @@ -38,11 +34,11 @@ def locator_class
Locators::Element::Locator
end

def element_validator_class
class_from_string("#{browser.locator_namespace}::#{element_class_name}::Validator") ||
class_from_string("Watir::Locators::#{element_class_name}::Validator") ||
class_from_string("#{browser.locator_namespace}::Element::Validator") ||
Locators::Element::Validator
def element_matcher_class
class_from_string("#{browser.locator_namespace}::#{element_class_name}::Matcher") ||
class_from_string("Watir::Locators::#{element_class_name}::Matcher") ||
class_from_string("#{browser.locator_namespace}::Element::Matcher") ||
Locators::Element::Matcher
end

def selector_builder_class
Expand All @@ -69,8 +65,8 @@ def build_locator
else
selector_builder_class.new(element_class.attribute_list)
end
element_validator = element_validator_class.new
locator_class.new(@query_scope, @selector.dup, selector_builder, element_validator)
element_matcher = element_matcher_class.new(@query_scope, @selector.dup)
locator_class.new(@query_scope, @selector.dup, selector_builder, element_matcher)
end
end
end
Expand Down
32 changes: 0 additions & 32 deletions lib/watir/locators/button/locator.rb

This file was deleted.

40 changes: 40 additions & 0 deletions lib/watir/locators/button/matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Watir
module Locators
class Button
class Matcher < Element::Matcher
def elements_match?(element, values_to_match)
copy_values_to_match = values_to_match.dup
value = copy_values_to_match.delete(:value)

if value
matching = matches_values?(fetch_value(element, :text), value)
deprecate_value_button if matching

matching ||= matches_values?(fetch_value(element, :value), value)

return false unless matching
return true if copy_values_to_match.empty?
end

super(element, copy_values_to_match)
end

def deprecate_value_button
Watir.logger.deprecate(':value locator key for finding button text',
'use :text locator',
ids: [:value_button])
end

def validate_tag(element, _tag_name)
tag_name = element.tag_name.downcase
return unless %w[input button].include?(tag_name)

# TODO: - Verify this is desired behavior based on https://bugzilla.mozilla.org/show_bug.cgi?id=1290963
return if tag_name == 'input' && !Watir::Button::VALID_TYPES.include?(element.attribute('type').downcase)

element
end
end
end
end
end
17 changes: 0 additions & 17 deletions lib/watir/locators/button/validator.rb

This file was deleted.

13 changes: 0 additions & 13 deletions lib/watir/locators/cell/locator.rb

This file was deleted.

Loading

0 comments on commit 1e1f7f8

Please sign in to comment.