Support relative links, including href="?foo=bar"

This commit is contained in:
Kyle Hargraves 2008-04-12 18:20:43 -05:00 committed by Kyle Hargraves
parent 7ab6dd720f
commit 32dc36592b
2 changed files with 29 additions and 1 deletions

View File

@ -10,7 +10,7 @@ module Webrat
method ||= http_method method ||= http_method
return if href =~ /^#/ && method == :get return if href =~ /^#/ && method == :get
Page.new(@page.session, href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}) Page.new(@page.session, absolute_href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token})
end end
def matches_text?(link_text) def matches_text?(link_text)
@ -27,6 +27,16 @@ module Webrat
@element["href"] @element["href"]
end end
def absolute_href
if href =~ /^\?/
"#{@page.url}#{href}"
elsif href !~ /^\//
"#{@page.url}/#{href}"
else
href
end
end
def authenticity_token def authenticity_token
return unless onclick && onclick.include?("s.setAttribute('name', 'authenticity_token');") && return unless onclick && onclick.include?("s.setAttribute('name', 'authenticity_token');") &&
onclick =~ /s\.setAttribute\('value', '([a-f0-9]{40})'\);/ onclick =~ /s\.setAttribute\('value', '([a-f0-9]{40})'\);/

View File

@ -190,4 +190,22 @@ class ClicksLinkTest < Test::Unit::TestCase
@session.expects(:send).with('get_via_redirect', '#section-1', {}).never @session.expects(:send).with('get_via_redirect', '#section-1', {}).never
@session.clicks_link "Jump to Section 1" @session.clicks_link "Jump to Section 1"
end end
def test_should_follow_relative_links
@session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS)
<a href="sub">Jump to sub page</a>
EOS
@session.expects(:get_via_redirect).with("/page/sub", {})
@session.clicks_link "Jump to sub page"
end
def test_should_follow_query_parameters
@session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS)
<a href="?foo=bar">Jump to foo bar</a>
EOS
@session.expects(:get_via_redirect).with("/page?foo=bar", {})
@session.clicks_link "Jump to foo bar"
end
end end