So have been looking at these three for quite some time. Mostly in older versions of rails and as an after effect at the older version of these gems.
Started working in a hobby project with rails 5.2 (why not rails 6?). This is what I had to do to make it all work.
First things first, capybara and selenium come preinstalled.
What else do you need:
Capybara screenshot takes screenshots every time there is an error in your test and will help you when you scratch(pull out?) your head (hair?).
database_cleaner is used to clean out test DB to start each test with a clean DB.
Ok quickly moving on, where do you add the settings and what are the settings
Assumption: the Testing framework is RSpec
In spec helper (rspec/spec_helper.rb) add the following at the very top
In spec helper (rspec/spec_helper.rb) add the following right below require capybara
In spec helper (rspec/spec_helper.rb) add the following below capybara-screeshot
Add a file inside rspec/support called database_cleaner.rb. Add the following inside the file.
RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.strategy = :truncation end config.before(:each, js: true) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end
Pay special attention to
config.before(:each) do DatabaseCleaner.strategy = :truncation end
This setting becomes important when we use selenium with devise to login by visiting the login page in a headless chrome(More about this in another post).
Ok, now the main setting to make selenium work with capybara, inside spec helper, after the require statements add the following.
one by one now,
- we set the default driver for running tests on a headless browser as selenium. There are others, like poltergeist
- Options that we choose for the browser that will run our test suite, headless part is important, you don't want the browser popping up for every test.
Now that all the setting is done, when you actually write a test for logging in, how will it run?
In order for all this to work, inside rspec/rails_helper.rb there is a setting
Remember the pay attention to this part in database settings, ya well if you don't make it truncation all this won't work.
Why is that? Haven't dug deep enough to write about it yet, but expect an article soon.
Alright, that's it for now. Later.