在 Docker 中使用水豚和无头硒浏览器进行 Rails 系统测试 [英] Rails system test with capybara and headless selenium browser in Docker

查看:15
本文介绍了在 Docker 中使用水豚和无头硒浏览器进行 Rails 系统测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR:关于如何正确配置 capybara 以便能够在具有默认 Rails minitest 系统测试的 docker 容器中驱动远程 selenium 浏览器的任何想法?

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?

我在 dockerized 环境中运行 Rails..现在我想开始一些系统测试",但由于我在 Docker 中运行,所以我遇到了一些问题.

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.

我正在使用带有 capybaraselenium-webdriver gems 的默认测试套件(minitest?).

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

我已经使用以下方法在容器中安装了 chromedriver 数据包:

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

但是运行 rails test:system 会输出以下错误 Selenium::WebDriver::Error::WebDriverError: Unable to find chromedriver.

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

其实我也不知道chrome本身有没有安装?
which chrome 什么也不输出.
which chromium 输出 /usr/bin/chromium.

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

我也试过 xvfb 没有成功.

I also tried with xvfb without success.

所以(因为我不知道)我试图走得更远,实际上也使用了 dockerized 系统测试环境.

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

我从 selenium 中找到了一些 Docker 映像.所以我在我的 Rails 和数据库容器中运行了一个 selenium-standalone-chrome 容器(实际的 我使用的docker-compose.yml在这里)

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)

然后我发现了一些有用的信息,关于配置让水豚驱动 remote selenium 浏览器.
我在互联网上找到的所有示例都使用 rspec,但由于我使用的是默认的 minispec,因此我尝试将 capybara 驱动程序调整为 minispec,但我对如何做到这一点以及将配置放在哪里有些疑问.

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.

对于系统测试,我猜最好的位置是文件 application_system_test_case.rb.我还发现并尝试了许多不同的水豚配置,最终得到以下似乎是最完整的 (在这里)

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.

无论在测试用例之前调用驱动程序配置(我定义服务器主机和端口的 setup_remote 方法),它都会失败.

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.

无论是否拨打电话,我都会收到 无法访问网站" 错误(这里是截图)
这是我使用的测试文件.(测试一些react动态显示)

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)

但是,我可以从主机的浏览器中使用给定的 URL 访问 selenium 容器.两个容器都能看到对方.我从容器外壳中执行了一些 ping 操作.

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.

以下对我有帮助的 SO 问题对我不起作用:
Dockerized selenium 浏览器无法访问 Capybara 测试网址
如何无头运行Rails 5.1 中的浏览器系统测试?

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?

任何关于如何正确配置 capybara 以便能够在具有默认 Rails minitest 系统测试的 docker 容器中驱动远程 selenium 浏览器的想法?

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?

非常感谢.

推荐答案

您必须重写 host 方法,以便 Capybara 使用容器的 IP 地址.查看这篇文章:https://medium.com/@pacuna/using-rails-5-1-system-tests-with-docker-a90c52ed0648

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 中使用水豚和无头硒浏览器进行 Rails 系统测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆