Rspec System Test passes when run individually, fails when run with entire suite
Asked Answered
N

2

6

I have the following test that passes when I run it in isolation:

require 'rails_helper'

RSpec.describe 'deleting a non-recurring user event', js: true do

  let(:user)       { create(:proofreader_user) }
  let(:date)       { Date.current.strftime('%Y-%m-%d') }
  let(:date_time)  { date + ' 00:00'}

  it 'deletes the event' do
    visit root_path
    click_on "Login"
    fill_in  "Email",    with: user.email
    fill_in  "Password", with: user.password
    click_on "Sign In"
    visit calendar_path
    expect(current_path).to eq(calendar_path)
    expect(page).to have_css("#complete_registration:disabled")
    expect(page).to_not have_css("td.fc-event-container")
    find("td.fc-day[data-date='#{date}']").click
    expect(page).to have_css("div#user-event-modal")
    expect(page).to have_select('user_event[title]', :options => UserEvent.titles.keys)
    expect(find('input', id: 'user_event_starting').value).to eq date_time
    expect(find('input', id: 'user_event_ending').value).to eq date_time
    page.execute_script("$('input#user_event_starting').val('#{date} 09:00')")
    expect(find('input', id: 'user_event_starting').value).to eq date + ' 09:00'
    page.execute_script("$('input#user_event_ending').val('#{date} 12:00')")
    expect(find('input', id: 'user_event_ending').value).to eq date + ' 12:00'
    click_on 'Save'
    expect(page).to have_css("td.fc-event-container a.fc-day-grid-event")
    expect(page).to have_css("span.fc-time", text: '9a - 12p')
    expect(page).to have_css("span.fc-title", text: 'work')
    find("span.fc-time", text: '9a - 12p').click
    expect(page).to have_css("div#user-event-modal")
    find("#del_one_event").click
    within('.swal2-actions') { click_button('Yes') }
    wait_for { page }.to_not have_css("div#user-event-modal")
    expect(page).to_not have_css("td.fc-event-container")
    expect(page).to_not have_css("span.fc-time", text: '10a - 14p')
  end
end

However, when I run all of the tests I get the following error:

Failure/Error: within('.swal2-actions') { click_button('Yes') }

     Capybara::ElementNotFound:
       Unable to find visible css ".swal2-actions"

Why does this test fail when I run it with the other test and how can I fix it so it passes when I run all tests?

Nellynelms answered 18/2, 2019 at 10:22 Comment(0)
D
3

Cannot be sure without a lot more information. It would be best if you can create and publish a minimum complete verifiable example.

Based on the information given, I would guess that the test that fails when run with the other tests is not properly isolated. The other examples are changing the state of the application, causing this test to fail. So look at your before and after hooks and make sure you are setting config.use_transactional_fixtures = true in rails_helper.rb

If it is possible for there to be a delay between the time #del_one_event is clicked and .swal2-actions appears, then change

within('.swal2-actions') { click_button('Yes') }

to

find('.swal2-actions').click_button('Yes')

If none of those fix the problem, you might have an issue with browser caching, but that is even harder to debug.

Debug answered 22/2, 2019 at 3:4 Comment(2)
Thanks you I will try your suggestions and report back to you.Nellynelms
following your instructions to change the within to find seems to have fixed the problem.Nellynelms
P
2

This is hard to answer without seeing the other tests. But I noticed some points that are worth checking out:

  • you are creating data by navigating the application, perhaps other tests create data, that prevent data in this test to be created
  • The command find("#del_one_event").click (one line before) just executes the click, but does not wait until anything happens. With all tests and more data in the db, it might take a while longer until .swal2-actions appears and is not yet present when you execute within('.swal2-actions') { click_button('Yes') }

Another way to get closer to the bug is by making screenshots and comparing them. Check out the gem capybara-screenshot

Peace answered 20/2, 2019 at 14:15 Comment(3)
Thanks for your thoughts and expertise. I will try to see how I can set a wait for the swal2-actions to appear and see if that fixes it.Nellynelms
You are correct there was an issue with waiting for the swal-2 action confirmation modal to appear. I had to give it to @OldPro because of the exact code that was supplied and worked. Thank you also for your direction and expertise. I wish I could divide up the bounty but they don't give that option here yet.Nellynelms
@Nellynelms No problem. If this helped, an upvote would be appreciated, though.Peace

© 2022 - 2024 — McMap. All rights reserved.