2021-06-11 04:18:25 +02:00
|
|
|
require "./spec_helper"
|
2021-06-16 16:20:03 +02:00
|
|
|
|
|
|
|
WebMock.stub(:get, "example.com/check_form").to_return(body:
|
2021-06-11 04:18:25 +02:00
|
|
|
<<-BODY
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>page_title</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<form action="post_path" method="post" name="sample_form">
|
|
|
|
<input type="text" name="name">
|
|
|
|
<input type="text" name="email">
|
2021-06-16 16:20:03 +02:00
|
|
|
<input type="checkbox" id="remember_me" name="remember_me" checked>
|
2021-06-17 05:04:45 +02:00
|
|
|
<input type="radio" id="contactChoice1" name="contact" value="email">
|
|
|
|
<input type="radio" id="contactChoice2" name="contact" value="phone">
|
|
|
|
<input type="radio" id="contactChoice3" name="contact" value="mail">
|
2021-06-11 04:18:25 +02:00
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
BODY
|
|
|
|
)
|
|
|
|
|
|
|
|
describe "Mechanize Form test" do
|
|
|
|
agent = Mechanize.new
|
2021-06-16 16:20:03 +02:00
|
|
|
uri = "http://example.com/check_form"
|
2021-06-11 04:18:25 +02:00
|
|
|
page = agent.get(uri)
|
|
|
|
form = page.forms.first
|
2021-06-16 07:58:49 +02:00
|
|
|
|
2021-06-11 04:18:25 +02:00
|
|
|
it "retrun form attribute" do
|
|
|
|
form.action.should eq "post_path"
|
|
|
|
form.method.should eq "POST"
|
|
|
|
form.enctype.should eq "application/x-www-form-urlencoded"
|
|
|
|
form.name.should eq "sample_form"
|
|
|
|
end
|
|
|
|
|
2021-06-16 07:58:49 +02:00
|
|
|
it "includes fields" do
|
|
|
|
form.fields.size.should eq 2
|
|
|
|
end
|
|
|
|
|
2021-06-11 04:18:25 +02:00
|
|
|
context "Form Fields" do
|
2021-06-16 07:58:49 +02:00
|
|
|
it "returns field attribute" do
|
2021-06-11 04:18:25 +02:00
|
|
|
field = form.fields.first
|
|
|
|
field.type.should eq "text"
|
|
|
|
field.name.should eq "name"
|
|
|
|
end
|
|
|
|
end
|
2021-06-16 07:58:49 +02:00
|
|
|
|
|
|
|
context "Form Fields CheckBox" do
|
|
|
|
checkbox = form.checkboxes.first
|
2021-06-16 08:23:48 +02:00
|
|
|
it "returns checkbox status" do
|
|
|
|
checkbox.checked?.should eq true
|
|
|
|
end
|
|
|
|
it "can change check status" do
|
|
|
|
checkbox.checked?.should eq true
|
|
|
|
checkbox.uncheck
|
|
|
|
checkbox.checked?.should eq false
|
|
|
|
checkbox.check
|
|
|
|
checkbox.checked?.should eq true
|
|
|
|
# #click reverses check status
|
|
|
|
checkbox.click
|
|
|
|
checkbox.checked?.should eq false
|
|
|
|
checkbox.click
|
|
|
|
checkbox.checked?.should eq true
|
|
|
|
end
|
2021-06-17 07:10:53 +02:00
|
|
|
it "doesn't included in request data if checkbox isn't checked" do
|
2021-06-16 16:20:03 +02:00
|
|
|
form.request_data.should contain("remember_me=on")
|
|
|
|
checkbox.uncheck
|
|
|
|
form.request_data.should_not contain("remember_me=")
|
2021-06-16 08:23:48 +02:00
|
|
|
end
|
2021-06-16 07:58:49 +02:00
|
|
|
end
|
2021-06-17 05:04:45 +02:00
|
|
|
|
|
|
|
context "Form Fields RadioButton" do
|
|
|
|
radiobuttons = form.radiobuttons
|
|
|
|
radiobuttons.size.should eq 3
|
|
|
|
it "returns radiobutton check status" do
|
|
|
|
radiobuttons.map(&.checked?).should eq [false,false,false]
|
|
|
|
end
|
|
|
|
it "can change check status" do
|
|
|
|
radiobutton = radiobuttons.first
|
|
|
|
radiobutton.checked?.should eq false
|
|
|
|
radiobutton.check
|
|
|
|
radiobutton.checked?.should eq true
|
|
|
|
radiobutton.uncheck
|
|
|
|
radiobutton.checked?.should eq false
|
|
|
|
# #click reverses check status
|
|
|
|
radiobutton.click
|
|
|
|
radiobutton.checked?.should eq true
|
|
|
|
radiobutton.click
|
|
|
|
radiobutton.checked?.should eq false
|
|
|
|
end
|
|
|
|
it "check status is exclusive" do
|
|
|
|
radiobuttons[0].check
|
|
|
|
radiobuttons[0].checked.should eq true
|
|
|
|
radiobuttons[1].checked.should eq false
|
|
|
|
radiobuttons[1].check
|
|
|
|
radiobuttons[1].checked.should eq true
|
|
|
|
radiobuttons[0].checked.should eq false
|
|
|
|
end
|
2021-06-17 07:10:53 +02:00
|
|
|
it "doesn't included in request data if checkbox isn't checked" do
|
|
|
|
radiobuttons[0].check
|
|
|
|
form.request_data.should contain "contact=email"
|
|
|
|
radiobuttons[0].uncheck
|
|
|
|
form.request_data.should_not contain "contact"
|
|
|
|
end
|
2021-06-17 05:04:45 +02:00
|
|
|
end
|
2021-06-11 04:18:25 +02:00
|
|
|
end
|