在Docker中使用Capybara和无头硒浏览器进行Rails系统测试 [英] Rails system test with capybara and headless selenium browser in Docker
问题描述
TL; DR:关于如何正确配置capybara以便能够使用默认的Rails minitest系统测试在docker容器中驱动远程硒浏览器的想法吗?
我正在dockerized环境中运行Rails. 现在,我想开始一些系统测试",但是由于我正在Docker中运行,所以我遇到了一些问题.
我正在使用带有capybara
和selenium-webdriver
gem的默认测试套件(最小?).
我已经使用以下方法在容器中安装了chromedriver
数据包:
RUN apt-get install -y chromedriver \
&& ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin
但是运行rails test:system
会输出以下错误Selenium::WebDriver::Error::WebDriverError: Unable to find chromedriver.
实际上我不知道 chrome 本身是否已安装?
which chrome
不输出任何内容.
which chromium
输出/usr/bin/chromium
.
我也尝试了xvfb
,但没有成功.
所以(由于我一无所知),我尝试了更进一步,实际上也使用了dockerized系统测试环境.
我从硒中找到了一些Docker映像.因此,我在Rails和数据库容器中运行了一个selenium-standalone-chrome
容器(实际的 docker-compose.yml我使用在这里)
然后,我发现了一些有关配置的有用信息,以使capybara驱动程序成为 remote 硒浏览器.
我在Internet上找到的所有示例都使用rspec,但是由于我使用的是默认的minispec,因此我试图使capybara驱动程序适应minispec,但我对如何执行以及将配置放在何处有些疑问.>
对于系统测试,我猜最好的位置是文件application_system_test_case.rb
.我也发现了,并尝试了许多不同的水豚配置,最后得到以下内容,它似乎是最完整的在此处可用)
由于我没有错误,但此刻测试似乎表现良好,但它总是失败.
无论在测试用例之前调用驱动程序配置(在其中定义服务器主机和端口的setup_remote
方法),都失败.
无论有无通话,我都收到无法访问网站" 错误(我使用的测试文件. (测试一些反应动态显示)
但是,我可以从主机从浏览器访问具有给定URL的硒容器.并且两个容器都可以看到对方. 我在容器外壳内进行了ping操作.
以下有帮助的SO问题对我不起作用:
Dockerized的硒浏览器无法访问Capybara测试URL
如何无头跑步在Rails 5.1中进行浏览器系统测试?
关于如何正确配置capybara以便能够使用默认的Rails minitest系统测试在docker容器中驱动远程硒浏览器的想法吗?
非常感谢您.
您必须覆盖host方法,以便Capybara使用容器的IP地址.看看这篇文章: https://medium.com/@pacuna/using-rails-5-1-system-tests-with-docker-a90c52ed0648
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.
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
这篇关于在Docker中使用Capybara和无头硒浏览器进行Rails系统测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!