Prefer exact matches over partial, links & buttons

This aligns links and buttons with the behaviour
of fields for more consistent behaviour
This commit is contained in:
Jonas Nicklas 2010-01-11 21:18:32 +01:00
parent 67e834245c
commit 159424151b
5 changed files with 46 additions and 2 deletions

View File

@ -56,12 +56,15 @@ module Capybara
end
def link(locator)
append("//a[@id=#{s(locator)} or contains(.,#{s(locator)}) or @title=#{s(locator)}]")
xpath = append("//a[@id=#{s(locator)} or contains(.,#{s(locator)}) or contains(@title,#{s(locator)})]")
xpath.prepend("//a[text()=#{s(locator)} or @title=#{s(locator)}]")
end
def button(locator)
xpath = append("//input[@type='submit' or @type='image'][@id=#{s(locator)} or contains(@value,#{s(locator)})]")
xpath.append("//button[@id=#{s(locator)} or contains(@value,#{s(locator)}) or contains(.,#{s(locator)})]")
xpath = xpath.append("//button[@id=#{s(locator)} or contains(@value,#{s(locator)}) or contains(.,#{s(locator)})]")
xpath = xpath.prepend("//input[@type='submit' or @type='image'][@value=#{s(locator)}]")
xpath = xpath.prepend("//button[@value=#{s(locator)} or text()=#{s(locator)}]")
end
def text_area(locator)

View File

@ -115,6 +115,11 @@ module ClickButtonSpec
@session.click_button('Click')
extract_results(@session)['first_name'].should == 'John'
end
it "should prefer exact matches over partial matches" do
@session.click_button('Just an input')
extract_results(@session)['button'].should == 'button_second'
end
end
context "with id given on a button defined by <button> tag" do
@ -134,6 +139,11 @@ module ClickButtonSpec
@session.click_button('ck_me')
extract_results(@session)['first_name'].should == 'John'
end
it "should prefer exact matches over partial matches" do
@session.click_button('Just a button')
extract_results(@session)['button'].should == 'Just a button'
end
end
context "with a locator that doesn't exist" do

View File

@ -17,6 +17,16 @@ module ClickLinkSpec
@session.click_link('labore')
@session.body.should include('Bar')
end
it "should accept partial matches" do
@session.click_link('abo')
@session.body.should include('Bar')
end
it "should prefer exact matches over partial matches" do
@session.click_link('A link')
@session.body.should include('Bar')
end
end
context "with title given" do
@ -24,6 +34,16 @@ module ClickLinkSpec
@session.click_link('awesome title')
@session.body.should include('Bar')
end
it "should accept partial matches" do
@session.click_link('some tit')
@session.body.should include('Bar')
end
it "should prefer exact matches over partial matches" do
@session.click_link('a fine link')
@session.body.should include('Bar')
end
end
context "with a locator that doesn't exist" do

View File

@ -168,3 +168,12 @@
<input type="color" name="form[html5_color]" value="#FFF" id="html5_color"/>
</p>
</form>
<form action="/form" method="post">
<p>
<button type="submit" name="form[button]" value="button_first">Just an input that came first</button>
<button type="submit" name="form[button]" value="button_second">Just an input</button>
<input type="submit" name="form[button]" value="Just a button that came first"/>
<input type="submit" name="form[button]" value="Just a button"/>
</p>
</form>

View File

@ -16,6 +16,8 @@
<p>
<input type="text" id="test_field" value="monkey"/>
<a title="twas a fine link" href="/redirect">A link came first</a>
<a title="a fine link" href="/with_simple_html">A link</a>
</p>
<div id="hidden" style="display:none;">