wip add checkbox

This commit is contained in:
Kanezoh 2021-06-16 14:58:49 +09:00
parent 76d09d6802
commit eba8ad6d6b
3 changed files with 19 additions and 8 deletions

View File

@ -10,7 +10,7 @@ WebMock.stub(:get, "html_example.com").to_return(body:
<form action="post_path" method="post" name="sample_form"> <form action="post_path" method="post" name="sample_form">
<input type="text" name="name"> <input type="text" name="name">
<input type="text" name="email"> <input type="text" name="email">
<input type="submit" value=""> <input type="checkbox" id="ababa" name="ababa" checked>
</form> </form>
</body> </body>
</html> </html>
@ -22,6 +22,7 @@ describe "Mechanize Form test" do
uri = "http://html_example.com/" uri = "http://html_example.com/"
page = agent.get(uri) page = agent.get(uri)
form = page.forms.first form = page.forms.first
it "retrun form attribute" do it "retrun 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"
@ -29,14 +30,20 @@ describe "Mechanize Form test" do
form.name.should eq "sample_form" form.name.should eq "sample_form"
end end
context "Form Fields" do it "includes fields" do
it "forms include fields" do form.fields.size.should eq 2
form.fields.size.should eq 3
end end
it "return field attribute" do
context "Form Fields" do
it "returns field attribute" do
field = form.fields.first field = form.fields.first
field.type.should eq "text" field.type.should eq "text"
field.name.should eq "name" field.name.should eq "name"
end end
end end
context "Form Fields CheckBox" do
checkbox = form.checkboxes.first
p checkbox.checked?
end
end end

View File

@ -58,9 +58,15 @@ class MechanizeCr::Form
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new @checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
@node.css("input").not_nil!.each do |html_node| @node.css("input").not_nil!.each do |html_node|
html_node = html_node.as(Myhtml::Node) html_node = html_node.as(Myhtml::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) @fields << MechanizeCr::FormContent::Field.new(html_node)
end end
end end
end
private def build_query_string(params : Array(Array(String))) private def build_query_string(params : Array(Array(String)))
params.reduce("") do |acc, arr| params.reduce("") do |acc, arr|

View File

@ -1,10 +1,8 @@
class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::Field class MechanizeCr::FormContent::CheckBox < MechanizeCr::FormContent::Field
property :checked property :checked
property :form
def initialize(node : Node | Myhtml::Node, value = "") def initialize(node : Node | Myhtml::Node, value = "")
@checked = !!node["checked"] @checked = !!node["checked"]
@form = form
super(node, value) super(node, value)
end end