The following test passed first time (after rake db:test:prepare) but failed gave error on subsequent run.
Capybara::ElementNotFound: Unable to find css "#sale_payment_btn"
require "test_helper"
DatabaseCleaner.strategy = :transaction
class SaleFeatureTest < Capybara::Rails::TestCase
include Warden::Test::Helpers
Warden.test_mode!
self.use_transactional_fixtures = false
setup do
login_as users(:admin), scope: :user
Capybara.current_driver = :selenium #:webkit
DatabaseCleaner.start
end
teardown do
DatabaseCleaner.clean
Warden.test_reset!
end
test "Sale" do
visit(new_sale_path) # create new sale and redirect to it
assert page.has_css?('#sale_payment_btn') # gave error at second time
find(:css, '#sale_payment_btn').click # this create payment
end
As I used selenium with chrome, I could see the ID of the sale. I noticed that the ID was the same for subsequent test. i.e. 980190963
My theory is that
database_cleanernot functioning as expected. (Although I see DB cleaning sql commands ontest.logfile, I saw data remained in database)visitdoes not create new@sale(as stated here though I use minitest) because#sal_payment_btnis not rendered (sale already haspaymenton first run).
I'm pulling my hair around for half day now. I have tried
webkitdriver- different cleaning strategy
truncation,deletion
and I still can't pass the test on second run. It is running okay on manual test.
What and where did I do wrong?
P.S. I'm using the following gems
minitest-rails-capybara
selenium-webdriver
chromedriver-helper
database_cleaner
minitest-around
pg
I have read the following
I've confirmed that the problem was my setup of
database_cleaner.Short version
:transactionstrategy was not working for my setupcucumber+webkit/selenium. Changed to:deletionstrategy.What I learnt
It seemed I didn't study enough. I found out the following during my search for answer.
Someone asked Why is the database cleaner transaction strategy not working with Cucumber and Selenium? - no answer
How to set up database_cleaner for Rails with Cucumber and RSpec stated that
database_cleanerreadme stated that:transactionstrategy imposes bit more workingOther worthy reading
deletionstrategydatabase_cleanerwithrails,rspec,capybaraandselenium- http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/