Skip to content

Commit

Permalink
Add support to click_button for IDs (Patch form Gwyn Morfey)
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Oct 14, 2008
1 parent d7a9447 commit cf55dba
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/webrat/core/field.rb
Expand Up @@ -138,6 +138,10 @@ def matches_text?(text)
@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
Expand Down
3 changes: 2 additions & 1 deletion lib/webrat/core/form.rb
Expand Up @@ -30,7 +30,8 @@ def find_select_option(option_text)

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

Expand Down
10 changes: 10 additions & 0 deletions spec/api/clicks_button_spec.rb
Expand Up @@ -344,6 +344,16 @@
@session.clicks_button
end

it "should find buttons by their IDs" do
@session.response_body = <<-EOS
<form action="/">
<input type="submit" id="my_button" />
</form>
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
<form action="/">
Expand Down

0 comments on commit cf55dba

Please sign in to comment.