Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created December 28, 2009 23:25
Show Gist options
  • Save myronmarston/265027 to your computer and use it in GitHub Desktop.
Save myronmarston/265027 to your computer and use it in GitHub Desktop.
def find_element(xpath_expression, text, case_insensitive = false)
exp = if case_insensitive
xpath_expression % ["translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvqxyz')", text.downcase]
else
xpath_expression % ["text()", text]
end
unless elem = find(exp)
if case_insensitive
raise "Failed to find element using locator #{text}"
else
# As a convenience to the developer, try finding the element with a case insensitive search.
# This is slower than a case sensitive search, so we don't want to try it unless we really need it.
if elem = find_element(xpath_expression, text, true)
warn ["The locator #{text} did not match anything using a case sensitive search.",
"We found an element using a case-insensitive search, but this performs poorly.",
"We recommend that you fix the casing on this locator."].join("\n")
end
end
end
elem
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment