Skip to content

Instantly share code, notes, and snippets.

@jferris
Created July 2, 2010 17:59
Show Gist options
  • Select an option

  • Save jferris/461676 to your computer and use it in GitHub Desktop.

Select an option

Save jferris/461676 to your computer and use it in GitHub Desktop.
When %{I follow "$link_text" closest to "$reference_text"} do |link_text, reference_text|
link_element = closest_to(reference_text, %{.//a[contains(., "#{link_text}")]})
link = Webrat::Link.load(webrat_session, link_element)
link.click
end
module ClosestTo
def closest_to(reference_text, selector)
elements = current_dom.search("h1, h2, h3, h4, h5, h6, td, li, p")
reference_element = elements_directly_containing(reference_text).first
unless reference_element
raise "Couldn't find element with text:\n#{reference_text}"
end
closest_element = nil
while reference_element && !closest_element
closest_element = reference_element.at(selector)
reference_element = reference_element.respond_to?(:parent) ? reference_element.parent : nil
end
closest_element or raise "Couldn't find element matching #{selector}"
end
end
World(ClosestTo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment