From 07364963355a8da135cd708da08b319d1a3cbd19 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 19 Aug 2021 11:08:49 +0900 Subject: [PATCH] make link clickable --- spec/page/link_spec.cr | 8 ++++++++ src/mechanize.cr | 6 ++++++ src/mechanize/page/link.cr | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/spec/page/link_spec.cr b/spec/page/link_spec.cr index e42db51..e876f94 100644 --- a/spec/page/link_spec.cr +++ b/spec/page/link_spec.cr @@ -14,4 +14,12 @@ describe "Mechanize Page Link test" do link = page.links.first link.text.should eq "link text" end + + it "is clickable and returns page" do + agent = Mechanize.new + page = agent.get("http://example.com/link") + link = page.links.first + page = link.click + page.uri.to_s.should eq "http://example.com/" + end end diff --git a/src/mechanize.cr b/src/mechanize.cr index 271d9da..5cd6dbc 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -113,4 +113,10 @@ class Mechanize def max_history=(length) history.max_size = length end + + # click link, and return page. + def click(link) + href = link.href + get href + end end diff --git a/src/mechanize/page/link.cr b/src/mechanize/page/link.cr index d302e0c..048d2ab 100644 --- a/src/mechanize/page/link.cr +++ b/src/mechanize/page/link.cr @@ -12,4 +12,9 @@ class MechanizeCr::PageContent::Link @href = node.fetch("href", "") @text = node.inner_text end + + # click on this link + def click + @mech.click self + end end