2017-05-17 14:25:13 -04:00
|
|
|
require_relative './wait_for_requests'
|
2017-04-13 12:31:33 -04:00
|
|
|
|
2017-03-09 21:28:20 -05:00
|
|
|
module WaitForRequests
|
|
|
|
extend self
|
|
|
|
|
|
|
|
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
|
2017-05-17 14:25:13 -04:00
|
|
|
def block_and_wait_for_requests_complete
|
2017-03-09 21:28:20 -05:00
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.block_requests!
|
2017-05-17 14:25:13 -04:00
|
|
|
wait_for('pending requests complete') do
|
2017-06-15 19:46:18 -04:00
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? && finished_all_requests?
|
2017-03-09 21:28:20 -05:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
|
|
|
|
end
|
|
|
|
|
2017-05-17 14:25:13 -04:00
|
|
|
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
|
|
|
|
|
2017-03-09 21:28:20 -05:00
|
|
|
# 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
|
2017-05-17 14:25:13 -04:00
|
|
|
|
|
|
|
def finished_all_vue_resource_requests?
|
2017-06-15 22:54:09 -04:00
|
|
|
Capybara.page.evaluate_script('window.activeVueResources || 0').zero?
|
2017-05-17 14:25:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def finished_all_ajax_requests?
|
2017-06-15 22:54:09 -04:00
|
|
|
return true if Capybara.page.evaluate_script('typeof jQuery === "undefined"')
|
2017-05-17 14:25:13 -04:00
|
|
|
|
2017-06-15 22:54:09 -04:00
|
|
|
Capybara.page.evaluate_script('jQuery.active').zero?
|
2017-05-17 14:25:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def javascript_test?
|
|
|
|
Capybara.current_driver == Capybara.javascript_driver
|
|
|
|
end
|
2017-03-09 21:28:20 -05:00
|
|
|
end
|