Skip to content

Commit

Permalink
Prefer exact matches over partial, links & buttons
Browse files Browse the repository at this point in the history
This aligns links and buttons with the behaviour
of fields for more consistent behaviour
  • Loading branch information
jnicklas committed Jan 11, 2010
1 parent 67e8342 commit 1594241
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/capybara/xpath.rb
Expand Up @@ -56,12 +56,15 @@ def fieldset(locator)
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)
Expand Down
10 changes: 10 additions & 0 deletions spec/dsl/click_button_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/dsl/click_link_spec.rb
Expand Up @@ -17,13 +17,33 @@ 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
it "should take user to the linked page" do
@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
Expand Down
9 changes: 9 additions & 0 deletions spec/views/form.erb
Expand Up @@ -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>
2 changes: 2 additions & 0 deletions spec/views/with_html.erb
Expand Up @@ -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;">
Expand Down

0 comments on commit 1594241

Please sign in to comment.