简体   繁体   English

在Capybara Rails中等待重定向

[英]Waiting for redirect in Capybara Rails

I am doing some integration tests on my website that is builtin Spree. 我正在我的网站上进行一些集成测试,这是在Spree内置的。 I am using RSpec + Capybara in order to do these tests. 我正在使用RSpec + Capybara进行这些测试。

I found that in the newer versions of Capybara, wait_until was removed from the source code because it is not necessary anymore to wait for an AJAX Request this way. 我发现在较新版本的Capybara中,wait_until已从源代码中删除,因为不再需要以这种方式等待AJAX​​请求。 Somehow, now Capybara is smart enough to wait for the AJAX Requests. 不知何故,现在Capybara足够聪明,等待AJAX​​请求。

What I am doing is quite 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 done. 单击该按钮时,将完成AJAX请求。 When the AJAX request is done, a redirect happens. 完成AJAX请求后,会发生重定向。

However, the expectation fails because it seems that Capybara will process this faster than the AJAX request and the redirect. 然而,期望失败,因为似乎Capybara将比AJAX请求和重定向更快地处理它。

If add a "sleep 10" above the expect, the spec will pass because it has enough time to process this. 如果在期望值之上添加“sleep 10”,则规范将通过,因为它有足够的时间来处理它。

I would like a nicer way to wait for the redirect. 我想要一个更好的方法来等待重定向。 Any ideas?. 有任何想法吗?。

I don't know if this will solve this problem, but I usually add a wait_for_ajax.rb file in my supports folder, and create a module such as this. 我不知道这是否会解决这个问题,但我通常会在support文件夹中添加一个wait_for_ajax.rb文件,并创建一个这样的模块。

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)

My assumption you are using :js=>true on your describe blocks. 我在假设你正在使用:js => true在你的描述块上。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM