1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Added support for following links containing only an image via it's alt-attribute

Given the following HTML:
<a href="/foo"><img src="img.jpg" alt="Img alt text" /><a/>

@session.click_link("Img alt text") will now work and follow to "/foo"
This commit is contained in:
Thorbjørn Hermansen 2010-01-15 16:13:48 +01:00
parent fe911868fc
commit 233dbbde4c
3 changed files with 24 additions and 4 deletions

View file

@ -56,8 +56,8 @@ module Capybara
end end
def link(locator) def link(locator)
xpath = append("//a[@href][@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)})]") xpath = append("//a[@href][@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)}) or img[contains(@alt,#{s(locator)})]]")
xpath.prepend("//a[@href][text()=#{s(locator)} or @title=#{s(locator)}]") xpath.prepend("//a[@href][text()=#{s(locator)} or @title=#{s(locator)} or img[@alt=#{s(locator)}]]")
end end
def button(locator) def button(locator)

View file

@ -46,6 +46,23 @@ module ClickLinkSpec
end end
end end
context "with alternative text given to a contained image" do
it "should take user to the linked page" do
@session.click_link('awesome image')
@session.body.should include('Bar')
end
it "should take user to the linked page" do
@session.click_link('some imag')
@session.body.should include('Bar')
end
it "should prefer exact matches over partial matches" do
@session.click_link('fine image')
@session.body.should include('Bar')
end
end
context "with a locator that doesn't exist" do context "with a locator that doesn't exist" do
it "should raise an error" do it "should raise an error" do
running do running do

View file

@ -6,6 +6,7 @@
et dolore magna aliqua. Ut enim ad minim veniam, et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation <a href="/foo" id="foo">ullamco</a> laboris nisi quis nostrud exercitation <a href="/foo" id="foo">ullamco</a> laboris nisi
ut aliquip ex ea commodo consequat. ut aliquip ex ea commodo consequat.
<a href="/with_simple_html"><img src="http://www.foobar.sun/dummy_image.jpg" width="20" height="20" alt="awesome image" /></a>
</p> </p>
<p id="second"> <p id="second">
@ -25,6 +26,8 @@
<a href="/with_simple_html#anchor">Anchor on different page</a> <a href="/with_simple_html#anchor">Anchor on different page</a>
<a href="/with_html#anchor">Anchor on same page</a> <a href="/with_html#anchor">Anchor on same page</a>
<input type="text" value="" id="test_field"> <input type="text" value="" id="test_field">
<a href="/redirect"><img src="http://www.foobar.sun/dummy_image.jpg" width="20" height="20" alt="very fine image" /></a>
<a href="/with_simple_html"><img src="http://www.foobar.sun/dummy_image.jpg" width="20" height="20" alt="fine image" /></a>
</p> </p>
<div id="hidden" style="display:none;"> <div id="hidden" style="display:none;">