submit form with button

master
Kanezoh 2021-06-29 19:14:30 +09:00
parent dc9f937ccc
commit 0c4e5fefcf
3 changed files with 25 additions and 9 deletions

View File

@ -26,6 +26,18 @@ describe "Mechanize Agent test" do
page.not_nil!.body.should eq "success" page.not_nil!.body.should eq "success"
end 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 it "can receive and send cookie" do
agent = Mechanize.new agent = Mechanize.new
# receive cookies # receive cookies

View File

@ -18,7 +18,7 @@ WebMock.stub(:get, "example.com/form").to_return(body:
<form action="post_path" method="post" name="sample_form"> <form action="post_path" method="post" name="sample_form">
<input type="text" name="name"> <input type="text" name="name">
<input type="text" name="email"> <input type="text" name="email">
<input type="submit" value=""> <input type="submit" name="commit" value="submit">
</form> </form>
</body> </body>
</html> </html>
@ -27,3 +27,7 @@ BODY
WebMock.stub(:post, "example.com/post_path"). WebMock.stub(:post, "example.com/post_path").
with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}). with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}).
to_return(body: "success") 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")

View File

@ -129,14 +129,14 @@ class MechanizeCr::Form
# This method adds a button to the query. If the form needs to be # This method adds a button to the query. If the form needs to be
# submitted with multiple buttons, pass each button to this method. # submitted with multiple buttons, pass each button to this method.
private def add_button_to_query(button) def add_button_to_query(button)
unless button.node == @node #unless button.node == @node
message = # message = ""
"#{button.inspect} does not belong to the same page as " \ # "#{button.inspect} does not belong to the same page as " \
"the form #{@name.inspect} in #{@page.uri}" # "the form #{@name.inspect} in #{@page.uri}"
#
raise ArgumentError, message # raise ArgumentError, message
end #end
@clicked_buttons << button @clicked_buttons << button
end end