Poltergeist / Capybara测试无法间歇性地找到CSS

时间:2015-02-11 20:42:07

标签: ruby-on-rails rspec capybara poltergeist

我使用Capybara和Poltergeist并且在我的生命中不能让我的所有测试始终如一地通过。我有一个问题,尤其是日期选择器。它应该非常简单 - 用户点击输入,输出选择的月份(第一张图片)。然后单击一个月,然后出现一天选择(第二张图像),选择该月的某一天。

first image second image

现在,我的代码如下所示:

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

更新

在过程中的每个步骤之后添加睡眠(0.5)时,它每次都会传递。这是不好的做法,我应该能够在不这样做的情况下编写测试。 :/

1 个答案:

答案 0 :(得分:0)

每当我遇到这些不一致的情况时,我会尝试找到其他要断言的东西,这样你就不必依赖睡眠了。

如果有任何类型的动画启动/停止,您可以尝试对其进行断言。

您也可以尝试更具体地了解当前的期望,因为您似乎没有点击找到的对象时遇到任何问题

expect(page).not_to have_css('.datepicker-months .month', :text => 'Jun')

如果其中任何一个让你更进一步,请告诉我们。