How to use synchronize in Capybara exactly?

丶灬走出姿态 提交于 2019-12-22 04:10:06

问题


If how to use wait_until is pretty clear (I've used the methods like this while creating tests through the native Webdriver methods), but not the new synchronize method (sorry:)). I've read the theme about why wait_until is deprecated, I've read the article about that, I've read the docs with method description and also read the code where the description present too. But I didn't find any example or tutorial how exactly to use this method.

Anybody, please, provide few cases where I (and maybe someone else) could see and learn how to use this method

For example the case

expect(actual).to equal(expected)

where should I "put" synchronize method to get negative exception only after timeout had been passed?

UPD: For those who interested please look into this links:

http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara

https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/base.rb#L44


回答1:


I just found a case where I needed to use #synchronize. I have a helper method which looks for an element on the page and if it exists it clicks on it and returns some text that gets updated on the page (via JavaScript). There are cases where between finding the element and clicking on it, it might disappear (and possibly re-appear) due to other JavaScript code and could raise a Selenium::WebDriver::Error::StaleElementReferenceError exception. So I use synchronize like this:

page.document.synchronize do
  element = first('#whatever')
  if element
    element.click
    find('#foo').text
  else
    nil
  end
end



回答2:


See this helper method that might help you. Found at https://gist.github.com/10c41024510ee9f235e0

# spec/support/capybara_helpers.rb

module CapybaraHelpers
  def wait_for_whizboo
    start = Time.now
    while true
      break if [check for whizboo here, e.g. with page.evaluate_script]
      if Time.now > start + 5.seconds
        fail "Whizboo didn't happen."
      end
      sleep 0.1
    end
  end
end

RSpec.configure do |config|
  config.include CapybaraHelpers, type: :request
end


来源:https://stackoverflow.com/questions/14588241/how-to-use-synchronize-in-capybara-exactly

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