2016-09-29 115 views
1

我一直在尝试寻找替代使用睡眠一段时间,似乎每一个建议的方法,以避免使用睡眠从来没有工作,我在我的绳索结束。水豚测试 - 替代睡眠

it 'finds the correct product when searching by job type', js: true do 
    fill_in('filterrific_for_work_type', with: 'Central') 
    # once again, sleep feels like the only thing that works 
    #sleep 1 # TODO: find a better way.. 
    expect(find('#work_queue_items_filter_reset')).to have_content('Reset All Filters') 
    expect(page).to have_link('IP Central Report', 
           href: work_queue_item_path(@release.id)) 
    end 

我也有这个等待AJAX​​辅助:

module CapybaraHelpers 
    def wait_for_ajax 
    Timeout.timeout(Capybara.default_max_wait_time) do 
     loop until finished_all_ajax_requests? 
    end 
    end 

    def finished_all_ajax_requests? 
    page.evaluate_script('jQuery.active').zero? 
    end 
end 

而且在spec_helper.rb:

RSpec.configure do |config| 
    config.include CapybaraHelpers, type: :feature 
end 

这个测试休息,除非我补充睡眠1.我已经尝试了很多迭代,使用“内部”,“查找”,“有效内容”等。这真的让我发疯。

回答

3

只要在页面上有可见的更改,wait_for_ajax应该几乎不需要。你不说你得到什么错误,但我会写你期望的方式将是

expect(page).to have_css('#work_queue_items_filter_reset', text: 'Reset All Filters') 

如果不工作,那么它可能仅仅是因为你有Capybara.default_max_wait_time设置太短您正在测试的硬件/应用程序。您可以测试在您的期望指定自定义等待时间,看它是否有差别

expect(page).to have_css('#work_queue_items_filter_reset', text: 'Reset All Filters', wait: 10) # wait up to 10 seconds for matching element to appear 
+0

我不明白 - 我有Capybara.default_max_wait_time = 10。我尝试了你提出的那两个have_css选项,但它失败了。当我加入睡眠1时,测试通过! WTF? – user1200990

+1

@ user1200990你在使用什么驱动程序?你究竟得到什么错误?你的页面如何工作?即 - 在填充字段时触发页面执行任何操作。 –

+0

@ user1200990另外你在运行测试的ruby是什么? –

0

你永远不应该使用“睡眠”与水豚。方法如 https://github.com/ordinaryzelig/capybara_minitest_spec

他们在里面使用睡眠。

+0

我知道我不应该使用睡眠。我为这个测试套件使用rspec和Capybara,我觉得我已经用尽了所有不使用睡眠技术,而且我似乎还没有得到任何东西,只有睡觉才能工作。 – user1200990

+0

此测试正在测试数据库关联集合的正确过滤。奇怪的是,在睡眠状态下,可以显示集合,并且各行可以显示其正确的db关联。例如,在视图中的这一行:[code] <%= link_to(wqi.workable。getType,work_queue_item_path(wqi))%> [/ code],wqi.workable不是零。在没有睡眠的情况下,wqi.workable是零的,所以它是水豚对元素进行测试和完全有时间处理它的依赖关系的集合之间的竞争条件。 – user1200990