Ensure input field document order is retained when generating params in the RackTest driver

This commit is contained in:
Jonas Nicklas 2011-01-20 11:36:38 +00:00 committed by Joel Chippindale
parent 296041cf1e
commit 97dde68f76
3 changed files with 64 additions and 41 deletions

View File

@ -123,39 +123,43 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
def params(button)
params = {}
native.xpath(".//input[not(@disabled) and (not(@type) or (@type!='radio' and @type!='file' and @type!='checkbox' and @type!='submit' and @type!='image'))]").map do |input|
merge_param!(params, input['name'].to_s, input['value'].to_s)
end
native.xpath(".//textarea[not(@disabled)]").map do |textarea|
merge_param!(params, textarea['name'].to_s, textarea.text.to_s)
end
native.xpath(".//input[not(@disabled) and (@type='radio' or @type='checkbox')]").map do |input|
merge_param!(params, input['name'].to_s, input['value'].to_s) if input['checked']
end
native.xpath(".//select[not(@disabled)]").map do |select|
if select['multiple'] == 'multiple'
options = select.xpath(".//option[@selected]")
options.each do |option|
merge_param!(params, select['name'].to_s, (option['value'] || option.text).to_s)
end
else
option = select.xpath(".//option[@selected]").first
option ||= select.xpath('.//option').first
merge_param!(params, select['name'].to_s, (option['value'] || option.text).to_s) if option
end
end
native.xpath(".//input[not(@disabled) and @type='file']").map do |input|
if multipart?
file = \
if (value = input['value']).to_s.empty?
NilUploadedFile.new
native.xpath("(.//input|.//select|.//textarea)[not(@disabled)]").map do |field|
case field.name
when 'input'
if %w(radio checkbox).include? field['type']
merge_param!(params, field['name'].to_s, field['value'].to_s) if field['checked']
elsif %w(submit image).include? field['type']
# TO DO identify the click button here (in document order, rather
# than leaving until the end of the params)
elsif field['type'] =='file'
if multipart?
file = \
if (value = field['value']).to_s.empty?
NilUploadedFile.new
else
content_type = MIME::Types.type_for(value).first.to_s
Rack::Test::UploadedFile.new(value, content_type)
end
merge_param!(params, field['name'].to_s, file)
else
content_type = MIME::Types.type_for(value).first.to_s
Rack::Test::UploadedFile.new(value, content_type)
merge_param!(params, field['name'].to_s, File.basename(field['value'].to_s))
end
merge_param!(params, input['name'].to_s, file)
else
merge_param!(params, input['name'].to_s, File.basename(input['value'].to_s))
else
merge_param!(params, field['name'].to_s, field['value'].to_s)
end
when 'select'
if field['multiple'] == 'multiple'
options = field.xpath(".//option[@selected]")
options.each do |option|
merge_param!(params, field['name'].to_s, (option['value'] || option.text).to_s)
end
else
option = field.xpath(".//option[@selected]").first
option ||= field.xpath('.//option').first
merge_param!(params, field['name'].to_s, (option['value'] || option.text).to_s) if option
end
when 'textarea'
merge_param!(params, field['name'].to_s, field.text.to_s)
end
end
merge_param!(params, button[:name], button[:value] || "") if button[:name]

View File

@ -75,18 +75,25 @@ shared_examples_for "session" do
@session.visit('/form')
@session.fill_in('address1_city', :with =>'Paris')
@session.fill_in('address1_street', :with =>'CDG')
@session.fill_in('address1_street', :with =>'CDG')
@session.select("France", :from => 'address1_country')
@session.fill_in('address2_city', :with => 'Mikolaiv')
@session.fill_in('address2_street', :with => 'PGS')
@session.select("Ukraine", :from => 'address2_country')
@session.click_button "awesome"
addresses=extract_results(@session)["addresses"]
addresses.should have(2).addresses
addresses[0]["street"].should == 'CDG'
addresses[0]["city"].should == 'Paris'
addresses[0]["street"].should == 'CDG'
addresses[0]["city"].should == 'Paris'
addresses[0]["country"].should == 'France'
addresses[1]["street"].should == 'PGS'
addresses[1]["city"].should == 'Mikolaiv'
addresses[1]["street"].should == 'PGS'
addresses[1]["city"].should == 'Mikolaiv'
addresses[1]["country"].should == 'Ukraine'
end
end

View File

@ -163,18 +163,30 @@
<span>First address<span>
<label for='address1_street'>Street</label>
<input type="text" name="form[addresses][][street]" value="" id="address1_street">
<label for='address1_city'>City</label>
<input type="text" name="form[addresses][][city]" value="" id="address1_city">
</p>
<p>
<label for='address1_city'>City</label>
<input type="text" name="form[addresses][][city]" value="" id="address1_city">
<label for='address1_country'>Country</label>
<select name="form[addresses][][country]" id="address1_country">
<option>France</option>
<option>Ukraine</option>
</select>
</p>
<p>
<span>Second address<span>
<label for='address2_street'>Street</label>
<input type="text" name="form[addresses][][street]" value="" id="address2_street">
<label for='address2_city'>City</label>
<input type="text" name="form[addresses][][city]" value="" id="address2_city">
<label for='address2_country'>Country</label>
<select name="form[addresses][][country]" id="address2_country">
<option>France</option>
<option>Ukraine</option>
</select>
</p>
<div style="display:none;">