From eba8ad6d6b8f824a8a7a495dde4dca881989aff6 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Wed, 16 Jun 2021 14:58:49 +0900 Subject: [PATCH] wip add checkbox --- spec/form_spec.cr | 17 ++++++++++++----- src/mechanize/form.cr | 8 +++++++- src/mechanize/form/check_box.cr | 2 -- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spec/form_spec.cr b/spec/form_spec.cr index 47d823b..c5ffe76 100644 --- a/spec/form_spec.cr +++ b/spec/form_spec.cr @@ -10,7 +10,7 @@ WebMock.stub(:get, "html_example.com").to_return(body:
- +
@@ -22,6 +22,7 @@ describe "Mechanize Form test" do uri = "http://html_example.com/" page = agent.get(uri) form = page.forms.first + it "retrun form attribute" do form.action.should eq "post_path" form.method.should eq "POST" @@ -29,14 +30,20 @@ describe "Mechanize Form test" do form.name.should eq "sample_form" end + it "includes fields" do + form.fields.size.should eq 2 + end + context "Form Fields" do - it "forms include fields" do - form.fields.size.should eq 3 - end - it "return field attribute" do + it "returns field attribute" do field = form.fields.first field.type.should eq "text" field.name.should eq "name" end end + + context "Form Fields CheckBox" do + checkbox = form.checkboxes.first + p checkbox.checked? + end end diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index be3334f..c5670fc 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -58,7 +58,13 @@ class MechanizeCr::Form @checkboxes = Array(MechanizeCr::FormContent::CheckBox).new @node.css("input").not_nil!.each do |html_node| html_node = html_node.as(Myhtml::Node) - @fields << MechanizeCr::FormContent::Field.new(html_node) + type = (html_node["type"] || "text").downcase + case type + when "checkbox" + @checkboxes << MechanizeCr::FormContent::CheckBox.new(html_node) + else + @fields << MechanizeCr::FormContent::Field.new(html_node) + end end end diff --git a/src/mechanize/form/check_box.cr b/src/mechanize/form/check_box.cr index 3e3218b..7de65f3 100644 --- a/src/mechanize/form/check_box.cr +++ b/src/mechanize/form/check_box.cr @@ -1,10 +1,8 @@ class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::Field property :checked - property :form def initialize(node : Node | Myhtml::Node, value = "") @checked = !!node["checked"] - @form = form super(node, value) end