1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Prevent fields from other forms being submitted

This commit is contained in:
Jonas Nicklas 2009-11-09 23:38:19 +01:00
parent f6339daff6
commit 3c0a052d91
3 changed files with 28 additions and 11 deletions

View file

@ -42,25 +42,25 @@ class Webcat::Driver::RackTest
class Form < Node
def params(button)
params = []
params << node.xpath("//input[@type='text']").inject([]) do |agg, input|
params << node.xpath(".//input[@type='text']").inject([]) do |agg, input|
agg << param(input['name'].to_s, input['value'].to_s)
agg
end
params << node.xpath("//textarea").inject([]) do |agg, textarea|
params << node.xpath(".//textarea").inject([]) do |agg, textarea|
agg << param(textarea['name'].to_s, textarea.text.to_s)
agg
end
params << node.xpath("//input[@type='radio']").inject([]) do |agg, input|
params << node.xpath(".//input[@type='radio']").inject([]) 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([]) do |agg, input|
params << node.xpath(".//input[@type='checkbox']").inject([]) do |agg, input|
agg << param(input['name'].to_s, input['value'].to_s) if input['checked']
agg
end
params << node.xpath("//select").inject([]) do |agg, select|
option = select.xpath("option[@selected]").first
option ||= select.xpath('option').first
params << node.xpath(".//select").inject([]) 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

View file

@ -63,7 +63,11 @@ shared_examples_for "session" do
end
it "should serialize and submit text fields" do
@results['foo'].should == 'blah'
@results['first_name'].should == 'John'
end
it "should not serialize fields from other forms" do
@results['middle_name'].should be_nil
end
it "should submit the button that was clicked, but not other buttons" do
@ -113,7 +117,7 @@ shared_examples_for "session" do
it "should submit the associated form" do
@session.click_button('awe123')
results = YAML.load(@session.body)
results['foo'].should == 'blah'
results['first_name'].should == 'John'
end
end
end

View file

@ -2,11 +2,13 @@
<form action="/form" method="post">
<p>
<input type="text" name="form[foo]" value="blah" id="foo"/>
<label for="form_first_name">First Name</label>
<input type="text" name="form[first_name]" value="John" id="form_first_name"/>
</p>
<p>
<input type="text" name="form[bar]" value="fish" id="bar"/>
<label for="form_last_name">Last Name</label>
<input type="text" name="form[last_name]" value="Smith" id="form_last_name"/>
</p>
<p>
@ -70,3 +72,14 @@
<input type="submit" name="form[crappy]" id="crap321" value="crappy"/>
</p>
</form>
<form action="/blah" method="get"/>
<p>
<label for="form_middle_name">Last Name</label>
<input type="text" name="form[middle_name]" value="Darren" id="form_middle_name"/>
</p>
<p>
<input type="submit" name="form[mediocre]" id="mediocre" value="med"/>
<p>
</form>