Skip to content

Commit

Permalink
rack_test driver should return submission element in document order
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Oct 18, 2018
1 parent 71491a6 commit 3b294d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
22 changes: 11 additions & 11 deletions lib/capybara/rack_test/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@ def read; ''; end
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)
Expand Down Expand Up @@ -86,8 +83,7 @@ def add_input_param(field, params)
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?
Expand Down Expand Up @@ -119,4 +115,8 @@ def add_select_param(field, params)
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
6 changes: 6 additions & 0 deletions lib/capybara/spec/session/click_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@
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)
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/spec/views/form.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand All @@ -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>
Expand Down

0 comments on commit 3b294d3

Please sign in to comment.