A quick fix for an annoying (and not very descriptive) error Browsing context has been discarded when setting up Ruby on Rails system tests with Firefox headless.
Here is how that might look like:
Error:
PublicPagesTest#test_visiting_the_index:
Selenium::WebDriver::Error::NoSuchWindowError: Browsing context has been discarded
test/system/public_pages_test.rb:7:in `block in '
Error:
PublicPagesTest#test_visiting_the_index:
Selenium::WebDriver::Error::UnknownError: Failed to decode response from marionette
Alternatively:
Error:
PublicPagesTest#test_visiting_the_index:
Selenium::WebDriver::Error::SessionNotCreatedError: Tried to run command without establishing a connection
test/system/public_pages_test.rb:7:in `block in <class:PublicPagesTest>'
Error:
PublicPagesTest#test_visiting_the_index:
Selenium::WebDriver::Error::SessionNotCreatedError: Tried to run command without establishing a connection
This happens especially if you are running this in a constrained environment such as Linux container (with Docker) or a VM (with Vagrant). Simply put there is not enough memory.
If you are running Vagrant as me the actual fix is to increase the memory limit:
# Vagrantfile
...
config.vm.provider :libvirt do |libvirt|
libvirt.memory = 4096
end
...
And now the tests work!
$ rails test:system
Run options: --seed 53251
# Running:
Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:36151
.
Finished in 5.667349s, 0.1764 runs/s, 0.1764 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
Check out my book
Interested in Ruby on Rails default testing stack? Take Minitest and fixtures for a spin with my latest book.
Get Test Driving Rails while it's in prerelease.