def wait_for_ajax(timeout=5, increment=0.5) #Timeout in seconds
# this is not exactly precise in terms of time elapsed but good enough
timespan = 0
loop do
break if page.execute_script "return jQuery.active == 0"
sleep increment
timespan += increment
if timespan > timeout
flunk "Wait for AJAX timed out after waiting for #{timeout} seconds"
end
end
end