Skip to content

Commit

Permalink
click_link now supports finding links using css class attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Premdas committed Mar 13, 2009
1 parent 742db2d commit ab47260
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/webrat/core/locators/link_locator.rb
Expand Up @@ -16,7 +16,8 @@ def link_element
def matching_links
@matching_links ||= link_elements.select do |link_element|
matches_text?(link_element) ||
matches_id?(link_element)
matches_id?(link_element) ||
matches_class?(link_element)
end
end

Expand All @@ -39,6 +40,15 @@ def matches_id?(link)
(Webrat::XML.attribute(link, "id") == @value) ? true : false
end
end

def matches_class?(link)
if @value.is_a?(Regexp)
(Webrat::XML.attribute(link, "class") =~ @value) ? true : false
else
Webrat::XML.attribute(link, "class").split(%r{\s}).include?(@value) unless Webrat::XML.attribute(link, "class").nil?
end
end


def link_elements
Webrat::XML.xpath_search(@dom, *Link.xpath_search)
Expand All @@ -53,7 +63,7 @@ def replace_nbsp_ref(str)
end

def error_message
"Could not find link with text or title or id #{@value.inspect}"
"Could not find link with text, title, id or class #{@value.inspect}"
end

end
Expand Down
30 changes: 30 additions & 0 deletions spec/public/click_link_spec.rb
Expand Up @@ -122,6 +122,36 @@
click_link /iddle/
end

it "should click links by class" do
with_html <<-HTML
<html>
<a class="piddle" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link 'piddle'
end

it "should click links by class when there are many classes" do
with_html <<-HTML
<html>
<a class="one two three" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link 'two'
end


it "should click links by class regex" do
with_html <<-HTML
<html>
<a class="piddlediddle" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link /iddle/
end

it "should click rails javascript links with authenticity tokens" do
with_html <<-HTML
Expand Down

0 comments on commit ab47260

Please sign in to comment.