0

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. 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).

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. Neither iteration of the test work. I also tried using sleep n in between the two actions, but to no avail.

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.

  • Have you tried turning off headless so you can see what exactly is happening on the page? Also, note that your first test doesn't actually verify the content isn't visible before clicking the button, so it's not really testing that pushing the button toggles the conent. – Thomas Walpole Sep 18 '17 at 21:01
  • @ThomasWalpole thanks for the input. I have indeed run it headless, and have seen the button toggle the comment into view. And thanks for that second point. I added the necessary expectation and the test is still passing, thankfully. –  Sep 18 '17 at 22:09
  • I think you have a typo there since you say you have run it headless and seen the button... . To clarify, are you're saying the tests pass when run in non-headless mode, but fail in headless mode? – Thomas Walpole Sep 18 '17 at 22:12
  • My apologies, I have run the test with headless on AND off. In both instances, the test fails. –  Sep 18 '17 at 22:17
  • Ok - so when running non-headless, and you set a breakpoint (byebug, binding.pry, etc) after the second `click_button` - are the comments hiding/hidden (ie does it take longer for them to hide than you have Capybara.default_max_wait_time set to)? If they're not hiding, do they hide if you manually click the button in the browser at that point (while the test is paused)? – Thomas Walpole Sep 18 '17 at 22:21
  • When byebug is inserted after the second `click_button`, the comments are not hidden, and don't appear to be in the process of doing so. They do hide manually upon clicking the button, which causes the test to pass. I tried setting `Capybara.default_max_wait_time = 5`, but the test still failed. –  Sep 18 '17 at 22:32
  • If you put a `sleep 5` immediately before the second `click_button` does it pass? – Thomas Walpole Sep 18 '17 at 22:35
  • Thanks, that seemed to work. I don't know why I couldn't get it to work before (probably misplaced). –  Sep 18 '17 at 22:43
  • Added an explanation as an answer – Thomas Walpole Sep 18 '17 at 22:47

1 Answers1

0

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. The other option (and better from a test time perspective) is to disable animations in test mode.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78