如何在Capybara中准确使用同步? [英] How to use synchronize in Capybara exactly?

查看:69
本文介绍了如何在Capybara中准确使用同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果已经很清楚如何使用wait_until(我在通过本地Webdriver方法创建测试时使用了类似的方法),但没有使用新的syncize方法(抱歉:)。我已经阅读了有关为什么不赞成使用wait_until的主题,已经阅读了有关该主题的文章,阅读了带有方法说明的文档,还阅读了该说明所在的代码。但是我没有找到任何示例或教程来确切地说明如何使用此方法。

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

例如,情况

expect(actual).to equal(expected)

我应该在哪里同步同步方法以仅在经过超时后获得否定异常?

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

UPD:对于那些感兴趣的人,请查看以下链接:

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

推荐答案

我只是发现需要使用#synchronize的情况。我有一个帮助程序方法,该方法在页面上查找元素,如果存在该元素,则单击该元素,并返回一些在页面上通过JavaScript更新的文本。在某些情况下,找到该元素然后单击它之间,由于其他JavaScript代码,它可能会消失(并可能重新出现),并可能引发Selenium :: WebDriver :: Error :: StaleElementReferenceError异常。所以我这样使用sync:

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

这篇关于如何在Capybara中准确使用同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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