Created
March 31, 2016 00:11
-
-
Save grantspeelman/cf1a333ea187285f6adfb1b929d3b198 to your computer and use it in GitHub Desktop.
Acceptance testing multiple sessions/actors using capybara
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
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 |
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
class SharingAShoppingListTest < FeatureTest | |
setup do | |
@grant = ShopperPersonaSession.new(email: '[email protected]') | |
@grant.sign_up | |
@kate = ShopperPersonaSession.new(email: '[email protected]') | |
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: '[email protected]' | |
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 |
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
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