Skip to content

Instantly share code, notes, and snippets.

@grantspeelman
Created March 31, 2016 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantspeelman/cf1a333ea187285f6adfb1b929d3b198 to your computer and use it in GitHub Desktop.
Save grantspeelman/cf1a333ea187285f6adfb1b929d3b198 to your computer and use it in GitHub Desktop.
Acceptance testing multiple sessions/actors using capybara
class PersonaSession
include Capybara::DSL
include Capybara::Email::DSL
# override to stop usage of global current_session
def page
@page ||= Capybara::Session.new(Capybara.current_driver, Capybara.app)
end
def click_link_in_email(link_name)
email = open_email(@email)
host = Rails.configuration.action_mailer.default_url_options[:host]
link_path = email.find_link(link_name)[:href].gsub("http://#{host}", '')
visit link_path
end
def perform(&block)
instance_exec(&block)
end
end
class SharingAShoppingListTest < FeatureTest
setup do
@grant = ShopperPersonaSession.new(email: 'grant@example.com')
@grant.sign_up
@kate = ShopperPersonaSession.new(email: 'kate@example.com')
end
test 'Grant and kate work together on a shopping list' do
@grant.perform do
click_link 'Price Book'
click_link 'Invite'
fill_in 'Name', with: 'Kate'
fill_in 'Email', with: 'kate@example.com'
click_button 'Invite'
end
@kate.perform do
click_link_in_email 'My Price Book'
sign_up
click_button 'Accept'
end
@grant.perform do
click_link 'Shopping List'
click_on 'New Shopping List'
click_button 'Edit Title'
fill_in 'Title', with: 'Our Shopping'
click_button 'Update'
end
assert @grant.has_css?('span', text: 'Our Shopping')
assert @grant.has_no_css?('span', text: 'Update Failed')
@kate.perform do
click_link 'Shopping List'
click_link 'Items'
fill_in 'Item name', with: 'bread'
fill_in 'Unit', with: 'loaves'
fill_in 'Amount', with: '2'
click_button 'Add'
end
assert @kate.has_content?('bread')
@grant.click_link 'Refresh'
assert @grant.has_content?('bread')
end
end
class ShopperPersonaSession < PersonaSession
def initialize(email:)
super()
@email = email
@password = 'password'
end
def sign_up
visit '/shoppers/sign_up' unless current_path.try(:include?, '/shoppers/sign_up')
fill_in 'Email', with: @email
fill_in 'Password', with: @password
fill_in 'Password confirmation', with: @password
click_button 'Sign up'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment