Skip to content
Newer
Older
100644 140 lines (119 sloc) 3.77 KB
ce0aa83 Initial commit.
xw temp authored
1 module Habitat
2 class RspecExampleGroup < defined?(Merb) ? Merb::Test::ExampleGroup : Spec::Example::ExampleGroup
3 cattr_accessor :free_browsers, :selenium_browser_creation_mutex, :browser_number
4 self.free_browsers = []
5 self.selenium_browser_creation_mutex = Mutex.new
6 self.browser_number = 0
7
8 attr_accessor :browsers
9 attr_accessor :allocated_browsers
10
11 before(:all) do
12 end
13
14 before(:each) do
15 self.browsers = {}
16 self.allocated_browsers = []
17 end
18
19 after(:each) do
20 #unless example_passed?
21 # basedir = ENV['CC_BUILD_ARTIFACTS'] || "/tmp"
22 # screenshot_name = "#{basedir}/selenium-screenshot-#{__full_description.gsub("/", "_")}.jpg"
23 # allocated_browsers.first.driver.capture_screenshot(screenshot_name)
24 #end
25
26 threads = allocated_browsers.map do |browser|
27 Thread.new do
28 browser.reset
29 end
30 end
31 join_all(threads)
32 allocated_browsers.each do |browser|
33 # N.B. Unshifting to preserve order
34 self.class.free_browsers.unshift browser
35 end
36
37 at_exit do
38 self.free_browsers.each(&:close)
39 self.free_browsers = []
40 end
41 end
42
43 def method_which_must_exist_to_make_testing_this_spec_work
44 end
45
46 def as(*actors, &block)
47 actors.each do |actor|
48 browser = browsers[actor] ||= begin
49 browser = allocate_browser
50 allocated_browsers << browser
51 browser.actor_name = actor
52 browser
53 end
54 begin
55 browser.run(&block)
56 rescue ::Exception => exception
57 exception.message.replace("As #{actor}: #{exception.message}")
58 raise exception
59 end
60 end
61 end
62
63 def concurrently(&block)
64 concurrent_action_collector = ConcurrentActionCollector.new(self)
65 concurrent_action_collector.instance_eval(&block)
66 concurrent_action_collector.run
67 end
68
69 def concurrently_as(*actors, &block)
70 concurrently do
71 as(*actors, &block)
72 end
73 end
74
75 class ConcurrentActionCollector
76 attr_reader :spec, :concurrent_procs
77
78 def initialize(spec)
79 @spec = spec
80 @concurrent_procs = []
81 end
82
83 def as(*actors, &block)
84 actors.each do |actor|
85 add_concurrent_proc(actor, &block)
86 end
87 end
88
89 def add_concurrent_proc(actor, &block)
90 concurrent_procs << lambda { spec.as(actor, &block) }
91 end
92
93 def run
94 threads = concurrent_procs.map do |proc|
95 Thread.new do
96 begin
97 proc.call
98 rescue Exception => e
99 puts "---- CAUGHT EXCEPTION IN ConcurrentActionCollector#run --------------------------------"
100 puts e.message
101 puts e.backtrace * "\n"
102 raise e
103 end
104 end
105 end
106 spec.join_all(threads)
107 end
108 end
109
110 def join_all(threads)
111 exceptions = []
112 threads.each do |thread|
113 begin
114 thread.join
115 rescue Exception => e
116 exceptions << e
117 end
118 end
119 p "There were multiple exceptions, just throwing the first one..." if exceptions.size > 1
120 raise exceptions.first unless exceptions.empty?
121 end
122
123 def allocate_browser
124 return self.class.free_browsers.pop unless self.class.free_browsers.empty?
125 driver = nil
126
127 # Selenium RC server appears to have a thread safety issue when opening Firefox browsers...
128 self.class.selenium_browser_creation_mutex.synchronize do
129 driver = Webrat::SeleniumSession.new
130 end
131 #driver.prepare_nifty_display_stuff self.browser_number
132 #driver.set_polonium_status "Preparing browser..."
133
134 self.browser_number += 1
135
136 Habitat::Browser.new(driver, self, "/commercial/login", Click::Commercial::LoginPage)
137 end
138
139 end
140 end
Something went wrong with that request. Please try again.