Skip to content

Commit

Permalink
Search for buttons using DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 28, 2008
1 parent ca0642e commit 0b1dfbe
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
20 changes: 0 additions & 20 deletions lib/webrat/core/field.rb
Expand Up @@ -50,14 +50,6 @@ def path
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
Expand All @@ -66,10 +58,6 @@ def matches_label?(label_text)
return false if labels.empty?
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'
Expand Down Expand Up @@ -158,14 +146,6 @@ def replace_param_value(params, oval, nval)

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
Expand Down
7 changes: 0 additions & 7 deletions lib/webrat/core/form.rb
Expand Up @@ -36,13 +36,6 @@ def find_select_option(option_text)
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

Expand Down
13 changes: 11 additions & 2 deletions lib/webrat/core/locators.rb
Expand Up @@ -82,10 +82,19 @@ def find_select_option(option_text, id_or_name_or_label) #:nodoc:
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
Expand Down
6 changes: 5 additions & 1 deletion spec/api/locators/field_labeled_spec.rb
Expand Up @@ -37,7 +37,11 @@ def should_raise_error_matching regexp, opts

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

Expand Down

0 comments on commit 0b1dfbe

Please sign in to comment.