Rails system test with capybara browser and headless selenium in Docker
TL; DR: any idea on how to properly configure capybara to be able to control selenium remote browser in docker container with minimal default Rails system test?
I am running Rails in a docked env. Now I want to start some "system tests", but since I am running inside Docker, I come up with some problems.
I am using a standard test suite (minitest?) With capybara
and selenium-webdriver
gems.
I have already installed the package chromedriver
to the container using the following:
RUN apt-get install -y chromedriver \
&& ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin
But the launch rails test:system
throws the following errorSelenium::WebDriver::Error::WebDriverError: Unable to find chromedriver.
Actually I don't know if chrome itself is installed or not?
which chrome
does not output anything.
which chromium
outputs /usr/bin/chromium
.
I have also tried with xvfb
no success.
So (since I had no idea) I tried to go ahead and actually go with a docker test environment of the system.
I found some docker images of selenium. So I ran among my rails and database containers the container selenium-standalone-chrome
(the actual docker-compose.yml which I use here )
Then I found useful information about the configuration that needs to be done to allow the capybara driver to the selenium remote browser.
All examples I found on the internet use rspec, but since I am using the default mini-interface, I tried to adapt the capybara driver to minispec, but I had some doubts about how to do this and where to place the config.
For system tests, I guessed that the best location is the file application_system_test_case.rb
. Also I found and tried many different capybara configs and ended up with the following which seems to be the most complete (available here)
At this point, the test seems to be working well as I have no error, but it always fails.
It doesn't work regardless of accessing the driver configuration (the method setup_remote
where I defined the server host and port) prior to the test case.
With or without a call, I got a "site cannot be" error ( here's a screenshot ) the
test file I used . (Testing some responsive dynamic displays)
However, I can access the selenium container with the specified url from the browser from my host machine. And both containers see each other. I did a ping from the container shell.
The following helpful questions don't work for me:
selenium dockerized browser cannot access Capybara test url
How do I run system tests without a browser in Rails 5.1?
Any idea on how to properly configure capybara to be able to control a selenium remote browser in a docker container with a minimal default Rails system test?
Many thanks.
source to share
You must override the host method for Capybara to use the container's IP address. Send a message: https://medium.com/@pacuna/using-rails-5-1-system-tests-with-docker-a90c52ed0648
source to share