chell
chell

Reputation: 7866

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?

Upvotes: 6

Views: 625

Answers (2)

Old Pro
Old Pro

Reputation: 25557

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.

Upvotes: 3

mbuechmann
mbuechmann

Reputation: 5770

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

Upvotes: 2

Related Questions