Dockerized硒浏览器无法访问Capybara测试网址 [英] Dockerized selenium browser cannot access Capybara test url

查看:76
本文介绍了Dockerized硒浏览器无法访问Capybara测试网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在dockerized硒独立firefox浏览器上运行Ruby on Rails功能测试。
似乎我的网络出现问题,因为硒实例无法将
连接到Capybara启动的url。

I am trying to run Ruby on Rails feature tests on dockerized selenium standalone firefox browser. It seems like I am having issues with networking because the selenium instance can't connect to the url started by Capybara.

这是我的示例 docker-compose.yml 文件:

ff:
  image: selenium/standalone-firefox:2.48.2
  container_name: firefox-browser
web:
  build: .
  container_name: my-app
  volumes:
    - ".:/home/ubuntu/my-app"
  command: /bin/bash -l scripts/docker-start-tests.sh
  ports:
    - "3000:3000"

在启用网络的情况下启动docker-compose:

And I start docker-compose with networking enabled:

docker-compose --x-networking up

测试脚本运行这样的rspec命令

The test script runs an rspec command like this

rspec ./spec/features/login_spec.rb:43

对于docker test我已启用Capybara的远程驱动程序:

For docker tests I have enabled remote driver for Capybara:

Capybara.register_driver :docker_firefox do |app|
  Capybara::Selenium::Driver.new(app, {
    browser: :remote,
    url: "#{ENV['FF_URL']}/wd/hub",
    desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox
  })
end

最后我这样调用测试:

unless ENV['FF_URL'].nil?
  Capybara.current_driver = :docker_firefox
  Capybara.javascript_driver = :docker_firefox

  Capybara.app_host = "http://my-app:56555"
  Capybara.server_port = "56555"
  # Capybara.server_host = "my-app"
end

visit root_path
save_and_open_screenshot
click_link "Sign in"
...

我可以查看浏览器容器日志,看到硒接收到了水豚的命令。
问题是它无法连接到提供的URL,我可以用屏幕快照确认。

I can tail browser container logs and I see that selenium receives commands from Capybara. The problem is that it cannot connect to the url provided, which I can confirm with the screenshot.


Firefox无法在my-app:56555上建立与服务器的连接

Firefox can't establish a connection to the server at my-app:56555

为了更好地理解问题,我启动了Rails应用程序,并尝试从硒
容器中访问它。我注意到,如果我使用ip绑定启动rails应用程序,则只能从硒容器访问该应用程序。

To better understand the problem, I started the rails app and I tried to access it from the selenium container. I notice that I can only access the app from the selenium container if I start the rails app with an ip binding.

rails s Puma -b 0.0.0.0

这看起来像是网络问题,但是我不知道解。

This looks like a networking problem, but I can't figure out a solution.

我如何使硒容器访问通过Capybara运行Rspec功能测试的rails应用程序?

How can I make the selenium container access the rails app running Rspec feature tests with Capybara?

capybara (2.6.0)
selenium-webdriver (2.48.1)

感谢您的帮助。

推荐答案

服务器线程轨在绑定Capybara.server_host接口(默认为127.0.0.1)的情况下运行测试应用程序-您可以将其更改为docker容器可以与您的计算机对话的任何ip接口-在您的情况下

The server thread rails runs the test app in binds to the Capybara.server_host interface (127.0.0.1 by default) - You can change that to whatever ip interface the docker container can talk back to your machine on - in your case possibly

Capybara.server_host = '0.0.0.0' # bind to all interfaces

这篇关于Dockerized硒浏览器无法访问Capybara测试网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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