diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index b90004b2..f2ef35d8 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -138,12 +138,11 @@ module Capybara alias_method :field_labeled, :find_field def find_link(locator) - find("//a[@id='#{locator}' or contains(.,'#{locator}') or @title='#{locator}']") + find(XPath.link(locator).to_s) end def find_button(locator) - button = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']") - button || find("//button[@id='#{locator}' or @value='#{locator}' or contains(.,'#{locator}')]") + find(XPath.button(locator).to_s) end private diff --git a/lib/capybara/xpath.rb b/lib/capybara/xpath.rb index 9093a859..0fac8caf 100644 --- a/lib/capybara/xpath.rb +++ b/lib/capybara/xpath.rb @@ -38,6 +38,15 @@ module Capybara def fieldset(locator) append("//fieldset[@id=#{s(locator)} or contains(legend,#{s(locator)})]") end + + def link(locator) + append("//a[@id=#{s(locator)} or contains(.,#{s(locator)}) or @title=#{s(locator)}]") + end + + def button(locator) + xpath = append("//input[@type='submit' or @type='image'][@id=#{s(locator)} or @value=#{s(locator)}]") + xpath.append("//button[@id=#{s(locator)} or @value=#{s(locator)} or contains(.,#{s(locator)})]") + end def text_field(locator) append("//input[@type='text'][@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") @@ -71,7 +80,7 @@ module Capybara @paths.join(' | ') end - private + protected # Sanitize a String for putting it into an xpath query def s(string)