add form test

This commit is contained in:
Kanezoh 2021-06-11 11:18:25 +09:00
parent 376ae24eee
commit 22c6a4a42a
2 changed files with 53 additions and 6 deletions

42
spec/form_spec.cr Normal file
View File

@ -0,0 +1,42 @@
require "./spec_helper"
WebMock.stub(:get, "html_example.com").to_return(body:
<<-BODY
<html>
<meta>
<head>
<title>page_title</title>
</head>
<body>
<form action="post_path" method="post" name="sample_form">
<input type="text" name="name">
<input type="text" name="email">
<input type="submit" value="">
</form>
</body>
</html>
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

View File

@ -1,11 +1,16 @@
class MechanizeCr::FormContent::Field class MechanizeCr::FormContent::Field
property :node, :value, :name getter :node
def initialize(node : Node | Myhtml::Node, value : String = node.fetch("value", ""), name : String = node.fetch("name", "")) getter value : String
getter name : String
getter type : String
getter raw_value : String
def initialize(node : Node | Myhtml::Node)
@node = node @node = node
@name = name @name = node.fetch("name", "")
#@raw_value = value @value = node.fetch("value", "")
@value = value @type = node.fetch("type", "")
#@type = node['type'] @raw_value = value
end end
def query_value def query_value