According to the Capybara Docs there are two separate strategies for handling problems with Selenium not being able to access data generated in a test:
Monkey patch ActiveRecord:
class ActiveRecord::Base @@shared_connection = nil def self.connection @@shared_connection || retrieve_connection end end ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
Use truncation rather than transactions
I've set up DatabaseCleaner as outlined by Avdi Grimm here, and although looking at my logs I can clearly see DatabaseCleaner is using truncation, for example:
(7.4ms) TRUNCATE TABLE "galleries", "photos", "users" RESTART IDENTITY CASCADE;
… when I run my feature, any models I create as part of the setup, are not available to the driver. I can see the models exist within the scenario, but if I print them out to the page, they don't exist.
The only way I can get the driver to see the models is if I use the ActiveRecord monkey-patch above, but this seems to call lots of strange secondary issues - tests hang etc.
Why is it that the driver (Selenium or Capybara-Webkit) cannot see models created in the tests even when I use DatabaseCleaner with a truncation strategy and how can I get Capybara working with a JavaScript driver?
Note: The test passes fine using the default driver
My DatabaseCleaner configuration:
RSpec.configure do |config|
# Clean DB completely
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
# Set default strategy (non-js/Rack::Test)
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# Set strategy for tests using JS (slower)
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
# Wrap DatabaseCleaner around each test (before & after)
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Here is an example of a feature that I would like to use JavaScript to test:
feature "User deletes a photo" do
before {
as_signed_in_user
@gallery = create(:gallery_with_photos)
}
scenario "successfully deletes a photo from a gallery", js: true do
photo_title = @gallery.photos.first.title
puts "GALLERY #{Gallery.find(1)}" # <Gallery:0x007f9b928fe8e8>
visit gallery_path(@gallery) # JavaScript driver can't find gallery
within '.photos-list' do
click_link photo_title
end
click_link('Delete Photo')
expect(current_path).to eq( gallery_path(@gallery) )
expect(page).not_to have_link(photo_title)
end
end
When using the JavaScript driver, this test fails with:
1) User deletes a photo successfully deletes a photo from a gallery
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::RecordNotFound:
Couldn't find Gallery with 'id'=1
As suggested by Dave Schweisguth in the comments, I have tried moving the js:true out to the feature rather than the scenario without it having any effect.
:jstag in your specs? If it's onfeature/describe, try tagging the enclosingscenario/it. (You might need to restructure your spec for that to make sense.) - Dave Schweisguthjs:true. I switched them around just for a sanity check and it made no difference. - Undistractionconfig.use_transactional_fixturesset to? My capy suites run this asfalse- nowk