From b4c5e330375225c5225f21d2a14a5c5b5cbee620 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Mon, 28 Jun 2021 16:51:04 +0900 Subject: [PATCH] add checkbox_with method --- spec/form/check_box_spec.cr | 19 +++++++++++++++++-- src/mechanize/form.cr | 1 + src/mechanize/utils/element_matcher.cr | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spec/form/check_box_spec.cr b/spec/form/check_box_spec.cr index 7f227b2..714ec04 100644 --- a/spec/form/check_box_spec.cr +++ b/spec/form/check_box_spec.cr @@ -8,7 +8,8 @@ WebMock.stub(:get, "example.com/form/check_box").to_return(body:
- + +
@@ -19,13 +20,14 @@ describe "Form Fields CheckBox" do agent = Mechanize.new page = agent.get("http://example.com/form/check_box") form = page.forms[0] - checkbox = form.checkboxes.first it "returns checkbox status" do + checkbox = form.checkboxes.first checkbox.checked?.should eq true end it "can change check status" do + checkbox = form.checkboxes.first checkbox.checked?.should eq true checkbox.uncheck checkbox.checked?.should eq false @@ -39,8 +41,21 @@ describe "Form Fields CheckBox" do end 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") checkbox.uncheck form.request_data.should_not contain("remember_me=") 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 diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index a353221..35adb91 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -49,6 +49,7 @@ class MechanizeCr::Form elements_with "field" elements_with "radiobutton" + elements_with "checkbox", "checkboxes" private def parse @node.css("input").not_nil!.each do |html_node| diff --git a/src/mechanize/utils/element_matcher.cr b/src/mechanize/utils/element_matcher.cr index c2d1582..fcbb8ae 100644 --- a/src/mechanize/utils/element_matcher.cr +++ b/src/mechanize/utils/element_matcher.cr @@ -1,6 +1,6 @@ module MechanzeCr::ElementMatcher - macro elements_with(singular) - {% plural = "#{singular.id}s" %} + macro elements_with(singular, plural="") + {% plural = "#{singular.id}s" if plural.empty? %} def {{plural.id}}_with(criteria) {{plural.id}}_with(criteria){} end