Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created April 28, 2014 14:27
Show Gist options
  • Save cheeyeo/11373763 to your computer and use it in GitHub Desktop.
Save cheeyeo/11373763 to your computer and use it in GitHub Desktop.
<table>
<thead>
<tr>
<th>Name</th>
<th>Email></th>
<th>Country</th>
<th></th>
</tr>
</thead>
<tbody data-collection="users">
<% @users.each do |user| %>
<tr data-resource="user">
<td><%= user.name %></td>
<td><%= mail_to user.email %></td>
<td><%= user.country %></td>
<td>
<%= link_to 'Edit', edit_user_path(user), class: 'button' %>
<%= button_to 'Delete', user, method: :delete %>
</td>
</tr>
<% end %>
</tbody>
</table>
Capybara.add_selector(:models_element) do
xpath { |name| XPath.css("[data-collection='#{name}']") }
end
Capybara.add_selector(:model_element) do
xpath { |name| XPath.css("[data-resource='#{name}']") }
end
module ResourceDomElements
RESOURCES = %w( user post )
def models_element(model_name)
find(:models_element, model_name.pluralize)
end
def have_models_element(model_name)
have_selector(:models_element, text: model_name.pluralize)
end
def model_element(model_name, options = {})
find(:model_element, model_name, options)
end
def have_model_element(model_name, options)
have_selector :model_element, model_name, options
end
RESOURCES.each do |model_name|
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def #{model_name.pluralize}_element
models_element('#{model_name.pluralize}')
end
def have_#{model_name.pluralize}_element
have_models_element('#{model_name.pluralize}')
end
def #{model_name}_element(options = {})
model_element('#{model_name}', options)
end
def have_#{model_name}_element(options)
have_model_element('#{model_name}', options)
end
METHODS
end
end
feature 'Viewing users in a table' do
include ResourceDomElements
scenario 'lists name, email, country and buttons to manage' do
create(:user, name: 'Henry', email: '[email protected]', country: 'United States')
create(:user, name: 'Richard', email: '[email protected]', country: 'Canada')
visit users_path
within users_element do
expect(page).to have_content 'Henry'
expect(page).to have_content 'Richard'
end
within user_element(text: 'Henry') do
expect(page).to have_link '[email protected]', href: 'mailto:[email protected]'
expect(page).to have_content 'United States'
expect(page).to have_link 'Edit'
expect(page).to have_button 'Delete'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment