Last active
May 6, 2016 10:56
-
-
Save the-undefined/61c8715b8ff380dc3c131d08bfbf75c0 to your computer and use it in GitHub Desktop.
Capybara convenience module for finding elements by `data-X` values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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