From 22c6a4a42acd96ad7f6568cec70d18f1e86fe955 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Fri, 11 Jun 2021 11:18:25 +0900 Subject: [PATCH] add form test --- spec/form_spec.cr | 42 +++++++++++++++++++++++++++++++++++++ src/mechanize/form/field.cr | 17 +++++++++------ 2 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 spec/form_spec.cr diff --git a/spec/form_spec.cr b/spec/form_spec.cr new file mode 100644 index 0000000..47d823b --- /dev/null +++ b/spec/form_spec.cr @@ -0,0 +1,42 @@ +require "./spec_helper" +WebMock.stub(:get, "html_example.com").to_return(body: +<<-BODY + + + + page_title + + +
+ + + +
+ + +BODY +) + +describe "Mechanize Form test" do + agent = Mechanize.new + 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" + form.enctype.should eq "application/x-www-form-urlencoded" + form.name.should eq "sample_form" + end + + context "Form Fields" do + it "forms include fields" do + form.fields.size.should eq 3 + end + it "return field attribute" do + field = form.fields.first + field.type.should eq "text" + field.name.should eq "name" + end + end +end diff --git a/src/mechanize/form/field.cr b/src/mechanize/form/field.cr index 10cb583..c6ac667 100644 --- a/src/mechanize/form/field.cr +++ b/src/mechanize/form/field.cr @@ -1,11 +1,16 @@ class MechanizeCr::FormContent::Field - property :node, :value, :name - def initialize(node : Node | Myhtml::Node, value : String = node.fetch("value", ""), name : String = node.fetch("name", "")) + getter :node + getter value : String + getter name : String + getter type : String + getter raw_value : String + + def initialize(node : Node | Myhtml::Node) @node = node - @name = name - #@raw_value = value - @value = value - #@type = node['type'] + @name = node.fetch("name", "") + @value = node.fetch("value", "") + @type = node.fetch("type", "") + @raw_value = value end def query_value