add link#href, #text test

master
Kanezoh 2021-08-19 09:49:53 +09:00
parent 534ab0a47a
commit d34a20c73c
3 changed files with 25 additions and 8 deletions

17
spec/page/link_spec.cr Normal file
View File

@ -0,0 +1,17 @@
require "../spec_helper"
describe "Mechanize Page Link test" do
it "returns href" do
agent = Mechanize.new
page = agent.get("http://example.com/link")
link = page.links.first
link.href.should eq "http://example.com/"
end
it "returns text" do
agent = Mechanize.new
page = agent.get("http://example.com/link")
link = page.links.first
link.text.should eq "link text"
end
end

View File

@ -29,7 +29,7 @@ WebMock.stub(:get, "example.com/link").to_return(body: <<-BODY
<title>page_title</title>
</head>
<body>
<a href="http://example.com/"></a>
<a href="http://example.com/">link text</a>
</body>
</html>
BODY

View File

@ -1,15 +1,15 @@
class MechanizeCr::PageContent::Link
property node : Lexbor::Node
property page : Page
property mech : Mechanize
getter node : Lexbor::Node
getter page : Page
getter mech : Mechanize
getter href : String
getter text : String
def initialize(node, mech, page)
@node = node
@page = page
@mech = mech
# @attributes = node
# @href = node['href']
# @text = nil
# @uri = nil
@href = node.fetch("href", "")
@text = node.inner_text
end
end