rack_test driver should return submission element in document order

This commit is contained in:
Thomas Walpole 2018-10-17 17:43:19 -07:00
parent 71491a6287
commit 3b294d35bb
3 changed files with 19 additions and 13 deletions

View File

@ -19,25 +19,22 @@ class Capybara::RackTest::Form < Capybara::RackTest::Node
end
def params(button)
params = make_params
form_element_types = %i[input select textarea]
form_element_types = %i[input select textarea button]
form_elements_xpath = XPath.generate do |xp|
xpath = xp.descendant(*form_element_types).where(!xp.attr(:form))
xpath += xp.anywhere(*form_element_types).where(xp.attr(:form) == native[:id]) if native[:id]
xpath.where(!xp.attr(:disabled))
end.to_s
native.xpath(form_elements_xpath).map do |field|
form_elements = native.xpath(form_elements_xpath).reject { |el| submitter?(el) && (el != button.native) }
form_elements.each_with_object(make_params) do |field, params|
case field.name
when 'input' then add_input_param(field, params)
when 'input', 'button' then add_input_param(field, params)
when 'select' then add_select_param(field, params)
when 'textarea' then add_textarea_param(field, params)
end
end
merge_param!(params, button[:name], button[:value] || '') if button[:name]
params.to_params_hash
end.to_params_hash
end
def submit(button)
@ -86,8 +83,7 @@ private
merge_param!(params, field['name'], node.value.to_s)
end
elsif %w[submit image].include? field['type']
# TODO: identify the click button here (in document order, rather
# than leaving until the end of the params)
merge_param!(params, field['name'], field['value'].to_s) if field['name']
elsif field['type'] == 'file'
if multipart?
file = if (value = field['value']).to_s.empty?
@ -119,4 +115,8 @@ private
def add_textarea_param(field, params)
merge_param!(params, field['name'], field['_capybara_raw_value'].to_s.gsub(/\n/, "\r\n"))
end
def submitter?(el)
(%w[submit image].include? el['type']) || (el.name == 'button')
end
end

View File

@ -376,6 +376,12 @@ Capybara::SpecHelper.spec '#click_button' do
expect(@results['no_value']).not_to be_nil
end
it 'should send button in document order' do
@session.click_button('outside_button')
@results = extract_results(@session)
expect(@results.keys).to eq %w[for_form2 outside_button which_form post_count]
end
it 'should not send image buttons that were not clicked' do
@session.click_button('Click me!')
@results = extract_results(@session)

View File

@ -470,6 +470,8 @@ New line after and before textarea tag
<input type="submit" name="form[submit_form1]" value="submit_form1" id="submit_form1"/>
</form>
<button type="submit" name="form[outside_button]" value="outside_button" form="form2">Outside!</button>
<form id="form2" action="/form" method="post">
<input type="text" name="form[which_form]" value="form2" id="form_which_form2"/>
<input type="submit" name="form[unused]" value="unused"/>
@ -486,8 +488,6 @@ New line after and before textarea tag
</select>
<input type="submit" name="form[outside_submit]" value="outside_submit" form="form1"/>
<button type="submit" name="form[outside_button]" value="outside_button" form="form2">Outside!</button>
<form id="get-form" action="/form/get?foo=bar" method="get">
<p>