Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require File.dirname(__FILE__) + "/../test_helper"
class PlayingGameTest < Bbq::TestCase
include Bbq::SessionPool
background do
@admin = session_pool.create_user(TestUser, :driver => :selenium)
@admin.roles(:admin)
@admin.prepare_game_data
@user = session_pool.create_user(TestUser, :driver => :selenium)
@user.roles(:player)
@user.create_facebook_account
end
scenario "..." do
# ...
end
end
module Bbq
module SessionPool
def self.shared_pool
@shared_pool ||= SharedPool.new
end
def self.included(base)
base.class_eval do
def session_pool
Bbq::SessionPool.shared_pool
end
teardown do
Bbq::SessionPool.shared_pool.release_all
end
end
end
class SharedPool
def initialize
@driver_pools = {}
end
def create_user(klass, options)
driver = options[:driver]
@driver_pools[driver] ||= DriverPool.new
@driver_pools[driver].create_user(klass, options)
end
def release_all
@driver_pools.each_value(&:release_all)
end
end
class DriverPool
attr_accessor :idle, :users
def initialize
self.idle = []
self.users = []
end
def create_user(klass, options)
if idle.length > 0
session = idle.pop
else
session = SecureRandom.hex(8)
end
user = klass.new(options.merge(:session_name => session))
users.push(user)
user
end
def release_all
users.each do |user|
user.page.reset!
idle.push(user.session_name)
end
users.clear
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.