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-11-02 16:34:30 -04:00
|
|
|
block_requests { wait_for_all_requests }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Block all requests inside block with 503 response
|
|
|
|
def block_requests
|
2017-03-09 21:28:20 -05:00
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.block_requests!
|
2017-11-02 16:34:30 -04:00
|
|
|
yield
|
2017-03-09 21:28:20 -05:00
|
|
|
ensure
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
|
|
|
|
end
|
|
|
|
|
2017-11-02 16:34:30 -04:00
|
|
|
# Slow down requests inside block by injecting `sleep 0.2` before each response
|
|
|
|
def slow_requests
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.slow_requests!
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for client-side AJAX requests
|
2017-05-17 14:25:13 -04:00
|
|
|
def wait_for_requests
|
2018-07-05 12:49:18 -04:00
|
|
|
wait_for('JS requests complete', max_wait_time: 2 * Capybara.default_max_wait_time) do
|
|
|
|
finished_all_js_requests?
|
|
|
|
end
|
2017-11-02 16:34:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for active Rack requests and client-side AJAX requests
|
|
|
|
def wait_for_all_requests
|
|
|
|
wait_for('pending requests complete') do
|
|
|
|
finished_all_rack_reqiests? &&
|
|
|
|
finished_all_js_requests?
|
|
|
|
end
|
2017-05-17 14:25:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-02 16:34:30 -04:00
|
|
|
def finished_all_rack_reqiests?
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero?
|
|
|
|
end
|
|
|
|
|
|
|
|
def finished_all_js_requests?
|
2017-05-17 14:25:13 -04:00
|
|
|
return true unless javascript_test?
|
|
|
|
|
|
|
|
finished_all_ajax_requests? &&
|
|
|
|
finished_all_vue_resource_requests?
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|