Dockerized selenium browser cannot access Capybara

2019-03-29 06:54发布

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.

Here's my sample docker-compose.yml file:

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"

And I start docker-compose with networking enabled:

docker-compose --x-networking up

The test script runs an rspec command like this

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

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

And finally I call the test like this:

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"
...

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 can't establish a connection to the server at my-app:56555

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.

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)

Thanks for your help.

1条回答
做个烂人
2楼-- · 2019-03-29 07:14

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
查看更多
登录 后发表回答