added id support on clicks_link, added tests for matches_text? and matches_id? into link spec

This commit is contained in:
gaffo 2008-10-28 21:32:55 -05:00
parent 067bc791c5
commit 397dec1436
2 changed files with 37 additions and 5 deletions

View File

@ -111,14 +111,22 @@ module Webrat
# Passing a :method in the options hash overrides the HTTP method used
# for making the link request
#
# It will try to find links by (in order of precedence):
# innerHTML, with simple   handling
# title
# id
#
# innerHTML and title are matchable by text subtring or Regexp
# id is matchable by full text equality or Regexp
#
# Example:
# clicks_link "Sign up"
#
# clicks_link "Sign up", :javascript => false
#
# clicks_link "Sign up", :method => :put
def clicks_link(link_text, options = {})
find_link(link_text).click(options)
def clicks_link(text_or_title_or_id, options = {})
find_link(text_or_title_or_id).click(options)
end
alias_method :click_link, :clicks_link
@ -214,15 +222,15 @@ module Webrat
end
end
def find_link(text, selector = nil)
def find_link(text_or_title_or_id, selector = nil)
matching_links = links_within(selector).select do |possible_link|
possible_link.matches_text?(text)
possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id)
end
if matching_links.any?
matching_links.min { |a, b| a.text.length <=> b.text.length }
else
flunk("Could not find link with text #{text.inspect}")
flunk("Could not find link with text or title or id #{text_or_title_or_id.inspect}")
end
end

View File

@ -21,6 +21,14 @@ describe "clicks_link" do
@session.clicks_link "Link text", :method => :get
end
it "should click link on substring" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "ink tex", :method => :get
end
it "should click delete links" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
@ -54,6 +62,22 @@ describe "clicks_link" do
@session.clicks_link /link [a-z]/i
end
it "should click links by id" do
@session.response_body = <<-EOS
<a id="link_text_link" href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "link_text_link"
end
it "should click links by id regexp" do
@session.response_body = <<-EOS
<a id="link_text_link" href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link /_text_/
end
it "should click rails javascript links with authenticity tokens" do
@session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form');