7

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.

artsince
  • 1,022
  • 20
  • 36
  • The server thread rails runs the test app in binds to Capybara.server_host (127.0.0.1 by default) - you can try changing that to whatever ip interface the docker container can talk back to your machine on - maybe 0.0.0.0 – Thomas Walpole Jan 29 '16 at 19:43
  • @TomWalpole Are you referring to `Capybara.app_host`? I parse the assigned ip for the container running rails and I pass it to app_host. Still, I can't access it from the selenium container. – artsince Jan 29 '16 at 20:53
  • 1
    No -- Capybara.app_host is used to build addresses the browser is told to visit, Capybara.server_host sets the ip the test app listens on for connections from the browser – Thomas Walpole Jan 29 '16 at 20:55
  • Oh sorry. I noticed soon after I submitted my comment. You are right, adding server_host fixed it. Thanks so much! – artsince Jan 29 '16 at 21:03
  • 1
    No problem -- I'll add it as an answer for you to accept – Thomas Walpole Jan 29 '16 at 21:04

1 Answers1

7

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
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78