include ApplicationHelper

def cookie_jar
   Capybara.current_session.driver.browser.current_session.instance_variable_get(:@rack_mock_session).cookie_jar
end

def sign_in(user)
  visit signin_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "SIGN IN"
  # Sign in when not using Capybara as well.
  cookie_jar[:remember_token] = user.remember_token
end


def sign_in_poltergeist(user)
  visit signin_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "SIGN IN"

  if Capybara.javascript_driver == :poltergeist
    page.driver.set_cookie(:remember_token, user.remember_token)
  else
    page.driver.browser.manage.add_cookie :name => :remember_token, :value => user.remember_token
  end
end