loading...

Test Setup rails 5.2

tallerguy profile image tallerguy ・3 min read

Capybara, Selenium and Devise

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: Find the details here
  • database_cleaner: Find the details here

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

Capybara

In spec helper (rspec/spec_helper.rb) add the following at the very top

require 'capybara/rspec'

Capybara Screenshot

In spec helper (rspec/spec_helper.rb) add the following right below require capybara

require 'capybara-screenshot/rspec'

Selenium

In spec helper (rspec/spec_helper.rb) add the following below capybara-screeshot
require 'selenium-webdriver'

Database cleaner

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.

Capybara.default_driver = :selenium

options = Selenium::WebDriver::Chrome::Options.new(
  args: %w[headless disable-gpu no-sandbox disable-dev-shm-usage window-size=1600,1200 verbose]
)

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.javascript_driver = :selenium

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.
  • Setting the javascript_driver to selenium to make capybara pay heed JS (the ajax and async goodness).

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

config.use_transactional_fixtures turn it off (= false). Selenium has no idea what happens in the transaction (details here and here) and because of this, all you get are errors for invalid logins.

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.

Keep coding.

Posted on by:

tallerguy profile

tallerguy

@tallerguy

Passionate about making things. Rails is preferred tool

Discussion

markdown guide