Skip to content

Instantly share code, notes, and snippets.

@jdudek jdudek/bbq_java.rb forked from paneq/bbq_java.rb
Created Feb 8, 2012

Embed
What would you like to do?
bbq factories
class Session
# some kind of delegator is needed here
# delegate everything to @session
def initialize(options)
@session = Capybara::Session.new(options[:driver], Bbq.app)
end
def kill
page.driver.quit if page.driver.browser?
end
end
class PooledSessionFactory
attr_accessor :idle, :taken
def initialize(session_factory)
@idle = []
@taken = []
end
def new(options)
take_idle(options) || create(options)
end
def release
taken.each(&:reset!)
idle.concat(taken)
taken.clear
end
private
def take_idle(options)
driver = options[:driver]
idle.find { |s| s.mode == driver }.tap do |session|
if session
idle.delete(session)
taken.push(session)
end
end
end
def create(options)
session_factory.create(options)
end
end
class NonPooledSessionFactory
attr_accessor :taken
def initialize(session_factory)
@taken = []
end
def new(options)
taken << session = session_factory.create(options)
session
end
def release
@taken.select{|u| u.mode == :selenium}.each do |u|
u.kill
end
end
end
class SelectablePoolSessionFactory
def initialize(default_pool_session_factory, non_pool_session_factory)
end
def new(options)
if options.key?(:pool) && options[:pool].blank?
non_pool_session_factory.new(options)
else
default_pool_session_factory.new(options)
end
end
end
class Session
class << self
attr_accessor :default_factory
end
end
class TestCase
def new_test_user(options)
sessions_factory = options.delete(:sessions_factory) || Session.default_factory
test_user_factory = options.delete(:factory) || options.delete(:klass) || TestUser # klass is just to reduce confusion
test_user_factory.new(:session => sessions_factory.new)
end
end
class AppTest < TestCase
def test_lorem_ipsum
psf = PooledSessionFactory.new(Session)
nsf = NonPooledSessionFactory.new(Session)
spsf = SelectablePoolSessionFactory.new(psf, nsf)
Session.default_factory = spsf
tu = new_test_user
tu = new_test_user(:pool => true)
tu = new_test_user(:pool => true, :driver => :selenium)
tu = new_test_user(:pool => false)
tu = new_test_user(:pool => nil)
tu = new_test_user(:pool => nil, :factory => AdminUser)
tu = new_test_user(:pool => nil, :klass => AdminUser)
# want different pool ?
psf2 = PooledSessionFactory.new(Session)
tu = new_test_user(:sessions_factory => psf2)
#or
psf2 = PooledSessionFactory.new(Session)
tu = TestUser.new(:session => psf2.new)
# backwards compatibility
tu = TestUser.new
tu = TestUser.new(:driver => :selenium)
end
end
###
# backwards compatibility
class TestUser
def initialize(options)
@session = options[:session] || Session.default_factory.new(options)
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.