Last active
September 7, 2018 13:06
-
-
Save tomsabin/652be7272c9829bde9f4409e95ae57bd to your computer and use it in GitHub Desktop.
Custom RSpec helper `in_another_session` or `in_session(:name)` to run expectations within different browser sessions
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
RSpec.feature "Welcome message" do | |
sign_in | |
visit "/" | |
expect(page).to have_content "Hello Joe Bloggs" | |
in_another_session do | |
visit "/" | |
expect(page).to have_content "Welcome" | |
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
RSpec.feature "Welcome message" do | |
in_session :visitor do | |
visit "/" | |
expect(page).to have_content "Welcome" | |
end | |
in_session :authorised_user do | |
sign_in(authorised_user) | |
visit "/" | |
expect(page).to have_content "Hello Joe Bloggs" | |
end | |
in_session :unauthorised_user do | |
sign_in(unauthorised_user) | |
visit "/" | |
expect(page).to have_content "Your subscription has expired" | |
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
module FeatureHelpers | |
def in_another_session(new_session = :other, &block) | |
old_session = Capybara.session_name | |
Capybara.session_name = new_session | |
driver = Capybara.current_session.driver | |
block.call(driver) | |
Capybara.session_name = old_session | |
end | |
alias_method :in_session, :in_another_session | |
end | |
RSpec.configure do |config| | |
config.include FeatureHelpers, type: :feature | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment