加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Capybara与has_no_css同步?

发布时间:2020-12-16 20:51:26 所属栏目:百科 来源:网络整理
导读:自升级到Capybara 2.4以来,我一直在遇到这个问题.以前,这个块运行良好: page.document.synchronize do page.should have_no_css('#ajax_indicator',:visible = true)end 这意味着在继续下一步之前强制等待ajax指示消失. 由于上面的内容返回了RSpec :: Expec
自升级到Capybara 2.4以来,我一直在遇到这个问题.以前,这个块运行良好:
page.document.synchronize do
  page.should have_no_css('#ajax_indicator',:visible => true)
end

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

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

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

Capybara::ElementNotFound
Capybara::ExpectationNotMet

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

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

解决方法

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

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

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

工作实例

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

<html>
  <head>
    <title>wait test</title>
    <script type="text/javascript" charset="utf-8">
      function setTimeoutDisplay(id,display,timeout) {
          setTimeout(function() {
              document.getElementById(id).style.display = display;
          },timeout);
      }
    </script>
  </head>

  <body>
    <div id="ajax_indicator" style="display:block;">indicator</div>
    <a id="hide_foo" href="#" onclick="setTimeoutDisplay('ajax_indicator','none',6000);">hide indicator</a>
  </body>
</html>

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

require 'capybara/rspec'

Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'file:///C:/test/wait.htm'

RSpec.configure do |config|
  config.expect_with :rspec do |c|
    c.syntax = [:should,:expect]
  end
end

RSpec.describe "#have_no_css",:js => true,:type => :feature do
  it 'raise exception when element does not disappear in time' do
    Capybara.default_wait_time = 2

    visit('')
    click_link('hide indicator')
    page.should have_no_css('#ajax_indicator',:visible => true)
  end

  it 'passes when element disappears in time' do
    Capybara.default_wait_time = 10

    visit('')
    click_link('hide indicator')
    page.should have_no_css('#ajax_indicator',:visible => true)
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读