Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / test / integration_helper.rb
index f0c811aac20d5fb28720783ee97adc7428624fbd..5d2cefe447c4fb84e29eae4dc93bea6fd3748d90 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 require 'test_helper'
 require 'capybara/rails'
 require 'capybara/poltergeist'
@@ -71,16 +75,24 @@ Capybara.register_driver :selenium_with_download do |app|
 end
 
 module WaitForAjax
+  # FIXME: Huge side effect here
+  # The following line changes the global default Capybara wait time, affecting
+  # every test which follows this one. This should be removed and the failing tests
+  # should have their individual wait times increased, if appropriate, using
+  # the using_wait_time(N) construct to temporarily change the wait time.
+  # Note: the below is especially bad because there are places that increase wait
+  # times using a multiplier e.g. using_wait_time(3 * Capybara.default_max_wait_time)
   Capybara.default_max_wait_time = 10
   def wait_for_ajax
-    Timeout.timeout(Capybara.default_max_wait_time) do
-      loop until finished_all_ajax_requests?
+    timeout = 10
+    count = 0
+    while page.evaluate_script("jQuery.active").to_i > 0
+      count += 1
+      raise "AJAX request took more than #{timeout} seconds" if count > timeout * 10
+      sleep(0.1)
     end
   end
 
-  def finished_all_ajax_requests?
-    page.evaluate_script('jQuery.active').zero?
-  end
 end
 
 module AssertDomEvent
@@ -213,4 +225,19 @@ class ActionDispatch::IntegrationTest
     end
     Capybara.reset_sessions!
   end
+
+  def accept_alert
+    if Capybara.current_driver == :selenium
+      (0..9).each do
+        begin
+          page.driver.browser.switch_to.alert.accept
+          break
+        rescue Selenium::WebDriver::Error::NoSuchAlertError
+         sleep 0.1
+        end
+      end
+    else
+      # poltergeist returns true for confirm, so no need to accept
+    end
+  end
 end