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:
parent
ec4687a7a4
commit
8b31aba0b1
2 changed files with 27 additions and 8 deletions
|
@ -129,8 +129,17 @@ module Capybara
|
||||||
end
|
end
|
||||||
|
|
||||||
def find(locator)
|
def find(locator)
|
||||||
|
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
|
all(locator.to_s).first
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def find_field(locator)
|
def find_field(locator)
|
||||||
find(XPath.field(locator))
|
find(XPath.field(locator))
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue