ruby – Capybara与has_no_css同步?

前端之家收集整理的这篇文章主要介绍了ruby – Capybara与has_no_css同步?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
升级到Capybara 2.4以来,我一直在遇到这个问题.以前,这个块运行良好:
  1. page.document.synchronize do
  2. page.should have_no_css('#ajax_indicator',:visible => true)
  3. end

这意味着在继续下一步之前强制等待ajax指示消失.

由于上面的内容返回了RSpec :: Expectations :: ExpectationNotMetError,因此同步不会重新运行该块,而只是抛出错误.不知道为什么这个在我之前使用的版本中工作(我相信2.1).

同步块仅重新运行返回以下内容的块:

  1. Capybara::ElementNotFound
  2. Capybara::ExpectationNotMet

无论某个驱动程序添加到该列表中.

有关更全面的解释和不使用同步的示例,请参阅Justin的回复,或查看我对直接解决方案的回复.

解决方法

has_no_css匹配器已经等待元素消失.问题似乎是在同步块中使用它. synchronize方法仅针对某些异常重新运行,这些异常不包括RSpec :: Expectations :: ExpectationNotMetError.

删除同步似乎做你想要的 – 即强制等待直到元素消失.换句话说,就是:

  1. page.should have_no_css('#ajax_indicator',:visible => true)

工作实例

这是一个页面,比如“wait.htm”,我认为它会重现你的问题.它有一个链接,当点击时,等待6秒,然后隐藏指标元素.

  1. <html>
  2. <head>
  3. <title>wait test</title>
  4. <script type="text/javascript" charset="utf-8">
  5. function setTimeoutDisplay(id,display,timeout) {
  6. setTimeout(function() {
  7. document.getElementById(id).style.display = display;
  8. },timeout);
  9. }
  10. </script>
  11. </head>
  12.  
  13. <body>
  14. <div id="ajax_indicator" style="display:block;">indicator</div>
  15. <a id="hide_foo" href="#" onclick="setTimeoutDisplay('ajax_indicator','none',6000);">hide indicator</a>
  16. </body>
  17. </html>

以下规范显示,通过使用page.should have_no_css而无需手动调用synchronize,Capybara已经迫使等待.等待仅2秒时,规范失败,因为元素不会消失.当等待10秒时,规范通过,因为元素有时间消失.

  1. require 'capybara/rspec'
  2.  
  3. Capybara.run_server = false
  4. Capybara.current_driver = :selenium
  5. Capybara.app_host = 'file:///C:/test/wait.htm'
  6.  
  7. RSpec.configure do |config|
  8. config.expect_with :rspec do |c|
  9. c.Syntax = [:should,:expect]
  10. end
  11. end
  12.  
  13. RSpec.describe "#have_no_css",:js => true,:type => :feature do
  14. it 'raise exception when element does not disappear in time' do
  15. Capybara.default_wait_time = 2
  16.  
  17. visit('')
  18. click_link('hide indicator')
  19. page.should have_no_css('#ajax_indicator',:visible => true)
  20. end
  21.  
  22. it 'passes when element disappears in time' do
  23. Capybara.default_wait_time = 10
  24.  
  25. visit('')
  26. click_link('hide indicator')
  27. page.should have_no_css('#ajax_indicator',:visible => true)
  28. end
  29. end

猜你在找的Ruby相关文章