Search code examples
ruby-on-railsruby-on-rails-5rspec-rails

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


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?


Solution

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