3021: 4399: Refactor headless stuff into a module. Clear up new/start/stop use.
authorTom Clegg <tom@curoverse.com>
Tue, 6 Jan 2015 05:59:02 +0000 (00:59 -0500)
committerTom Clegg <tom@curoverse.com>
Tue, 6 Jan 2015 06:05:55 +0000 (01:05 -0500)
* Create one Headless per test process, when encountering the first
  test case that needs one.

* Call headless.start & stop exactly once for each test case that uses
  it.

apps/workbench/test/integration_helper.rb

index e0b82e5847326a31e21cc6918af48e82d36ddbf0..4c1d505d3ffdd156cb3e4581fa3c59889417db1d 100644 (file)
@@ -27,11 +27,52 @@ module WaitForAjax
   end
 end
 
+module HeadlessHelper
+  class HeadlessSingleton
+    def self.get
+      @headless ||= Headless.new reuse: false
+    end
+  end
+
+  Capybara.default_driver = :rack_test
+
+  def self.included base
+    base.class_eval do
+      setup do
+        Capybara.use_default_driver
+        @headless = false
+      end
+
+      teardown do
+        if @headless
+          @headless.stop
+          @headless = false
+        end
+      end
+    end
+  end
+
+  def need_selenium reason=nil
+    Capybara.current_driver = :selenium
+    unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
+      @headless = HeadlessSingleton.get
+      @headless.start
+    end
+  end
+
+  def need_javascript reason=nil
+    unless Capybara.current_driver == :selenium
+      Capybara.current_driver = :poltergeist
+    end
+  end
+end
+
 class ActionDispatch::IntegrationTest
   # Make the Capybara DSL available in all integration tests
   include Capybara::DSL
   include ApiFixtureLoader
   include WaitForAjax
+  include HeadlessHelper
 
   @@API_AUTHS = self.api_fixture('api_client_authorizations')
 
@@ -81,24 +122,4 @@ class ActionDispatch::IntegrationTest
     end
     Capybara.reset_sessions!
   end
-
-  Capybara.default_driver = :rack_test
-
-  setup do
-    Capybara.use_default_driver
-  end
-
-  def need_selenium reason=nil
-    Capybara.current_driver = :selenium
-    unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
-      @headless = Headless.new(display: 101, reuse: true)
-      @headless.start
-    end
-  end
-
-  def need_javascript reason=nil
-    unless Capybara.current_driver == :selenium
-      Capybara.current_driver = :poltergeist
-    end
-  end
 end