Search for buttons using DOM

This commit is contained in:
Bryan Helmkamp 2008-11-28 18:30:38 -05:00
parent ca0642e35c
commit 0b1dfbe0a2
4 changed files with 16 additions and 30 deletions

View File

@ -50,14 +50,6 @@ module Webrat
end
end
def matches_id?(expected_id)
if expected_id.is_a?(Regexp)
id =~ expected_id
else
id == expected_id.to_s
end
end
def matches_name?(name)
Webrat::XML.attribute(@element, "name") == name.to_s
end
@ -67,10 +59,6 @@ module Webrat
labels.any? { |label| label.matches_text?(label_text) }
end
def matches_alt?(alt)
Webrat::XML.attribute(@element, "alt") =~ /^\W*#{Regexp.escape(alt.to_s)}/i
end
def disabled?
@element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'
end
@ -158,14 +146,6 @@ module Webrat
class ButtonField < Field #:nodoc:
def matches_text?(text)
Webrat::XML.inner_html(@element) =~ /#{Regexp.escape(text.to_s)}/i
end
def matches_value?(value)
Webrat::XML.attribute(@element, "value") =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value)
end
def to_param
return nil if @value.nil?
super

View File

@ -36,13 +36,6 @@ module Webrat
nil
end
def find_button(value = nil)
return fields_by_type([ButtonField]).first if value.nil?
possible_buttons = fields_by_type([ButtonField])
possible_buttons.detect { |possible_button| possible_button.matches_id?(value) } ||
possible_buttons.detect { |possible_button| possible_button.matches_value?(value) }
end
def fields
return @fields if @fields

View File

@ -82,10 +82,19 @@ module Webrat
end
def find_button(value) #:nodoc:
button = forms.detect_mapped do |form|
form.find_button(value)
field_elements = Webrat::XML.css_search(dom, "button", "input[type=submit]", "input[type=image]")
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

View File

@ -37,7 +37,11 @@ describe "field_labeled" do
def match_id(id)
simple_matcher "element with id #{id.inspect}" do |element, matcher|
element.matches_id? id
if id.is_a?(Regexp)
element.id =~ id
else
element.id == id.to_s
end
end
end