gitlab-org--gitlab-foss/spec/support/wait_for_requests.rb
Rémy Coutable abc5d1d0a5
Call Capybara.reset_sessions! before block_and_wait_for_requests_complete
capybara/rspec already calls Capybara.reset_sessions! in an
`after` hook, but `block_and_wait_for_requests_complete` is called
before it so by calling it explicitely here, we prevent any new
requests from being fired.
See
ffb41cfad6/lib/capybara/rspec.rb (L20-L25)

Signed-off-by: Rémy Coutable <remy@rymai.me>
2017-06-16 17:23:49 +02:00

55 lines
1.5 KiB
Ruby

require_relative './wait_for_requests'
module WaitForRequests
extend self
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
def block_and_wait_for_requests_complete
Gitlab::Testing::RequestBlockerMiddleware.block_requests!
wait_for('pending requests complete') do
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? && finished_all_requests?
end
ensure
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
end
def wait_for_requests
wait_for('JS requests') { finished_all_requests? }
end
private
def finished_all_requests?
return true unless javascript_test?
finished_all_ajax_requests? &&
finished_all_vue_resource_requests?
end
# Waits until the passed block returns true
def wait_for(condition_name, max_wait_time: Capybara.default_max_wait_time, polling_interval: 0.01)
wait_until = Time.now + max_wait_time.seconds
loop do
break if yield
if Time.now > wait_until
raise "Condition not met: #{condition_name}"
else
sleep(polling_interval)
end
end
end
def finished_all_vue_resource_requests?
Capybara.page.evaluate_script('window.activeVueResources || 0').zero?
end
def finished_all_ajax_requests?
return true if Capybara.page.evaluate_script('typeof jQuery === "undefined"')
Capybara.page.evaluate_script('jQuery.active').zero?
end
def javascript_test?
Capybara.current_driver == Capybara.javascript_driver
end
end