2017-08-17 22:00:37 +05:30
|
|
|
module WaitForRequests
|
|
|
|
extend self
|
|
|
|
|
|
|
|
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
|
2017-09-10 17:25:29 +05:30
|
|
|
def block_and_wait_for_requests_complete
|
2018-03-17 18:26:18 +05:30
|
|
|
block_requests { wait_for_all_requests }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Block all requests inside block with 503 response
|
|
|
|
def block_requests
|
2017-08-17 22:00:37 +05:30
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.block_requests!
|
2018-03-17 18:26:18 +05:30
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Slow down requests inside block by injecting `sleep 0.2` before each response
|
|
|
|
def slow_requests
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.slow_requests!
|
|
|
|
yield
|
2017-08-17 22:00:37 +05:30
|
|
|
ensure
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# Wait for client-side AJAX requests
|
2017-09-10 17:25:29 +05:30
|
|
|
def wait_for_requests
|
2018-03-17 18:26:18 +05:30
|
|
|
wait_for('JS requests complete') { finished_all_js_requests? }
|
|
|
|
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-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def finished_all_rack_reqiests?
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero?
|
|
|
|
end
|
|
|
|
|
|
|
|
def finished_all_js_requests?
|
2017-09-10 17:25:29 +05:30
|
|
|
return true unless javascript_test?
|
|
|
|
|
|
|
|
finished_all_ajax_requests? &&
|
|
|
|
finished_all_vue_resource_requests?
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# 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
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if Time.now > wait_until
|
|
|
|
raise "Condition not met: #{condition_name}"
|
|
|
|
else
|
|
|
|
sleep(polling_interval)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|