Skip to content

Commit

Permalink
Add configursble delay before resynchronization looks for ajax requests
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Jun 20, 2011
1 parent d956e5e commit e7eb57a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/capybara/selenium/driver.rb
Expand Up @@ -4,9 +4,10 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
DEFAULT_OPTIONS = { DEFAULT_OPTIONS = {
:resynchronize => true, :resynchronize => true,
:resynchronization_timeout => 10, :resynchronization_timeout => 10,
:resynchronization_delay=>0,
:browser => :firefox :browser => :firefox
} }
SPECIAL_OPTIONS = [:browser, :resynchronize, :resynchronization_timeout] SPECIAL_OPTIONS = [:browser, :resynchronize, :resynchronization_timeout, :resynchronization_delay]


attr_reader :app, :rack_server, :options attr_reader :app, :rack_server, :options


Expand Down Expand Up @@ -55,6 +56,7 @@ def resynchronize
if options[:resynchronize] if options[:resynchronize]
load_wait_for_ajax_support load_wait_for_ajax_support
yield yield
sleep(options[:resynchronization_delay])
Capybara.timeout(options[:resynchronization_timeout], self, "failed to resynchronize, ajax request timed out") do Capybara.timeout(options[:resynchronization_timeout], self, "failed to resynchronize, ajax request timed out") do
evaluate_script("!window.capybaraRequestsOutstanding") evaluate_script("!window.capybaraRequestsOutstanding")
end end
Expand Down
16 changes: 16 additions & 0 deletions lib/capybara/spec/driver.rb
Expand Up @@ -166,6 +166,22 @@


after { @driver.options[:resynchronization_timeout] = 10 } after { @driver.options[:resynchronization_timeout] = 10 }
end end

context "with no synchronization delay" do
it "should miss the start of the ajax request when do delay" do
@driver.options[:resynchronization_delay]=0
@driver.find('//input[@id="fire_delayed_ajax_request"]').first.click
@driver.find('//p[@id="ajax_request_done"]').should be_empty
end

it "should wait for ajax requsts to finish when delay is long enough to see the start" do
@driver.options[:resynchronization_delay]=0.5
@driver.find('//input[@id="fire_delayed_ajax_request"]').first.click
@driver.find('//p[@id="ajax_request_done"]').should_not be_empty
end

after { @driver.options[:resynchronization_delay]=0 }
end
end end
end end


Expand Down
7 changes: 7 additions & 0 deletions lib/capybara/spec/public/test.js
Expand Up @@ -35,4 +35,11 @@ $(function() {
$('body').append('<p id="ajax_request_done">Ajax request done</p>'); $('body').append('<p id="ajax_request_done">Ajax request done</p>');
}}); }});
}); });
$('#fire_delayed_ajax_request').click(function() {
setTimeout(function() {
$.ajax({url: "/slow_response", context: document.body, success: function() {
$('body').append('<p id="ajax_request_done">Ajax request done</p>');
}});
}, 400)
});
}); });
4 changes: 4 additions & 0 deletions lib/capybara/spec/views/with_js.erb
Expand Up @@ -38,6 +38,10 @@
<p> <p>
<input type="submit" id="fire_ajax_request" value="Fire Ajax Request"/> <input type="submit" id="fire_ajax_request" value="Fire Ajax Request"/>
</p> </p>

<p>
<input type="submit" id="fire_delayed_ajax_request" value="Fire Delayed Ajax Request"/>
</p>
</body> </body>
</html> </html>


0 comments on commit e7eb57a

Please sign in to comment.