3

Is there a way to get, in the test, the port in which the rails app is running during cucumber test? I tried Capybara.server_port but that's nil.

Thanks.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • possible duplicate of [Cucumber / Capybara -- how to get the host and port of the current execution](http://stackoverflow.com/questions/6791601/cucumber-capybara-how-to-get-the-host-and-port-of-the-current-execution) – Ciro Santilli OurBigBook.com Oct 09 '14 at 14:44
  • possible duplicate of [How to find the local port a rails instance is running on?](http://stackoverflow.com/questions/1554267/how-to-find-the-local-port-a-rails-instance-is-running-on) – Brad Werth Oct 09 '14 at 22:35

2 Answers2

5

When using the selenium driver, the port can be found on:

Capybara.current_session.driver.rack_server.port

and when using the webkit driver, it can be found on:

Capybara.current_session.driver.server_port

Alternative, you can set

Capybara.server_port

to a known value and use that.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
3

My understanding is that if you're using rack-test, the default Capybara driver, then there's isn't actually any real web server running to make requests to.

If you want to view your app as Cucumber/Capybara would, then you'd need to start it up manually on a chosen port:

$ RAILS_ENV=test rails s -p 4000

And then have something like this in env.rb:

Capybara.configure do |config|
  config.run_server = false
  config.app_host = "http://localhost:4000"
end
Andy Waite
  • 10,785
  • 4
  • 33
  • 47