Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use synchronize in Capybara exactly?

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

like image 426
Dmitriy Konovalov Avatar asked Jan 29 '13 17:01

Dmitriy Konovalov


1 Answers

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
like image 142
Liron Yahdav Avatar answered Oct 11 '22 13:10

Liron Yahdav