diff --git a/spec/page/link_spec.cr b/spec/page/link_spec.cr new file mode 100644 index 0000000..e876f94 --- /dev/null +++ b/spec/page/link_spec.cr @@ -0,0 +1,25 @@ +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 + + 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/spec/page_spec.cr b/spec/page_spec.cr index 4c2f4a1..d31b35e 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, links means and " do + agent = Mechanize.new + page = agent.get("http://example.com/link") + page.links.size.should eq 2 + end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 74be39a..f3394a2 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -22,6 +22,23 @@ WebMock.stub(:get, "example.com/form").to_return(body: <<-BODY BODY ) + +WebMock.stub(:get, "example.com/link").to_return(body: <<-BODY + + + page_title + + + link text + + + + + +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") 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.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..048d2ab --- /dev/null +++ b/src/mechanize/page/link.cr @@ -0,0 +1,20 @@ +class MechanizeCr::PageContent::Link + 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 + @href = node.fetch("href", "") + @text = node.inner_text + end + + # click on this link + def click + @mech.click self + end +end