refactor checkbox

master
Kanezoh 2021-06-18 20:59:10 +09:00
parent 36f9f738bf
commit 7b231b9a6b
2 changed files with 22 additions and 3 deletions

View File

@ -26,7 +26,7 @@ describe "Mechanize Form test" do
page = agent.get(uri) page = agent.get(uri)
form = page.forms.first form = page.forms.first
it "retrun form attribute" do it "returns form attribute" do
form.action.should eq "post_path" form.action.should eq "post_path"
form.method.should eq "POST" form.method.should eq "POST"
form.enctype.should eq "application/x-www-form-urlencoded" form.enctype.should eq "application/x-www-form-urlencoded"
@ -47,9 +47,11 @@ describe "Mechanize Form test" do
context "Form Fields CheckBox" do context "Form Fields CheckBox" do
checkbox = form.checkboxes.first checkbox = form.checkboxes.first
it "returns checkbox status" do it "returns checkbox status" do
checkbox.checked?.should eq true checkbox.checked?.should eq true
end end
it "can change check status" do it "can change check status" do
checkbox.checked?.should eq true checkbox.checked?.should eq true
checkbox.uncheck checkbox.uncheck
@ -62,6 +64,7 @@ describe "Mechanize Form test" do
checkbox.click checkbox.click
checkbox.checked?.should eq true checkbox.checked?.should eq true
end end
it "doesn't included in request data if checkbox isn't checked" do it "doesn't included in request data if checkbox isn't checked" do
form.request_data.should contain("remember_me=on") form.request_data.should contain("remember_me=on")
checkbox.uncheck checkbox.uncheck
@ -72,9 +75,11 @@ describe "Mechanize Form test" do
context "Form Fields RadioButton" do context "Form Fields RadioButton" do
radiobuttons = form.radiobuttons radiobuttons = form.radiobuttons
radiobuttons.size.should eq 3 radiobuttons.size.should eq 3
it "returns radiobutton check status" do it "returns radiobutton check status" do
radiobuttons.map(&.checked?).should eq [false,false,false] radiobuttons.map(&.checked?).should eq [false,false,false]
end end
it "can change check status" do it "can change check status" do
radiobutton = radiobuttons.first radiobutton = radiobuttons.first
radiobutton.checked?.should eq false radiobutton.checked?.should eq false
@ -88,6 +93,7 @@ describe "Mechanize Form test" do
radiobutton.click radiobutton.click
radiobutton.checked?.should eq false radiobutton.checked?.should eq false
end end
it "check status is exclusive" do it "check status is exclusive" do
radiobuttons[0].check radiobuttons[0].check
radiobuttons[0].checked.should eq true radiobuttons[0].checked.should eq true
@ -96,11 +102,25 @@ describe "Mechanize Form test" do
radiobuttons[1].checked.should eq true radiobuttons[1].checked.should eq true
radiobuttons[0].checked.should eq false radiobuttons[0].checked.should eq false
end end
it "doesn't included in request data if checkbox isn't checked" do it "doesn't included in request data if checkbox isn't checked" do
radiobuttons[0].check radiobuttons[0].check
form.request_data.should contain "contact=email" form.request_data.should contain "contact=email"
radiobuttons[0].uncheck radiobuttons[0].uncheck
form.request_data.should_not contain "contact" form.request_data.should_not contain "contact"
end end
it "can be found by radiobutton_with method" do
radiobutton = form.radiobutton_with("contact")
radiobutton.value.should eq "email"
end
it "can be found by radiobuttons_with method" do
radiobuttons = form.radiobuttons_with("contact")
radiobuttons.size.should eq 3
radiobuttons[0].value.should eq "email"
radiobuttons[1].value.should eq "phone"
radiobuttons[2].value.should eq "mail"
end
end end
end end

View File

@ -62,14 +62,13 @@ class MechanizeCr::Form
v === elm.name v === elm.name
end end
end end
return nil if f.empty?
yield f yield f
f f
end end
def {{singular.id}}_with(criteria) def {{singular.id}}_with(criteria)
f = {{plural.id}}_with(criteria) f = {{plural.id}}_with(criteria)
raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.nil? raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.empty?
f.first f.first
end end
{% end %} {% end %}