From dc9f937ccc32a4d0ea17b506a3b36a5f0ed40128 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 29 Jun 2021 17:46:52 +0900 Subject: [PATCH] add clicked buttons --- src/mechanize.cr | 3 ++- src/mechanize/form.cr | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/mechanize.cr b/src/mechanize.cr index 1d127e9..d96e658 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -68,7 +68,8 @@ class Mechanize @agent.history.pop end - def submit(form) + def submit(form, button=nil) + form.add_button_to_query(button) if button case form.method.upcase when "POST" post_form(form.action, form, request_headers) diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 35adb91..ca3bb52 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -29,7 +29,7 @@ class MechanizeCr::Form @action = node.fetch("action", "") @method = node.fetch("method", "GET").upcase @name = node.fetch("name", "") - #@clicked_buttons = [] + @clicked_buttons = Array(FormContent::Button).new #@page = page #@mech = mech @@ -115,6 +115,10 @@ class MechanizeCr::Form end end + @clicked_buttons.each do |b| + successful_controls << b + end + successful_controls.each do |ctrl| value = ctrl.query_value next if value[0] == "" @@ -122,4 +126,18 @@ class MechanizeCr::Form end query end + + # This method adds a button to the query. If the form needs to be + # submitted with multiple buttons, pass each button to this method. + private def add_button_to_query(button) + unless button.node == @node + message = + "#{button.inspect} does not belong to the same page as " \ + "the form #{@name.inspect} in #{@page.uri}" + + raise ArgumentError, message + end + + @clicked_buttons << button + end end