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
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
def find_field(locator)

View File

@ -49,31 +49,31 @@ module Capybara
end
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
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
def text_area(locator)
append("//textarea[@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]")
add_field(locator) { |id| "//textarea[@id=#{id}]" }
end
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
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
def select(locator)
append("//select[@id=#{s(locator)} or @id=//label[contains(.,#{s(locator)})]/@for]")
add_field(locator) { |id| "//select[@id=#{id}]" }
end
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
def to_s
@ -82,6 +82,12 @@ module Capybara
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
def s(string)
if string.include?("'")
@ -93,6 +99,10 @@ module Capybara
"'#{string}'"
end
end
def prepend(path)
XPath.new(*[path, @paths].flatten)
end
def append(path)
XPath.new(*[@paths, path].flatten)