【发布时间】:2015-02-11 20:42:07
【问题描述】:
我正在使用 Capybara 和 Poltergeist,我这辈子都无法让我的所有测试始终通过。我有这个问题,特别是日期选择器。它应该非常简单 - 用户点击输入,输出选择月份(第一张图片)。然后单击一个月,然后出现一个日期选择(第二张图像),在该日期上选择了该月的某一天。
现在,我的代码如下所示:
all(:css, 'input.from_date').last.click
expect(page).to have_css(".datepicker-months")
within(:css, '.datepicker-months') { find('.month', :text => 'Jun', match: :first).click }
expect(page).not_to have_css(".datepicker-months")
expect(page).to have_css(".datepicker-days")
within(:css, '.datepicker-days') { find('.day', :text => work[:start_date].stamp('31').to_i.to_s, match: :first).click } #.to_i.to_s used to remove leading zeros
page.assert_no_selector('.datepicker-days')
有时它会过去,但大多数时候它会说:
expected to find css ".datepicker-days" but there were no matches
或
expected not to find css ".datepicker-months", found 1 match: "« 2015 » JanFebMarAprMayJunJulAugSepOctNovDec"
如果我尝试通过调用 binding.pry 来调试它,我可以在控制台中逐步运行这些命令,并且它运行良好。我的超时设置为绰绰有余(我认为)。知道为什么此测试会间歇性失败吗?
我的配置:
Capybara.javascript_driver = :poltergeist
Capybara.default_wait_time = 60
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {
timeout: 60,
js_errors: false,
phantomjs_logger: File.open("log/phantomjs.log", "a")})
end
更新:
当在进程的每一步之后添加 sleep(0.5) 时,它每次都通过。这是不好的做法,我应该能够在不这样做的情况下编写测试。 :/
【问题讨论】:
-
您最终有没有找到更好的方法来解决这个问题?我尝试使用
page.execute_script触发自定义 jQuery 事件来监听,但仍然不是很一致。 -
不是真的没有。如果库中有 JS 方法来触发事件,我就使用它。想法是该功能已经在库中进行了很好的测试,因此不必再次测试。
-
我明白了。感谢回复
标签: ruby-on-rails rspec capybara poltergeist