submit form with button

This commit is contained in:
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"
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

View File

@ -18,7 +18,7 @@ WebMock.stub(:get, "example.com/form").to_return(body:
<form action="post_path" method="post" name="sample_form">
<input type="text" name="name">
<input type="text" name="email">
<input type="submit" value="">
<input type="submit" name="commit" value="submit">
</form>
</body>
</html>
@ -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")

View File

@ -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