Search for buttons using DOM
This commit is contained in:
parent
ca0642e35c
commit
0b1dfbe0a2
@ -50,14 +50,6 @@ module Webrat
|
|||||||
end
|
end
|
||||||
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)
|
def matches_name?(name)
|
||||||
Webrat::XML.attribute(@element, "name") == name.to_s
|
Webrat::XML.attribute(@element, "name") == name.to_s
|
||||||
end
|
end
|
||||||
@ -67,10 +59,6 @@ module Webrat
|
|||||||
labels.any? { |label| label.matches_text?(label_text) }
|
labels.any? { |label| label.matches_text?(label_text) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_alt?(alt)
|
|
||||||
Webrat::XML.attribute(@element, "alt") =~ /^\W*#{Regexp.escape(alt.to_s)}/i
|
|
||||||
end
|
|
||||||
|
|
||||||
def disabled?
|
def disabled?
|
||||||
@element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'
|
@element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'
|
||||||
end
|
end
|
||||||
@ -158,14 +146,6 @@ module Webrat
|
|||||||
|
|
||||||
class ButtonField < Field #:nodoc:
|
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
|
def to_param
|
||||||
return nil if @value.nil?
|
return nil if @value.nil?
|
||||||
super
|
super
|
||||||
|
@ -36,13 +36,6 @@ module Webrat
|
|||||||
nil
|
nil
|
||||||
end
|
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
|
def fields
|
||||||
return @fields if @fields
|
return @fields if @fields
|
||||||
|
|
||||||
|
@ -82,10 +82,19 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def find_button(value) #:nodoc:
|
def find_button(value) #:nodoc:
|
||||||
button = forms.detect_mapped do |form|
|
field_elements = Webrat::XML.css_search(dom, "button", "input[type=submit]", "input[type=image]")
|
||||||
form.find_button(value)
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
|
button = field_by_element(field_element)
|
||||||
|
|
||||||
if button
|
if button
|
||||||
return button
|
return button
|
||||||
else
|
else
|
||||||
|
@ -37,7 +37,11 @@ describe "field_labeled" do
|
|||||||
|
|
||||||
def match_id(id)
|
def match_id(id)
|
||||||
simple_matcher "element with id #{id.inspect}" do |element, matcher|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user