diff --git a/History.txt b/History.txt index 6566b74..db42057 100644 --- a/History.txt +++ b/History.txt @@ -26,6 +26,7 @@ * Support clicking links by title (Patch from Dan Barry) * Added missing spec for clicking image buttons (Patch from Tim Harper) * Switched tests to specs, and from Mocha to RSpec's mocking library + * Add support to click_button for IDs (Patch form Gwyn Morfey) * Bug fixes diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index c9ea46b..4238afb 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -138,6 +138,10 @@ module Webrat @element.innerHTML =~ /#{Regexp.escape(text.to_s)}/i end + # def matches_id?(id) + # @element["id"] =~ /^\W*#{Regexp.escape(id.to_s)}/i + # end + def matches_value?(value) @element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value) end diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 4d8a47b..3d739ab 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -30,7 +30,8 @@ module Webrat def find_button(value = nil) return fields_by_type([ButtonField]).first if value.nil? - possible_buttons = fields_by_type([ButtonField]) + 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 diff --git a/spec/api/clicks_button_spec.rb b/spec/api/clicks_button_spec.rb index c732526..48b51d2 100644 --- a/spec/api/clicks_button_spec.rb +++ b/spec/api/clicks_button_spec.rb @@ -344,6 +344,16 @@ describe "clicks_button" do @session.clicks_button end + it "should find buttons by their IDs" do + @session.response_body = <<-EOS +
+ EOS + @session.should_receive(:get) + @session.clicks_button "my_button" + end + it "should find image buttons by their alt text" do @session.response_body = <<-EOS