黄瓜:等待ajax:成功

Pla*_*Ton 17 ruby-on-rails cucumber

我在Rails 3.1项目中有以下典型的黄瓜步骤:

...
When I follow "Remove from cart"
Then I should see "Test Product removed from cart"
Run Code Online (Sandbox Code Playgroud)

困难在于"从购物车中移除"按钮是一个ajax:远程调用,它通过以下方式将"测试产品从购物车中删除"返回到#cart_notice元素:

$('#cart_notice').append("<%= @product.name %> removed from cart");
Run Code Online (Sandbox Code Playgroud)

该功能在浏览器中工作正常,但没有在黄瓜中找到"从购物车中删除测试产品"文本.我猜这是因为Cucumber在AJAX返回之前正在搜索文本?

所以,简而言之...如何确保黄瓜在搜索所需内容之前等待ajax返回结果?

jcn*_*ghm 16

要添加到dexter所说的内容,您可能需要编写一个在浏览器中执行JS的步骤,该步骤等待ajax请求完成.使用jQuery,我使用这一步:

When /^I wait for the ajax request to finish$/ do
  start_time = Time.now
  page.evaluate_script('jQuery.isReady&&jQuery.active==0').class.should_not eql(String) until page.evaluate_script('jQuery.isReady&&jQuery.active==0') or (start_time + 5.seconds) < Time.now do
    sleep 1
  end
end
Run Code Online (Sandbox Code Playgroud)

然后,您可以根据需要或在每个javascript步骤之后包含该步骤:

AfterStep('@javascript') do
  begin
    When 'I wait for the ajax request to finish'
  rescue
  end
end
Run Code Online (Sandbox Code Playgroud)

我遇到了自动同步的问题,这清除了它.