简体   繁体   English

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

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

I'm new to Capybara and feature testing. 我是水豚和功能测试的新手。 I've been trying test a minor feature on a Rails app that toggles comments on a post into and out of view. 我一直在尝试在Rails应用程序上测试一个次要功能,该功能可将帖子中的评论切换为可见。 The first test for toggling the comments into view passes, but the second test for toggling them out of view doesn't. 将注释切换到视图中的第一个测试通过,但是将注释切换到视图外的第二个测试未通过。 (I am using the headless-chrome webdriver). (我正在使用无头铬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

Initially, I had click_button 'Toggle comments' twice, back-to-back. 最初,我两次click_button 'Toggle comments' ,背对背。 Neither iteration of the test work. 测试工作都没有迭代。 I also tried using sleep n in between the two actions, but to no avail. 我也尝试在两个动作之间使用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"

The button itself works when the app is fired up locally. 当应用在本地启动时,按钮本身可以工作。 It appears to become inactive once activated the first time around in testing. 第一次在测试中激活后,它似乎变得不活动。

Any insight would be appreciated, and thanks for reading. 任何见解将不胜感激,并感谢您的阅读。

What's happening here is the second button click is occurring after the expected text becomes visible on the page but before the animation has completed. 这里发生的是在预期的文本在页面上可见之后但在动画完成之前,第二次单击按钮。 The bootstrap collapse code then gets confused and doesn't collapse the comments since it considers them not fully opened yet. 引导折叠代码然后会变得混乱,并且不会折叠注释,因为它认为注释尚未完全打开。 A sleep for a second or so immediately before the second click_button will fix this since it delays long enough for the animation to complete. 在第二个click_button之前立即睡一秒钟左右click_button解决此问题,因为它会延迟足够长的时间以使动画完成。 The other option (and better from a test time perspective) is to disable animations in test mode. 另一个选项(从测试时间的角度来看更好)是在测试模式下禁用动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM