Last active
August 29, 2015 14:14
-
-
Save robhurring/da1ca83ad5fe65d5761d to your computer and use it in GitHub Desktop.
Capybara + Angular.js wait_for_ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Ohai</title> | |
<%= stylesheet_link_tag "application", media: "all" %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body class="container"> | |
<%= yield %> | |
<%= javascript_include_tag "request_tracking" if Rails.env.test? %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/feature_helpers.rb | |
module FeatureHelpers | |
DEFAULT_WAIT_TIME = 120 | |
Capybara.register_driver :poltergeist_debug do |app| | |
Capybara::Poltergeist::Driver.new(app, | |
inspector: true, | |
js_errors: true, | |
timeout: DEFAULT_WAIT_TIME | |
) | |
end | |
Capybara.default_wait_time = DEFAULT_WAIT_TIME | |
Capybara.javascript_driver = :poltergeist_debug | |
def wait_for_ajax(max_wait = Capybara.default_wait_time, check_pause = 0.1) | |
ticks = 0 | |
notify_after_ticks = 15 | |
Timeout.timeout(max_wait) do | |
until finished_all_ajax_requests? | |
ticks += 1 | |
puts '[waiting for ajax...]' if ticks > notify_after_ticks | |
sleep check_pause | |
end | |
end | |
end | |
def finished_all_ajax_requests? | |
page.evaluate_script('window.__activeRequests__').zero? | |
end | |
def save_and_open_page | |
filename = Rails.root.join('tmp', 'screenshots', "#{Time.now.to_i}.png") | |
page.save_screenshot(filename, full: true) | |
`open #{filename}` | |
end | |
end | |
RSpec.configure do |config| | |
config.include FeatureHelpers, type: :feature | |
# snapshot failures in dev | |
config.after(type: :feature) do |example| | |
save_and_open_page if example.exception | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/assets/javascripts/request_tracking.js | |
(function(module) { | |
module.config(['$httpProvider', | |
function($httpProvider) { | |
var key = '__activeRequests__'; | |
$httpProvider.interceptors.push(function($window) { | |
$window[key] = 0; | |
var startRequest = function() { | |
$window[key] += 1; | |
}; | |
var finishRequest = function() { | |
$window[key] -= 1; | |
if($window[key] < 0) { | |
$window[key] = 0; | |
} | |
} | |
return { | |
request: function(config) { | |
startRequest(); | |
return config; | |
}, | |
response: function(response) { | |
finishRequest(); | |
return response; | |
} | |
}; | |
}); | |
} | |
]); | |
})(angular.module('MyModule')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment