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
def link(locator)
xpath = append("//a[@href][@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)})]")
xpath.prepend("//a[@href][text()=#{s(locator)} or @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)} or img[@alt=#{s(locator)}]]")
end
def button(locator)

View File

@ -46,6 +46,23 @@ module ClickLinkSpec
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
it "should raise an error" do
running do
@ -85,4 +102,4 @@ module ClickLinkSpec
end
end
end
end
end

View File

@ -6,6 +6,7 @@
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation <a href="/foo" id="foo">ullamco</a> laboris nisi
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 id="second">
@ -25,8 +26,10 @@
<a href="/with_simple_html#anchor">Anchor on different page</a>
<a href="/with_html#anchor">Anchor on same page</a>
<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>
<div id="hidden" style="display:none;">
<div id="hidden_via_ancestor">Inside element with hidden ancestor</div>
</div>
</div>