Waiting for redirect in Capybara Rails

I am doing some integration tests on my website which is built into Spree. I am using RSpec + Capybara to run these tests.

I found that in newer versions of Capybara, wait_until has been removed from the source because it no longer has to wait for an AJAX request this way. Anyway, now Capybara is smart enough to wait for AJAX requests.

What I am doing is pretty simple:

visit product_path(product)
  within '.vip-buy-content' do
    first('.buy-button').click
  end
expect(page.current_path).to eq(cart_path)

      

When the button is clicked, an AJAX request is made. When the AJAX request is made, a redirect occurs.

However, the wait fails because it seems like Capybara will handle this faster than the AJAX request and redirect.

If you add "sleep 10" higher than expected, the spec will pass because it has enough time to process it.

I wish I liked the redirection. Any ideas?

+3


source to share


1 answer


I don't know if this will solve the problem, but I usually add the wait_for_ajax.rb file to my support folder and create a module like this.

module WaitForAjax
  def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end
end

RSpec.configure do |config|
  config.include WaitForAjax, type: :feature
end

      

And then add



visit product_path(product)
  within '.vip-buy-content' do
    first('.buy-button').click
  end
  wait_for_ajax
expect(page.current_path).to eq(cart_path)

      

I assume that you are using: js => true for the described blocks. Hope this helps!

0


source







All Articles