黄瓜:等待ajax:成功 [英] Cucumber: Wait for ajax:success

查看:123
本文介绍了黄瓜:等待ajax:成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following typical cucumber steps in a Rails 3.1 project:

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

困难在于从购物车中移除按钮是一个ajax:远程调用,它通过以下方式返回Test Shopping removed from cart到#cart_notice元素:

The difficulty is that "Remove from cart" button is an ajax :remote call, which returns "Test Product removed from cart" to the #cart_notice element via:

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

此功能在浏览器中正常工作,但找不到在黄瓜的文本。我猜这是因为Cucumber在AJAX返回之前搜索文本?

The function works fine in the browser, but doesn't find the "Test Product removed from cart" text in cucumber. I'm guessing this is because Cucumber is searching for the text before the AJAX returns it?

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

So, in short...how do I ensure cucumber waits for the ajax to return a result before searching for the desired content?

推荐答案

要添加到dexter说,它在浏览器中执行JS,等待ajax请求完成。使用jQuery,我使用这个步骤:

To add to what dexter said, you may want to write a step that executes JS in the browser which waits for ajax requests to finish. With jQuery, I use this step:

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步骤之后包含步骤:

You can then include the step as needed, or after every javascript step:

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

我遇到自动同步的问题,并将其清除。

I was having issues with the automatic synchronization, and this cleared it up.

这篇关于黄瓜:等待ajax:成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆