From 0b1dfbe0a26a75436a3a294a638e0f5fb9781ccf Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 18:30:38 -0500 Subject: [PATCH] Search for buttons using DOM --- lib/webrat/core/field.rb | 20 -------------------- lib/webrat/core/form.rb | 7 ------- lib/webrat/core/locators.rb | 13 +++++++++++-- spec/api/locators/field_labeled_spec.rb | 6 +++++- 4 files changed, 16 insertions(+), 30 deletions(-) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 10502b3..38b879c 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -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 @@ -66,10 +58,6 @@ module Webrat 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' @@ -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 diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index aff5cd6..c29a69b 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -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 diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 7d635e8..324586f 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -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 diff --git a/spec/api/locators/field_labeled_spec.rb b/spec/api/locators/field_labeled_spec.rb index 82cdba0..7d9a486 100644 --- a/spec/api/locators/field_labeled_spec.rb +++ b/spec/api/locators/field_labeled_spec.rb @@ -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