Fix and simplify wait_for_ajax
authorTom Morris <tfmorris@curoverse.com>
Thu, 29 Sep 2016 16:31:48 +0000 (12:31 -0400)
committerTom Morris <tfmorris@curoverse.com>
Thu, 29 Sep 2016 16:31:48 +0000 (12:31 -0400)
Don't change global Capybara.default_max_wait_time
Sleep so we're not competing with thread(s) doing work

apps/workbench/test/integration_helper.rb

index f0c811aac20d5fb28720783ee97adc7428624fbd..d5be993a4fa76e450cb3466d0a83d218056287da 100644 (file)
@@ -71,16 +71,16 @@ Capybara.register_driver :selenium_with_download do |app|
 end
 
 module WaitForAjax
-  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