Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add configursble delay before resynchronization looks for ajax requests

  • Loading branch information...
commit e7eb57a51c9655b8eb3465ecac17b364a0c27da1 1 parent d956e5e
Thomas Walpole authored
4  lib/capybara/selenium/driver.rb
@@ -4,9 +4,10 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
4 4
   DEFAULT_OPTIONS = {
5 5
     :resynchronize => true,
6 6
     :resynchronization_timeout => 10,
  7
+    :resynchronization_delay=>0,
7 8
     :browser => :firefox
8 9
   }
9  
-  SPECIAL_OPTIONS = [:browser, :resynchronize, :resynchronization_timeout]
  10
+  SPECIAL_OPTIONS = [:browser, :resynchronize, :resynchronization_timeout, :resynchronization_delay]
10 11
 
11 12
   attr_reader :app, :rack_server, :options
12 13
 
@@ -55,6 +56,7 @@ def resynchronize
55 56
     if options[:resynchronize]
56 57
       load_wait_for_ajax_support
57 58
       yield
  59
+      sleep(options[:resynchronization_delay])
58 60
       Capybara.timeout(options[:resynchronization_timeout], self, "failed to resynchronize, ajax request timed out") do
59 61
         evaluate_script("!window.capybaraRequestsOutstanding")
60 62
       end
16  lib/capybara/spec/driver.rb
@@ -166,6 +166,22 @@
166 166
 
167 167
       after { @driver.options[:resynchronization_timeout] = 10 }
168 168
     end
  169
+    
  170
+    context "with no synchronization delay" do
  171
+      it "should miss the start of the ajax request when do delay" do
  172
+        @driver.options[:resynchronization_delay]=0
  173
+        @driver.find('//input[@id="fire_delayed_ajax_request"]').first.click
  174
+        @driver.find('//p[@id="ajax_request_done"]').should be_empty
  175
+      end
  176
+      
  177
+      it "should wait for ajax requsts to finish when delay is long enough to see the start" do
  178
+        @driver.options[:resynchronization_delay]=0.5
  179
+        @driver.find('//input[@id="fire_delayed_ajax_request"]').first.click
  180
+        @driver.find('//p[@id="ajax_request_done"]').should_not be_empty
  181
+      end
  182
+      
  183
+      after { @driver.options[:resynchronization_delay]=0 }
  184
+    end
169 185
   end
170 186
 end
171 187
 
7  lib/capybara/spec/public/test.js
@@ -35,4 +35,11 @@ $(function() {
35 35
       $('body').append('<p id="ajax_request_done">Ajax request done</p>');
36 36
     }});
37 37
   });
  38
+  $('#fire_delayed_ajax_request').click(function() {
  39
+    setTimeout(function() {
  40
+      $.ajax({url: "/slow_response", context: document.body, success: function() {
  41
+        $('body').append('<p id="ajax_request_done">Ajax request done</p>');
  42
+      }});
  43
+    }, 400)
  44
+  });
38 45
 });
4  lib/capybara/spec/views/with_js.erb
@@ -38,6 +38,10 @@
38 38
     <p>
39 39
       <input type="submit" id="fire_ajax_request" value="Fire Ajax Request"/>
40 40
     </p>
  41
+
  42
+    <p>
  43
+      <input type="submit" id="fire_delayed_ajax_request" value="Fire Delayed Ajax Request"/>
  44
+    </p>
41 45
   </body>
42 46
 </html>
43 47
 

0 notes on commit e7eb57a

Please sign in to comment.
Something went wrong with that request. Please try again.