切换 Bootstrap 可折叠在 Rails/Capybara 功能测试中失败

Toggling a Bootstrap collapsible is failing in Rails/Capybara feature tests

我是 Capybara 和功能测试的新手。我一直在尝试在 Rails 应用程序上测试一个小功能,该功能可以将 post 上的评论切换到视图和视图外。第一个将评论切换到视图的测试通过了,但是第二个将它们切换到视图之外的测试没有通过。 (我正在使用 headless-chrome webdriver)。

context 'viewing comments', js: true do
  scenario 'toggling comments into view' do
    @post.comments.create(body: 'This is a comment.', user_id: @commenter.id)
    visit authenticated_root_path

    click_button 'Toggle comments'
    expect(page).to have_content('This is a comment')
  end

  scenario 'toggling comments out of view' do
    @post.comments.create(body: 'This is a comment.', user_id: @commenter.id)
    visit authenticated_root_path

    click_button 'Toggle comments'
    expect(page).to have_content('This is a comment')

    click_button 'Toggle comments'
    expect(page).to_not have_content('This is a comment')
  end
end

最初,我连续 click_button 'Toggle comments' 两次。测试工作的迭代都没有。我也试过在这两个动作之间使用sleep n,但没有用。

Failures:

  1) Comment management viewing comments toggling comments out of view
     Failure/Error: expect(page).to_not have_content('This is a comment')
       expected not to find text "This is a comment" in "OdinFB PROFILE REQUESTS 0 LOG OUT The Feed Create post Luna Lovegood said... Body of post 0 Likes 1 Comments Like Comment Share Toggle comments This is a comment. Morfin Gaunt on Sep 18 2017 at 4:22PM"

在本地启动应用程序时,按钮本身会起作用。在测试中第一次激活后,它似乎变得不活动。

如有任何见解,我们将不胜感激,感谢阅读。

这里发生的是第二次按钮单击发生在预期文本在页面上可见之后但动画完成之前。 bootstrap 折叠代码然后变得混乱并且不会折叠评论,因为它认为它们尚未完全打开。在第二个 click_button 之前立即休眠一秒钟左右将解决此问题,因为它会延迟足够长的时间让动画完成。另一个选项(从测试时间的角度来看更好)是在测试模式下禁用动画。