如何使用capybara-webkit等待AJAX​​调用后元素出现? [英] How to wait till an element appears after an AJAX call using capybara-webkit?

查看:102
本文介绍了如何使用capybara-webkit等待AJAX​​调用后元素出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

capybara方法, wait_until 似乎不适用于capybara-webkit。

The capybara method, wait_until doesn't seems to work for capybara-webkit. Is there any alternate solution for that, or any Javascript implementations?

有意需要一些替代 sleep 的替代方案,例如睡眠2

Intentionally need some replacement for sleep, e.g. sleep 2.

推荐答案

如果您的AJAX调用导致DOM发生更改,则Capybara将请等一下

If your AJAX call results in a change to the DOM, Capybara will wait for it if you do

page.should have_selector?("some selector")

这是它有意等待的Capybara功能(最多 Capybara.default_wait_time have_selector?和相关方法为真。

It is an intentional Capybara feature that it waits (up to Capybara.default_wait_time) for have_selector? and related methods to be true.

如果您的AJAX调用未导致DOM更改,无法使用Capybara在浏览器端等待它。您也许能够检测到Java语言中的AJAX调用何时完成,并以某种方式将其传达给Capybara,但这将使您的测试和实现紧密结合。在这种情况下,一种常见的方法是等待AJAX​​调用对服务器端的影响(创建,更新或删除模型对象,发送电子邮件等)。由于Capybara无法看到服务器端,因此您必须等待服务器端更改自己。

If your AJAX call does not result in a change to the DOM, there's no way to wait for it on the browser side using Capybara. You might be able to detect when the AJAX call is complete in Javascript and somehow communicate that to Capybara, but that would couple your test and implementation rather tightly. A common approach in this case is to wait for the server-side effect of your AJAX call (creation or update or deletion of a model object, sending of an email, etc.) to take place. Since Capybara can't see the server side you have to wait for the server-side change yourself.

在Capybara 1中,您可以使用Capybara的 wait_until 等待服务器端更改。 wait_until 从Capybara 2中删除。我在wait_until 的实现。 .com / a / 23555725/634576>我对为什么添加 sleep 1在后钩中导致此Rspec / Capybara测试通过?

In Capybara 1 you can use Capybara's wait_until to wait for the server-side change. wait_until was removed from Capybara 2. I posted an implementation of wait_until in my answer to Why does adding "sleep 1" in an after hook cause this Rspec/Capybara test to pass?

这篇关于如何使用capybara-webkit等待AJAX​​调用后元素出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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