2

I'm starting with integration tests and I'm using Docker on my dev env.

I have selenium-webdriver gem installed and this config file:

docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app,
  :browser => :remote,
  :url => "http://#{docker_ip}:4444/wd/hub")
end

Capybara.app_host = "http://#{docker_ip}:3010"
Capybara.server_host = '0.0.0.0'
Capybara.server_port = '3011'

and when I run my integration test, it fails because I created a record with FactoryGirl (which creates on test DB) and my app (on port 3010) is running in dev env. Capybara should starts by itself my app in test env? What am I doing wrong?

PS: My app starts on dev end when I run docker-compose up

jonatasdaniel
  • 183
  • 3
  • 11
  • If you set Capybara.app_host, that's where Capybara is going to tell the browser to connect to. Therefore, if your dev env is running on port 3010 you shouldn't be setting app_host to port 3010 – Thomas Walpole May 17 '17 at 01:28
  • But if I don't set app_host I got a reached error, capybara can't connect to my app. How should it be? – jonatasdaniel May 17 '17 at 01:37
  • app_host needs to be set the address that Capybara will run your app at. Assuming I'm understanding what you're doing correctly (running the browser/selenium grid in docker, but capybara on your local machine) then `app_host` would be set to `http://#{the ip of your local machine accessible from docker}` and you would set `Capybara.always_include_port = true` – Thomas Walpole May 17 '17 at 01:41
  • My capybara is also running inside Docker – jonatasdaniel May 17 '17 at 01:43
  • If everything is being run in Docker, then you may not need to specify `app_host` at all, or potentially `http://#{docker_ip}` and set `Capybara.always_include_port = true` . It needs to end up being an ip that resolves to one of the interfaces for whatever you've set Capybara.server_host to (in your case 0.0.0.0, which means all interfaces on the docker instance) – Thomas Walpole May 17 '17 at 01:50
  • But my app will run at dev env, and capybara in test env. That's the problem I'm having. I don't know if I'm doing erything right. – jonatasdaniel May 17 '17 at 01:52
  • By default Capybara runs it's own instance of your app at Capybara.server_host:Capybara.server_port, what your dev instance is being run at is irrelevant (unless you set app_host to point at your dev instance, which is just wrong) – Thomas Walpole May 17 '17 at 01:55
  • If I comment the line `app_host` I got this error: `Selenium::WebDriver::Error::UnknownError: Reached error page: about:neterror?e=connectionFailure` – jonatasdaniel May 17 '17 at 02:55
  • And did you try setting apphost to `http://#{docker_ip}` and also setting `Capybara.always_include_port = true` as I suggested? – Thomas Walpole May 17 '17 at 02:57
  • Now I have: `Capybara.app_host = "http://#{docker_ip}" Capybara.server_host = '0.0.0.0' Capybara.server_port = '3011' Capybara.always_include_port = true` but I got the same error – jonatasdaniel May 17 '17 at 03:01
  • Then there's something about your setup that you haven't explained fully, or you've changed other Capybara default settings -- As a last resort try setting Capybara.server_host to your docker ip, beyond that I have no idea what you're doing. – Thomas Walpole May 17 '17 at 03:57
  • I have in my docker-compose `selenium: image: selenium/standalone-firefox-debug:3.4.0 ports: - 4444:4444 - 5900:5900` and in my Dockerfile That's what I have – jonatasdaniel May 17 '17 at 13:28
  • @jonatasdaniel Hi, did you solve your problem? I have a similar problem [on this SO question here](https://stackoverflow.com/questions/44732620/rails-system-test-with-capybara-and-headless-selenium-browser-in-docker) – SanjiBukai Jun 24 '17 at 07:58
  • @SanjiBukai I didn't find any solution, then I moved to poltergeist rather then selenium. – jonatasdaniel Jun 26 '17 at 11:30
  • @jonatasdaniel I finally found a [very good solution](https://stackoverflow.com/a/44746792/5121955)! – SanjiBukai Jun 27 '17 at 09:10
  • @SanjiBukai very interesting, I'll try this. Do you thin Selenium is better than poltergeist? – jonatasdaniel Jun 28 '17 at 01:08

1 Answers1

1

In using docker-compose version 3, this works for me

Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
  :browser => :remote,
  :url => "http://selenium:4444/wd/hub")
end

Capybara.app_host = "http://web:3010"
Capybara.server_host = '0.0.0.0'
Capybara.server_port = '3010'
Capybara.always_include_port = true

docker-compose.yml

version: '3'
services:
  web:
    build: .
    depends_on:
      - db
      - selenium
  ...
  selenium:
    image: selenium/standalone-firefox-debug:2.47.1
    ports:
      - "4444:4444"
  ...
miguel savignano
  • 1,109
  • 13
  • 9