Rails + Cucumber: How to wait for javascript to finish before moving on to next step? -
Rails + Cucumber: How to wait for javascript to finish before moving on to next step? -
i have form uses jquery , ajax. 1 part of form dependent on jquery load inputs form, however, tests fail because fast don't wait javascript finish. there way wrap function wait javascript response before continuing? right utilize sleep 1:
when(/^i add together product collection$/) select(product.first.name, from: "unassociated_product_ids[]") click_link(">") #<-- uses ajax sleep 1 end
you shouldn't seek explicitly wait ajax or javascript finish - instead, should seek wait page change.
imagine js/ajax runs clicking link causes product's name listed in ul/li element on page. there no li text of product name, afterwards there be. step should wait that:
... click_link(">") page.should have_css("li", :text => product.first.name) ... that wait until there li element on page product's name. how long wait? capybara has default wait time, capybara.default_wait_time. can set default in env.rb capybara.default_wait_time = 5 or something. , of course of study can alter temporarily in step if know need more time.
default_wait_time = capybara.default_wait_time capybara.default_wait_time = 10 # long request # stuff capybara.default_wait_time = default_wait_time or utilize capybara.using_wait_time simplify that.
capybara.using_wait_time(10) # stuff end sometimes though might have wait ajax , might hard or impossible wait page different, , might outside of control. in case, can create custom method wait ajax , phone call step needs it.
def wait_for_ajax # see: https://gist.github.com/10c41024510ee9f235e0 # linked from: http://techblog.fundinggates.com/blog/2012/08/capybara-2-0-upgrade-guide/ start = time.now while true break if (page.evaluate_script("$.active") == 0) if time.now > start + capybara.default_wait_time.seconds fail "ajax did not register finish after #{capybara.default_wait_time} seconds!" end sleep 0.1 end end ruby-on-rails
Comments
Post a Comment