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

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 end
def link(locator) 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 end
def button(locator) def button(locator)
xpath = append("//input[@type='submit' or @type='image'][@id=#{s(locator)} or contains(@value,#{s(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 end
def text_area(locator) def text_area(locator)

View file

@ -115,6 +115,11 @@ module ClickButtonSpec
@session.click_button('Click') @session.click_button('Click')
extract_results(@session)['first_name'].should == 'John' extract_results(@session)['first_name'].should == 'John'
end 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 end
context "with id given on a button defined by <button> tag" do context "with id given on a button defined by <button> tag" do
@ -134,6 +139,11 @@ module ClickButtonSpec
@session.click_button('ck_me') @session.click_button('ck_me')
extract_results(@session)['first_name'].should == 'John' extract_results(@session)['first_name'].should == 'John'
end 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 end
context "with a locator that doesn't exist" do context "with a locator that doesn't exist" do

View file

@ -17,6 +17,16 @@ module ClickLinkSpec
@session.click_link('labore') @session.click_link('labore')
@session.body.should include('Bar') @session.body.should include('Bar')
end 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 end
context "with title given" do context "with title given" do
@ -24,6 +34,16 @@ module ClickLinkSpec
@session.click_link('awesome title') @session.click_link('awesome title')
@session.body.should include('Bar') @session.body.should include('Bar')
end 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 end
context "with a locator that doesn't exist" do 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"/> <input type="color" name="form[html5_color]" value="#FFF" id="html5_color"/>
</p> </p>
</form> </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> <p>
<input type="text" id="test_field" value="monkey"/> <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> </p>
<div id="hidden" style="display:none;"> <div id="hidden" style="display:none;">