Add support for <button> tag in Session#click_button

This commit is contained in:
Andrea Fazzi 2009-11-28 18:22:48 +01:00
parent ea6857b52b
commit 510f33e86d
3 changed files with 39 additions and 2 deletions

View File

@ -142,8 +142,9 @@ module Capybara
end
def find_button(locator)
button = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']").first || find("//button[@id='#{locator}' or @value='#{locator}' or contains(.,'#{locator}')]").first
raise Capybara::ElementNotFound, "no button with value or id '#{locator}' found" unless button
button = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']").first \
|| find("//button[@id='#{locator}' or @value='#{locator}' or contains(.,'#{locator}')]").first
raise Capybara::ElementNotFound, "no button with value or id or text '#{locator}' found" unless button
button
end

View File

@ -145,6 +145,38 @@ shared_examples_for "session" do
end
end
context "when the button is defined by a <button> tag" do
before do
@session.visit('/buttons')
end
context "with text given" do
it "should locate the button" do
running do
@session.click_button('Click me')
end.should_not raise_error(Capybara::ElementNotFound)
end
end
context "with id given" do
it "should locate the button" do
running do
@session.click_button('click_me_123')
end.should_not raise_error(Capybara::ElementNotFound)
end
end
context "with value given" do
it "should locate the button" do
running do
@session.click_button('click_me')
end.should_not raise_error(Capybara::ElementNotFound)
end
end
end
context "with a locator that doesn't exist" do
it "should raise an error" do
running do

4
spec/views/buttons.erb Normal file
View File

@ -0,0 +1,4 @@
<h1>Buttons</h1>
<button>Click me!</button>
<button id="click_me_123">Click me by id!</button>
<button value="click_me">Click me by value!</button>