Rails system test drivers can I still use rack_test?

1k Views Asked by At

Rails system tests use Capybara. Capybara uses rack_test driver by default, while System tests uses selenium by default.

Capybara Drivers:

I want to know why these were chosen and what are sensible choices for a Rails programmer writing System tests.

1

There are 1 best solutions below

0
On

System tests run using Capybara. Capybara use drivers to complete the testing. Capybara drivers can be defined as:

The driver is the part of Capybara that actually manages a web page as a series of DOM elements - Rails 5 Test Prescriptions - Noel Rappin.

Capybara uses by default a rack_test driver, a faster but JavaScript unaware version. while Rails system tests uses by default, a slower but Javascript aware version, selenium - think this code sets the Rails default.

I wanted to know the reasons for the default and if I could still be using rack_test. I found a quote from the original PR:

I chose the Selenium driver as default because the purpose of adding system testing to Rails is for the initial setup to be absolutely zero. Capybara does that through their default, Rack Test, but it's not really a useful demonstration of the merits of system testing since it doesn't support JavaScript testing. Comment in PR from Eileen codes

So there's no technical reason to choose Selenium vs Rack. Selenium was chosen as it was a fuller functioned low configuration default.

Rack test is limited it was described as:

If you don't want a full browser - if you just want to assert on HTML instead of just executing Javascript ... What rack test does is basically fakes out communication between a real browser with a fake one which Capybara can use to parse the response without making a web request at all. You can make assertions about what the page contains.

Rails conf 2018 - Sam Phippen

If your system test isn't using Javascript you may consider using rack_test instead. However, it's a trade off - rack will be faster because it doesn't know about Javascript but it can't replace the complexity of a physical browser.