如何让poltergeist/PhantomJS延迟将页面返回到Capybara,直到加载AJAX? [英] How to get poltergeist/PhantomJS to delay returning the page to Capybara until AJAX is loaded?

查看:170
本文介绍了如何让poltergeist/PhantomJS延迟将页面返回到Capybara,直到加载AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止PhantomJS将页面返回Capybara几秒钟,以允许AJAX完成加载?我返回的HTML正文是:

How do I keep PhantomJS from returning the page to Capybara for several seconds to allow AJAX to finish loading? The HTML body I am getting back is:

<!--div class='loading'>PLEASE WAIT WHILE PAGE LOADS</div-->

这是在告诉我PhantomJS在页面完成加载其AJAX资产之前返回了该页面.有什么办法可以减慢速度吗?我尝试使用rasterize.js解决方案,但看起来好像我添加到poltergeist的"phantomjs_options"哈希中的任何脚本都被忽略了.

Which is telling me that PhantomJS returned the page before the page finished loading its AJAX assets. Is there any way I can slow this down? I've tried using the rasterize.js solution but it looks as though any script I add to poltergeist's "phantomjs_options" hash is overlooked.

推荐答案

使用等待您的Capybara方法,断言页面上的内容仅在AJAX完成后才应存在于页面上.例如,如果页面上应显示页面已加载!"仅在AJAX完成之后,并假设您正在使用RSpec,请执行以下操作:

Using a Capybara method that waits for you, assert that content is on the page that should exist on the page only after AJAX has completed. For example, if the page should say "Page loaded!" only after AJAX has completed, and assuming you're using RSpec, do this:

expect(page).to have_content("Page loaded!")

然后,您可以在页面上安全地测试取决于AJAX是否已完成的其他事情.

You can then safely test other things on the page that depend on AJAX having completed.

水豚方法包括findfind_*方法,withinhas_content?/has_no_content?has_css?/has_no_css?has_selector?/has_no_selector?click_*fill_incheckuncheckselectchoose.一些Capybara方法不要等待,包括current_pathvisitexecute_scriptevaluate_script和节点访问器,例如textvalue. allfirst如果为它们提供选项,可以为它们提供特定的等待内容(:count:minimum:maximum:between),否则它们不会.这是一个雷区. (我的清单主要来自此博客文章.)

Capybara methods that wait for specified content to appear include find and find_* methods, within, has_content?/has_no_content?, has_css?/has_no_css?, has_selector?/has_no_selector?, click_*, fill_in, check, uncheck, select and choose. Some Capybara methods don't wait, including current_path, visit, execute_script, evaluate_script and node accessors like text and value. all and first wait if you give them options that give them something specific to wait for (:count, :minimum, :maximum, :between), otherwise they don't. It's quite a minefield. (I took the lists mostly from this blog post.)

还有其他等待方式(sleep(不好),轮询数据库,直到出现由AJAX引起的更改,等等),但是使用Capybara方法等待您是通常的推荐方法.

There are other ways to wait (sleep (bad), polling the database until a change caused by AJAX appears, etc.) but using Capybara methods that wait for you is the usual and recommended way.

这篇关于如何让poltergeist/PhantomJS延迟将页面返回到Capybara,直到加载AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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