From 0c4e5fefcf6355cb006a279527c34fa4a7392713 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 29 Jun 2021 19:14:30 +0900 Subject: [PATCH] submit form with button --- spec/agent_spec.cr | 12 ++++++++++++ spec/spec_helper.cr | 6 +++++- src/mechanize/form.cr | 16 ++++++++-------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index 7a966f8..42084b8 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -26,6 +26,18 @@ describe "Mechanize Agent test" do page.not_nil!.body.should eq "success" end + it "can fill and submit form with submit button" do + agent = Mechanize.new + page = agent.get("http://example.com/form") + form = page.forms[0] + form.field_with("name").value = "foo" + form.field_with("email").value = "bar" + submit_button = form.buttons[0] + page = agent.submit(form, submit_button) + page.not_nil!.code.should eq 200 + page.not_nil!.body.should eq "success with button" + end + it "can receive and send cookie" do agent = Mechanize.new # receive cookies diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index a0478f2..c6284af 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -18,7 +18,7 @@ WebMock.stub(:get, "example.com/form").to_return(body:
- +
@@ -27,3 +27,7 @@ BODY WebMock.stub(:post, "example.com/post_path"). with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}). to_return(body: "success") + +WebMock.stub(:post, "example.com/post_path"). + with(body: "name=foo&email=bar&commit=submit", headers: {"Content-Type" => "application/x-www-form-urlencoded"}). + to_return(body: "success with button") diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index ca3bb52..7bfe6e1 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -129,14 +129,14 @@ class MechanizeCr::Form # 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 + 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