Skip to content

Instantly share code, notes, and snippets.

@the-undefined
Last active May 6, 2016 10:56
Show Gist options
  • Save the-undefined/61c8715b8ff380dc3c131d08bfbf75c0 to your computer and use it in GitHub Desktop.
Save the-undefined/61c8715b8ff380dc3c131d08bfbf75c0 to your computer and use it in GitHub Desktop.
Capybara convenience module for finding elements by `data-X` values
# frozen_string_literal: true
module DomHelper
# Executes the given block within the context of a specific node identified
# using its data-xxx attributes. It will take any number of arguments.
#
# eg:
# user = create_user()
# within_element(id: user.id) do
# click_on 'Edit'
# end
#
def within_element(**opts)
within(:xpath, "//*[#{data_attrs(opts)}]") do
yield
end
end
private
def data_attrs(opts)
opts
.map{|k,v| "@data-#{k}=\"#{v}\"" }
.join(' and ')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment