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

Find exact label matches before partial matche

This commit is contained in:
Jonas Nicklas 2009-12-09 20:20:40 +01:00
parent ec4687a7a4
commit 8b31aba0b1
2 changed files with 27 additions and 8 deletions

View file

@ -129,7 +129,16 @@ module Capybara
end end
def find(locator) def find(locator)
all(locator.to_s).first case locator
when XPath
locator.paths.each do |path|
result = all(path).first
return result if result
end
return nil
else
all(locator.to_s).first
end
end end
def find_field(locator) def find_field(locator)

View file

@ -49,31 +49,31 @@ module Capybara
end end
def text_field(locator) def text_field(locator)
append("//input[@type='text'][@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") add_field(locator) { |id| "//input[@type='text'][@id=#{id}]" }
end end
def password_field(locator) def password_field(locator)
append("//input[@type='password'][@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") add_field(locator) { |id| "//input[@type='password'][@id=#{id}]" }
end end
def text_area(locator) def text_area(locator)
append("//textarea[@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") add_field(locator) { |id| "//textarea[@id=#{id}]" }
end end
def radio_button(locator) def radio_button(locator)
append("//input[@type='radio'][@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") add_field(locator) { |id| "//input[@type='radio'][@id=#{id}]" }
end end
def checkbox(locator) def checkbox(locator)
append("//input[@type='checkbox'][@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") add_field(locator) { |id| "//input[@type='checkbox'][@id=#{id}]" }
end end
def select(locator) def select(locator)
append("//select[@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") add_field(locator) { |id| "//select[@id=#{id}]" }
end end
def file_field(locator) def file_field(locator)
append("//input[@type='file'][@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]") add_field(locator) { |id| "//input[@type='file'][@id=#{id}]" }
end end
def to_s def to_s
@ -82,6 +82,12 @@ module Capybara
protected protected
def add_field(locator)
xpath = append(yield(s(locator)))
xpath.append(yield("//label[contains(.,#{s(locator)})]/@for"))
xpath.prepend(yield("//label[text()=#{s(locator)}]/@for"))
end
# Sanitize a String for putting it into an xpath query # Sanitize a String for putting it into an xpath query
def s(string) def s(string)
if string.include?("'") if string.include?("'")
@ -94,6 +100,10 @@ module Capybara
end end
end end
def prepend(path)
XPath.new(*[path, @paths].flatten)
end
def append(path) def append(path)
XPath.new(*[@paths, path].flatten) XPath.new(*[@paths, path].flatten)
end end