Restore clicks_link_within()

This commit is contained in:
Kyle Hargraves 2008-04-12 16:07:33 -05:00 committed by Kyle Hargraves
parent 7ab6dd720f
commit c2ace0efad
3 changed files with 28 additions and 25 deletions

View File

@ -35,7 +35,7 @@ module ActionController
current_page.save_and_open current_page.save_and_open
end end
[:reloads, :fills_in, :clicks_button, :selects, :chooses, :checks, :unchecks, :clicks_link, :clicks_put_link, :clicks_get_link, :clicks_post_link, :clicks_delete_link].each do |method_name| [:reloads, :fills_in, :clicks_button, :selects, :chooses, :checks, :unchecks, :clicks_link, :clicks_link_within, :clicks_put_link, :clicks_get_link, :clicks_post_link, :clicks_delete_link].each do |method_name|
define_method(method_name) do |*args| define_method(method_name) do |*args|
current_page.send(method_name, *args) current_page.send(method_name, *args)
end end

View File

@ -117,13 +117,14 @@ module Webrat
link.click link.click
end end
# # Works like clicks_link, but only looks for the link text within a given selector # Works like clicks_link, but only looks for the link text within a given selector
# # #
# # Example: # Example:
# # clicks_link_within "#user_12", "Vote" # clicks_link_within "#user_12", "Vote"
# def clicks_link_within(selector, link_text) def clicks_link_within(selector, link_text)
# clicks_one_link_of(links_within(selector), link_text) link = find_link(link_text, selector)
# end link.click
end
# Works like clicks_link, but forces a GET request # Works like clicks_link, but forces a GET request
# #
@ -206,10 +207,10 @@ module Webrat
end end
def find_link(text) def find_link(text, selector = nil)
matching_links = [] matching_links = []
links.each do |possible_link| links_within(selector).each do |possible_link|
matching_links << possible_link if possible_link.matches_text?(text) matching_links << possible_link if possible_link.matches_text?(text)
end end
@ -253,11 +254,13 @@ module Webrat
end end
def links def links
return @links if @links @links ||= links_within(nil)
end
@links = (dom / "a[@href]").map do |link_element|
def links_within(selector)
(dom / selector / "a[@href]").map do |link_element|
Link.new(self, link_element) Link.new(self, link_element)
end end
end end
def forms def forms

View File

@ -170,17 +170,17 @@ class ClicksLinkTest < Test::Unit::TestCase
@session.clicks_link "Link" @session.clicks_link "Link"
end end
# def test_should_click_link_within_a_selector def test_should_click_link_within_a_selector
# @response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
# <a href="/page1">Link</a> <a href="/page1">Link</a>
# <div id="container"> <div id="container">
# <a href="/page2">Link</a> <a href="/page2">Link</a>
# </div> </div>
# EOS EOS
#
# @session.expects(:get_via_redirect).with("/page2", {}) @session.expects(:get_via_redirect).with("/page2", {})
# @session.clicks_link_within "#container", "Link" @session.clicks_link_within "#container", "Link"
# end end
def test_should_not_make_request_when_link_is_local_anchor def test_should_not_make_request_when_link_is_local_anchor
@response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)