Form submission galore

This commit is contained in:
Jonas Nicklas 2009-11-09 23:10:15 +01:00
parent b905699cd7
commit 8f355cdadb
3 changed files with 137 additions and 9 deletions

View File

@ -24,7 +24,7 @@ class Webcat::Driver::RackTest
if tag_name == 'a'
session.visit(self[:href])
elsif tag_name == 'input' and self[:type] == 'submit'
Form.new(session, form).submit
Form.new(session, form).submit(self)
end
end
@ -40,15 +40,42 @@ class Webcat::Driver::RackTest
end
class Form < Node
def params
node.xpath('//input').inject({}) do |agg, node|
agg[node['name'].to_s] = node['value'].to_s
def params(button)
params = []
params = node.xpath("//input[@type='text']").inject(params) do |agg, input|
agg << param(input['name'].to_s, input['value'].to_s)
agg
end
params = node.xpath("//textarea").inject(params) do |agg, textarea|
agg << param(textarea['name'].to_s, textarea.text.to_s)
agg
end
params = node.xpath("//input[@type='radio']").inject(params) do |agg, input|
agg << param(input['name'].to_s, input['value'].to_s) if input['checked']
agg
end
params = node.xpath("//input[@type='checkbox']").inject(params) do |agg, input|
agg << param(input['name'].to_s, input['value'].to_s) if input['checked']
agg
end
params = node.xpath("//select").inject(params) do |agg, select|
option = select.xpath("option[@selected]").first
option ||= select.xpath('option').first
agg << param(select['name'].to_s, (option['value'] || option.text).to_s) if option
agg
end
params << param(button[:name], button.value) if button[:name]
params.join('&')
end
def submit
session.submit(node['action'].to_s, params)
def submit(button)
session.submit(node['action'].to_s, params(button))
end
private
def param(key, value)
"#{key}=#{value}"
end
end

View File

@ -57,10 +57,55 @@ shared_examples_for "session" do
end
context "with value given" do
it "should submit the associated form" do
before do
@session.click_button('awesome')
results = YAML.load(@session.body)
results['foo'].should == 'blah'
@results = YAML.load(@session.body)
end
it "should serialize and submit text fields" do
@results['foo'].should == 'blah'
end
it "should submit the button that was clicked, but not other buttons" do
@results['awesome'].should == 'awesome'
@results['crappy'].should be_nil
end
it "should serialize radio buttons" do
@results['gender'].should == 'female'
end
it "should serialize check boxes" do
@results['pets'].should include('dog', 'hamster')
@results['pets'].should_not include('cat')
end
it "should serialize text areas" do
@results['description'].should == 'Descriptive text goes here'
end
it "should serialize select tag with values" do
@results['locale'].should == 'en'
end
it "should serialize select tag without values" do
@results['region'].should == 'Norway'
end
it "should serialize first option for select tag with no selection" do
@results['city'].should == 'London'
end
it "should not serialize a select tag without options" do
@results['tendency'].should be_nil
end
context "with multipart form" do
it "should attach the file"
end
context "with normal form" do
it "should serialize the file path"
end
end

View File

@ -9,6 +9,62 @@
<input type="text" name="form[bar]" value="fish" id="bar"/>
</p>
<p>
<label for="form_locale">Locale</label>
<select name="form[locale]" id="form_locale">
<option value="sv">Swedish</option>
<option selected="selected" value="en">English</option>
<option value="fi">Finish</option>
<option value="no">Norwegian</option>
</select>
</p>
<p>
<label for="form_region">Region</label>
<select name="form[region]" id="form_region">
<option>Sweden</option>
<option selected="selected">Norway</option>
<option>Finland</option>
</select>
</p>
<p>
<label for="form_city">City</label>
<select name="form[city]" id="form_city">
<option>London</option>
<option>Stockholm</option>
<option>Paris</option>
</select>
</p>
<p>
<label for="form_tendency">Tendency</label>
<select name="form[tendency]" id="form_tendency"></select>
</p>
<p>
<label for="form_description">Description</label></br>
<textarea name="form[description]" id="form_description">Descriptive text goes here</textarea>
<p>
<p>
<input type="radio" name=form[gender] value="male" id="gender_male"/>
<label for="gender_male">Male</label>
<input type="radio" name=form[gender] value="female" id="gender_female" checked="checked"/>
<label for="gender_female">Female</label>
<input type="radio" name=form[gender] value="both" id="gender_both"/>
<label for="gender_both">Both</label>
</p>
<p>
<input type="checkbox" value="dog" name="form[pets][]" id="pets_dog" checked="checked"/>
<label for="pets_dog">Dog</label>
<input type="checkbox" value="cat" name="form[pets][]" id="pets_cat"/>
<label for="pets_cat">Cat</label>
<input type="checkbox" value="hamster" name="form[pets][]" id="pets_hamster" checked="checked"/>
<label for="pets_hamster">Hamster</label>
</p>
<p>
<input type="submit" name="form[awesome]" id="awe123" value="awesome"/>
<input type="submit" name="form[crappy]" id="crap321" value="crappy"/>