黄瓜:等待ajax:成功

2024-04-26

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

...
When I follow "Remove from cart"
Then I should see "Test Product removed from cart"

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

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

该函数在浏览器中运行良好,但在 Cucumber 中找不到“测试产品已从购物车中删除”文本。我猜这是因为 Cucumber 在 AJAX 返回文本之前搜索文本?

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


为了补充 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

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

AfterStep('@javascript') do
  begin
    When 'I wait for the ajax request to finish'
  rescue
  end
end

我在自动同步方面遇到了问题,这解决了问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

黄瓜:等待ajax:成功 的相关文章

随机推荐