Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails system test with capybara and headless selenium browser in Docker

TL;DR: Any idea about how to properly configure capybara to be able to drive a remote selenium browser in a docker container with default Rails minitest system test?

I'm running Rails in a dockerized env.. Now I want to start some "system tests" but since I'm running inside Docker I come up with some issues.

I'm using the default test suite (minitest?) with capybara and selenium-webdriver gems.

I've already installed the chromedriver packet in the container using the following:

RUN apt-get install -y chromedriver \
  && ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin

But running rails test:system outputs the following error Selenium::WebDriver::Error::WebDriverError: Unable to find chromedriver.

In fact I don't know if chrome itself is installed or not?
which chrome outputs nothing.
which chromium outputs /usr/bin/chromium.

I also tried with xvfb without success.

So (since I had no clue) I tried to go further and actually go with a dockerized system test environment as well.

I found some Docker images from selenium. So I ran among my rails and database containers a selenium-standalone-chrome container (the actual docker-compose.yml I'm using is here)

Then I found some useful information about the configuration to be done to let capybara driver the remote selenium browser.
All the examples I found on internet use rspec, but since I'm using the default minispec I tried to adapt the capybara driver to minispec but I had some doubt about how to do it and where to put the configuration.

For system tests I guessed that the best location is the file application_system_test_case.rb. Also I found and I tried many different capybara configurations and I end up with the following which seems to be the most complete (available here)

At that moment the test seems to perform well since I have no error but it always fails.

It fails regardless of making a call to the driver configuration (the setup_remote method where I defined the server host and port) before the tests case.

With or without the call I got the "site can't be reached" error (here is the screenshot)
Here is the test file I used. (Testing some react dynamic display)

However I can access to the selenium container with the given URL from the browser from my host machine. And both containers sees each others. I did some ping from within the containers shell.

The following SO questions being helpful don't work for me:
Dockerized selenium browser cannot access Capybara test url
How can I run headless browser system tests in Rails 5.1?

Any idea about how to properly configure capybara to be able to drive a remote selenium browser in a docker container with default Rails minitest system test?

Thank you very much.

like image 203
SanjiBukai Avatar asked Jun 24 '17 04:06

SanjiBukai


1 Answers

You have to override the host method so Capybara uses the container's IP address. Check out this post: https://medium.com/@pacuna/using-rails-5-1-system-tests-with-docker-a90c52ed0648

like image 117
pacuna Avatar answered Oct 23 '22 14:10

pacuna