From f0ac299bb33ab3fc82a111b97a547562ffd0e03f Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 19 Aug 2021 08:44:05 +0900 Subject: [PATCH 1/5] add page class, and Page#links --- src/mechanize/page.cr | 9 +++++++++ src/mechanize/page/link.cr | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/mechanize/page/link.cr diff --git a/src/mechanize/page.cr b/src/mechanize/page.cr index d9f0f04..4b7a474 100644 --- a/src/mechanize/page.cr +++ b/src/mechanize/page.cr @@ -1,5 +1,6 @@ require "./file" require "./utils/element_matcher" +require "./page/link" class MechanizeCr::Page < MechanizeCr::File include MechanzeCr::ElementMatcher @@ -33,6 +34,14 @@ class MechanizeCr::Page < MechanizeCr::File end.to_a end + def links + links = %w{a area}.map do |tag| + css(tag).map do |node| + PageContent::Link.new(node, @mech, self) + end + end.flatten + end + # generate form_with, forms_with methods # ex) form_with({:name => "login_form"}) # it detects form(s) which match conditions. diff --git a/src/mechanize/page/link.cr b/src/mechanize/page/link.cr new file mode 100644 index 0000000..469c7ac --- /dev/null +++ b/src/mechanize/page/link.cr @@ -0,0 +1,15 @@ +class MechanizeCr::PageContent::Link + property node : Lexbor::Node + property page : Page + property mech : Mechanize + + def initialize(node, mech, page) + @node = node + @page = page + @mech = mech + # @attributes = node + # @href = node['href'] + # @text = nil + # @uri = nil + end +end From 534ab0a47aedfe257c6ea9eba185b2e4d6e7f1d5 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 19 Aug 2021 09:02:14 +0900 Subject: [PATCH 2/5] add Page#links test --- spec/page_spec.cr | 6 ++++++ spec/spec_helper.cr | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/spec/page_spec.cr b/spec/page_spec.cr index 4c2f4a1..88b9840 100644 --- a/spec/page_spec.cr +++ b/spec/page_spec.cr @@ -45,4 +45,10 @@ describe "Mechanize Page test" do form = page.form_with({name: "sample_form"}) form.name.should eq "sample_form" end + + it "return page links" do + agent = Mechanize.new + page = agent.get("http://example.com/link") + page.links.size.should eq 1 + end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 74be39a..6000e77 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -22,6 +22,19 @@ WebMock.stub(:get, "example.com/form").to_return(body: <<-BODY BODY ) + +WebMock.stub(:get, "example.com/link").to_return(body: <<-BODY + + + page_title + + + ページのリンク + + +BODY +) + WebMock.stub(:post, "example.com/post_path") .with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}) .to_return(body: "success") From d34a20c73c6c13115d5444d8d40233840d0617a7 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 19 Aug 2021 09:49:53 +0900 Subject: [PATCH 3/5] add link#href, #text test --- spec/page/link_spec.cr | 17 +++++++++++++++++ spec/spec_helper.cr | 2 +- src/mechanize/page/link.cr | 14 +++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 spec/page/link_spec.cr diff --git a/spec/page/link_spec.cr b/spec/page/link_spec.cr new file mode 100644 index 0000000..e42db51 --- /dev/null +++ b/spec/page/link_spec.cr @@ -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 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 6000e77..a6d8251 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -29,7 +29,7 @@ WebMock.stub(:get, "example.com/link").to_return(body: <<-BODY page_title - ページのリンク + link text BODY diff --git a/src/mechanize/page/link.cr b/src/mechanize/page/link.cr index 469c7ac..d302e0c 100644 --- a/src/mechanize/page/link.cr +++ b/src/mechanize/page/link.cr @@ -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 From 48549dcaa29df6389da2d237ddaa561fef2cc24a Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 19 Aug 2021 10:42:11 +0900 Subject: [PATCH 4/5] add area --- spec/page_spec.cr | 4 ++-- spec/spec_helper.cr | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/page_spec.cr b/spec/page_spec.cr index 88b9840..d31b35e 100644 --- a/spec/page_spec.cr +++ b/spec/page_spec.cr @@ -46,9 +46,9 @@ describe "Mechanize Page test" do form.name.should eq "sample_form" end - it "return page links" do + it "return page links, links means and " do agent = Mechanize.new page = agent.get("http://example.com/link") - page.links.size.should eq 1 + page.links.size.should eq 2 end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index a6d8251..f3394a2 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -30,6 +30,10 @@ WebMock.stub(:get, "example.com/link").to_return(body: <<-BODY link text + + + BODY From 07364963355a8da135cd708da08b319d1a3cbd19 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 19 Aug 2021 11:08:49 +0900 Subject: [PATCH 5/5] 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