add checkbox_with method

master
Kanezoh 2021-06-28 16:51:04 +09:00
parent 8fa89fab2d
commit b4c5e33037
3 changed files with 20 additions and 4 deletions

View File

@ -8,7 +8,8 @@ WebMock.stub(:get, "example.com/form/check_box").to_return(body:
</head> </head>
<body> <body>
<form action="post_path" method="post" name="sample_form"> <form action="post_path" method="post" name="sample_form">
<input type="checkbox" id="remember_me" name="remember_me" checked> <input type="checkbox" class="some_checkbox" id="remember_me" name="remember_me" checked>
<input type="checkbox" class="some_checkbox" id="forget_me" name="forget_me">
</form> </form>
</body> </body>
</html> </html>
@ -19,13 +20,14 @@ describe "Form Fields CheckBox" do
agent = Mechanize.new agent = Mechanize.new
page = agent.get("http://example.com/form/check_box") page = agent.get("http://example.com/form/check_box")
form = page.forms[0] form = page.forms[0]
checkbox = form.checkboxes.first
it "returns checkbox status" do it "returns checkbox status" do
checkbox = form.checkboxes.first
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 = form.checkboxes.first
checkbox.checked?.should eq true checkbox.checked?.should eq true
checkbox.uncheck checkbox.uncheck
checkbox.checked?.should eq false checkbox.checked?.should eq false
@ -39,8 +41,21 @@ describe "Form Fields CheckBox" do
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
checkbox = form.checkboxes.first
form.request_data.should contain("remember_me=on") form.request_data.should contain("remember_me=on")
checkbox.uncheck checkbox.uncheck
form.request_data.should_not contain("remember_me=") form.request_data.should_not contain("remember_me=")
end end
it "can be found by checkbox_with method" do
checkbox = form.checkbox_with("remember_me")
checkbox.name.should eq "remember_me"
end
it "can be found by checkboxes_with method" do
checkboxes = form.checkboxes_with({"class" => "some_checkbox"})
checkboxes.size.should eq 2
checkboxes[0].name.should eq "remember_me"
checkboxes[1].name.should eq "forget_me"
end
end end

View File

@ -49,6 +49,7 @@ class MechanizeCr::Form
elements_with "field" elements_with "field"
elements_with "radiobutton" elements_with "radiobutton"
elements_with "checkbox", "checkboxes"
private def parse private def parse
@node.css("input").not_nil!.each do |html_node| @node.css("input").not_nil!.each do |html_node|

View File

@ -1,6 +1,6 @@
module MechanzeCr::ElementMatcher module MechanzeCr::ElementMatcher
macro elements_with(singular) macro elements_with(singular, plural="")
{% plural = "#{singular.id}s" %} {% plural = "#{singular.id}s" if plural.empty? %}
def {{plural.id}}_with(criteria) def {{plural.id}}_with(criteria)
{{plural.id}}_with(criteria){} {{plural.id}}_with(criteria){}
end end