Rack_Test or Selenium ?

Improving performance for your test suites is one of those things that can easily get overlooked when starting a new project but gets increasingly crucial as the test suites grow. a faster test suite results in less time waiting for the tests to run which saves time during development and increases productivity for developers.

In this article, we will explore the differences between selenium and rack_test and how you can leverage them to speed up your test; we will use Rspec as our testing framework for this article.

Rack::Test

Speed: It is generally faster than Selenium because it does not require a server or a browser to run tests. `rack_test` interacts directly with Rack interfaces to simulate HTTP requests and responses.

Simplicity and Limitations: Ideal for simple request/response cycles and is capable of testing templates, redirects, cookies, and more. However, it cannot handle JavaScript or test the front end beyond what is sent in the HTML responses.

Direct Integration: It integrates directly with Rails and does not require any setup for browsers or testing environments outside of the application’s stack.

Selenium

Handling Browsers: Selenium tests are run in a real browser environment or in headless browser environments. This capability means that it can test JavaScript and AJAX interactions as they would occur in an actual browser.

Slower but Comprehensive: Due to its operation in a full browser environment, Selenium tests tend to be slower compared to `rack_test`. However, this allows it to execute and validate more complex user interactions and end-to-end workflows.

Cross-browser Testing: Selenium supports testing across multiple browsers and platforms.

Use Case

Rspec uses selenium by default; that’s fine but it’s an unnecessary performance slowdown.

We can leverage both the speed of rack_test and the browser capabilities of selenium by setting up RSpec to use rack_test by default and only use selenium for tests that require Javascript.

Here’s what our configuration will look like.

config.before(:each, type: :system) do
  driven_by :rack_test
end

config.before(:each, type: :system, js: true) do
  driven_by :selenium_chrome_headless
end

And voilà! Just like that, you’re reduced the time you have to wait for your specs to run.