Skip to content

Commit

Permalink
Extracting ButtonLocator object
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 29, 2008
1 parent e5fb990 commit 0b6d9c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
20 changes: 3 additions & 17 deletions lib/webrat/core/locators.rb
Expand Up @@ -94,24 +94,10 @@ def find_select_option(option_text, id_or_name_or_label) #:nodoc:
end

def find_button(value) #:nodoc:
field_elements = Webrat::XML.xpath_search(dom, *ButtonField.xpath_search)
require "webrat/core/locators/button_locator"

field_element = field_elements.detect do |field_element|
value.nil? ||
(value.is_a?(Regexp) && Webrat::XML.attribute(field_element, "id") =~ value) ||
(!value.is_a?(Regexp) && Webrat::XML.attribute(field_element, "id") == value.to_s) ||
Webrat::XML.attribute(field_element, "value") =~ /^\W*#{Regexp.escape(value.to_s)}/i ||
Webrat::XML.inner_html(field_element) =~ /#{Regexp.escape(value.to_s)}/i ||
Webrat::XML.attribute(field_element, "alt") =~ /^\W*#{Regexp.escape(value.to_s)}/i
end

button = field_by_element(field_element)

if button
return button
else
raise NotFoundError.new("Could not find button #{value.inspect}")
end
ButtonLocator.new(self, value).locate ||
raise(NotFoundError.new("Could not find button #{value.inspect}"))
end

def find_area(id_or_title) #:nodoc:
Expand Down
43 changes: 43 additions & 0 deletions lib/webrat/core/locators/button_locator.rb
@@ -0,0 +1,43 @@
class ButtonLocator

def initialize(scope, value)
@scope = scope
@value = value
end

def locate
@scope.field_by_element(button_element)
end

def button_element
button_elements.detect do |element|
@value.nil? ||
matches_id?(element) ||
matches_value?(element) ||
matches_html?(element) ||
matches_alt?(element)
end
end

def matches_id?(element)
(@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) ||
(!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s)
end

def matches_value?(element)
Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
end

def matches_html?(element)
Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i
end

def matches_alt?(element)
Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
end

def button_elements
Webrat::XML.xpath_search(@scope.dom, *Webrat::ButtonField.xpath_search)
end

end

0 comments on commit 0b6d9c2

Please sign in to comment.