Skip to content

Instantly share code, notes, and snippets.

@mrmemes-eth
Created March 30, 2011 18:24
Show Gist options
  • Save mrmemes-eth/894947 to your computer and use it in GitHub Desktop.
Save mrmemes-eth/894947 to your computer and use it in GitHub Desktop.
Scope assertions to portions of the page
module SectionLocator
def within_parent(content, elements = ['*'], &block)
expr = %(//*[(#{elements.join('|')})/descendant-or-self::*[contains(., "#{content}")]])
within(:xpath, expr, &block)
end
def within_parent_preceding(content, elements = ['*'], &block)
expr = %(//*[(#{elements.join('|')})[contains(., "#{content}")]]/..)
within(:xpath, expr, &block)
end
end
World(SectionLocator)
sections = %w(h1 h2 h3 h4 h5 h6 legend caption dt strong p label a)
When /^(.*) in the "([^\"]*)" section$/ do |action, title|
within_parent(title, sections) do
When action
end
end
When /^(.*) in the "([^\"]*)" section:$/ do |action, title, table|
within_parent(title, sections) do
When "#{action}:", table
end
end
When /^(.*) in the "([^\"]*)" row$/ do |action, title|
within_parent(title, %w(th td)) do
When action
end
end
When /^(.*) in the section before "([^\"]*)"/ do |action, title|
within_parent_preceding(title, sections) do
When action
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment