如何使Behat等待AJAX​​呼叫? [英] How to make Behat wait for an AJAX call?

查看:95
本文介绍了如何使Behat等待AJAX​​呼叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:修改并保存对广告系列的不完整更改

Scenario: Modify and save an incomplete change to a Campaign

Given I click on the Campaign section folder
And I press Save in the selected Campaign
Then I should see an error balloon informing the changes cannot be saved

重点是,最后一步中的错误提示框"是ajax调用,然后会根据操作成功而带来绿色或红色提示框.目前我要做的是 然后按保存...",我将进行一次睡眠(3),以使其有时间显示此气球.这似乎不是很聪明,因为您在浪费时间,还因为有时需要花费更多或更少的时间来处理此呼叫.

Point is that this 'error balloon' in the final step is a ajax call which will then bring a green or red balloon according to the success of the operation. Currently what I do is after 'And I press Save...' I will do a sleep(3) to give it time for this balloon to show up. This doesn't seem very smart coz you are wasting time and also because some times it can take more or less time for this call to be processed.

你们如何让Behat测试等待Ajax完成,而不只是让野兽入睡?

How do you guys make your behat tests wait for Ajax do be done instead of just putting the beasts to sleep?

非常感谢您的任何反馈!

thank you very much for any feedback!

推荐答案

这是通过等待未完成的ajax调用达到0来完成的.jQuery.active会为您进行检查.

This is done by waiting for your outstanding ajax calls to hit 0. jQuery.active will check just that for you.

在FeatureContext.php中,您可以执行类似操作;

In your FeatureContext.php, you can do something like;

public function iShouldSeeAnErrorBalloon($title)
{
    $time = 5000; // time should be in milliseconds
    $this->getSession()->wait($time, '(0 === jQuery.active)');
    // asserts below
}

并确保您使用运行javascript和ajax的Mink驱动程序(默认情况下不运行).

And do make sure you use a Mink Driver that runs javascript and ajax (the default does not).

这篇关于如何使Behat等待AJAX​​呼叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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