Last Updated: February 25, 2016
·
1.79K
· felipeelias

Add Capybara selector for your models

If you add this selector:

Capybara.add_selector :record do
  xpath { |record| XPath.css("#" + ActionController::RecordIdentifier.dom_id(record)) }
  match { |record| record.is_a?(ActiveRecord::Base) }
end

Capybara automatically identifies that you passed a model in find and within methods and selects with the appropriate xpath.

Example:

post = Post.first

find(post).click_link("Hello")

within post do
  fill_in("What's going on here", :with => "Holy shit this works??")
  click_button("Yes")
end

For Mongoid, use:

match { |record| record.class.ancestors.include? Mongoid::Document }

Via https://gist.github.com/4071870


endorse