title: Multiple sessions in capybara
body: <h2>Background</h2>
  
  <p>I want to test logging in as multiple users on my webapp, and I am using cucumber and capybara to do this. I found <a href="https://tristandunn.com/2010/10/21/multiple-sessions-cucumber-selenium/" rel="nofollow">this link</a> on how to handle multiple sessions, but unfortunately, it doesn't look like cucumber can find the <code>in_session</code> method that is defined. How can I access it?</p>
  
  <h2>Cucumber scenario</h2>
  
  <pre><code>Given I go to teacher login in Steve's browser
  
  When I enter "Steve" in the username field 
       And I enter "StevesPassword" in the password field 
       And I click login 
  Then I should see "Steve Lastname" as the current user
  
  When I go to teacher login in Lisa's browser
       And I enter "Lisa" in the username field 
       And I enter "LisasPassword" in the password field 
       And I click login 
  Then I should see "Lisa Lastname" as the current user
  </code></pre>
  
  <h2>Ruby code</h2>
  
  <pre><code>#standard_definitions/switching_sessions.rb
  When /^(.*) in (.*) browser$/ do |next_step, name|
      in_session(name) do
          step next_step
      end 
  end
  </code></pre>
  
  <hr>
  
  <pre><code>  #features/support/sessions.rb
    module Capybara
    module Driver
      module Sessions
        def set_session(id)
          Capybara.instance_variable_set("@session_pool", {
            "#{Capybara.current_driver}#{Capybara.app.object_id}" =&gt; $sessions[id]
          })
        end
  
        def in_session(id, &amp;block)
          $sessions ||= {}
          $sessions[:default] ||= Capybara.current_session
          $sessions[id]       ||= Capybara::Session.new(Capybara.current_driver, Capybara.app)
          set_session(id)
  
          yield
  
          set_session(:default)
        end
      end
    end
  end
  </code></pre>
  
  <h2>Errors</h2>
  
  <p>When I run this, I get the following:</p>
  
  <pre><code>Scenario: multiple users                                  # features/Provebank/Provebank.feature:23
  Given I go to teacher login in Steve's browser         # features/step_definitions/standard_definitions/switching_sessions.rb:5
    undefined method `in_session' for Capybara::Driver::Sessions:Module (NoMethodError)
    ./features/step_definitions/standard_definitions/switching_sessions.rb:8:in `/^(.*) in (.*) browser$/'
    features/test.feature:24:in `Given I go to teacher login in Steve's browser'
  </code></pre>