Skip to content

Instantly share code, notes, and snippets.

@testobsessed
Created March 1, 2011 01:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save testobsessed/848439 to your computer and use it in GitHub Desktop.
Save testobsessed/848439 to your computer and use it in GitHub Desktop.
Check if element is invisible with Capybara
def element_visible?(element_id)
# does the element exist?
exists = page.has_css?(element_id)
# is the element itself hidden with the .ui-helper-hidden class?
self_hidden = page.has_css?("#{element_id}.ui-helper-hidden")
# is the parent of the element hidden, thus hiding the element?
parent_hidden = page.has_css?(".ui-helper-hidden > #{element_id}")
# is the grandparent of the element, or any other ancestor, hidden?
other_ancestor_hidden = page.has_css?(".ui-helper-hidden * #{element_id}")
# if any of the above conditions are true, then the element is invisible
invisible = self_hidden || parent_hidden || other_ancestor_hidden
# the element is visible if it exists and it is not invisible
return (exists && !invisible)
end
@JeremyEllingham
Copy link

Very helpful, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment