Skip to content

Commit

Permalink
added id support on clicks_link, added tests for matches_text? and ma…
Browse files Browse the repository at this point in the history
…tches_id? into link spec
  • Loading branch information
gaffo committed Oct 29, 2008
1 parent 067bc79 commit 397dec1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/webrat/core/scope.rb
Expand Up @@ -111,14 +111,22 @@ def clicks_area(area_name)
# 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
Expand Down Expand Up @@ -214,15 +222,15 @@ def areas
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

Expand Down
24 changes: 24 additions & 0 deletions spec/api/clicks_link_spec.rb
Expand Up @@ -21,6 +21,14 @@
@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>
Expand Down Expand Up @@ -54,6 +62,22 @@
@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');
Expand Down

0 comments on commit 397dec1

Please sign in to comment.