How to get poltergeist/PhantomJS to delay returning the page to Capybara until AJAX is loaded?

て烟熏妆下的殇ゞ 提交于 2020-01-04 15:27:54

问题


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-->

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.


回答1:


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!")

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

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.)

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.




回答2:


Not sure what you are actually waiting for css wise on the loaded page, but the way I would do it is:

Capybara.using_wait_time(x) { page.find('css_here') }

where x is the number of seconds you want capybara to allow before erroring if it cannot find the css.




回答3:


expect(page).to have_content "Page loaded!", wait: x


来源:https://stackoverflow.com/questions/36108196/how-to-get-poltergeist-phantomjs-to-delay-returning-the-page-to-capybara-until-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!