emulate browser behavior in the rack driver by sending the button that was clicked even if it doesn't have a value

This commit is contained in:
Darrin Holst 2010-02-24 12:37:31 -06:00
parent 08e5ae0c77
commit 7ae8f8db58
3 changed files with 22 additions and 15 deletions

View File

@ -106,7 +106,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
class Form < Node
def params(button)
params = {}
text_fields = %w[text hidden password url color tel email search].map{|f| "@type='#{f}'"}.join(' or ')
node.xpath(".//input[#{text_fields}]").map do |input|
@ -141,7 +141,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
end
end
end
merge_param!(params, button[:name], button[:value]) if button[:name]
merge_param!(params, button[:name], button[:value] || "") if button[:name]
params
end
@ -154,7 +154,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
end
private
def method
self[:method] =~ /post/i ? :post : :get
end
@ -200,7 +200,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
end
def submit(method, path, attributes)
path = current_path if not path or path.empty?
path = current_path if not path or path.empty?
send(method, path, attributes, env)
follow_redirects!
cache_body

View File

@ -13,33 +13,33 @@ shared_examples_for "click_button" do
end
context "with value given on a submit button" do
context "on a form with HTML5 fields" do
context "on a form with HTML5 fields" do
before do
@session.click_button('html5_submit')
@results = extract_results(@session)
end
it "should serialise and submit search fields" do
@results['html5_search'].should == 'what are you looking for'
end
it "should serialise and submit email fields" do
@results['html5_email'].should == 'person@email.com'
end
it "should serialise and submit url fields" do
@results['html5_url'].should == 'http://www.example.com'
end
it "should serialise and submit tel fields" do
@results['html5_tel'].should == '911'
end
it "should serialise and submit color fields" do
@results['html5_color'].should == '#FFF'
end
end
end
context "on an HTML4 form" do
before do
@session.click_button('awesome')
@ -175,7 +175,7 @@ shared_examples_for "click_button" do
@session.click_button('ck_me')
extract_results(@session)['first_name'].should == 'John'
end
it "should prefer exact matches over partial matches" do
@session.click_button('Just a button')
extract_results(@session)['button'].should == 'Just a button'
@ -190,6 +190,12 @@ shared_examples_for "click_button" do
end
end
it "should serialize and send valueless buttons that were clicked" do
@session.click_button('No Value!')
@results = extract_results(@session)
@results['no_value'].should_not be_nil
end
it "should serialize and send GET forms" do
@session.visit('/form')
@session.click_button('med')
@ -202,13 +208,13 @@ shared_examples_for "click_button" do
@session.click_button('Go FAR')
@session.body.should include('You landed')
end
it "should post pack to the same URL when no action given" do
@session.visit('/postback')
@session.click_button('With no action')
@session.body.should include('Postback')
end
it "should post pack to the same URL when blank action given" do
@session.visit('/postback')
@session.click_button('With blank action')

View File

@ -153,6 +153,7 @@
<input type="submit" name="form[crappy]" id="crap321" value="crappy"/>
<input type="image" name="form[okay]" id="okay556" value="okay"/>
<button type="submit" id="click_me_123" value="click_me">Click me!</button>
<button type="submit" name="form[no_value]">No Value!</button>
</p>
</form>