53

I need to download a csv file from my app using Open::URI and to do that I need to give it a fully qualified URI. So whats the best way to get the HOST and more importantly PORT in my cucumber script?

Note I am using Capybara and Selenium

Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
Jonathan
  • 16,077
  • 12
  • 67
  • 106

3 Answers3

90

For capybara < 2.0:

Capybara.current_session.driver.rack_server.host
Capybara.current_session.driver.rack_server.port

Capybara 2.0:

Capybara.current_session.server.host
Capybara.current_session.server.port

FYI, my use case is having clickable links in the mail generated by the integration tests.

Woahdae
  • 4,951
  • 2
  • 28
  • 26
  • 2
    Setting Capybara.server_port worked when the selenium integration test ran independent of other integration tests, but failed to change the port when run with other tests, at least in my env. Asking for the port number capybara wanted to use, seemed to work better with running multiple tests. Maybe it would have worked if I changed the port for all tests, instead of letting some choose on their own. – Anatortoise House Sep 21 '12 at 17:49
  • 1
    I used to use this solution but it doesn't work anymore with the latest Capybara version. I'm getting this error: NoMethodError: undefined method `rack_server' for # – Pablo Fernandez Nov 27 '12 at 09:18
  • Updated for 2.0 (it's even simpler, yay!) – Woahdae Nov 28 '12 at 17:44
  • 1
    Hi guys I have stupid question: is there way to tell Capybara to lunch a server when running without Selenium => therefore can access host & port in tests where I don't need Selenium ? Thank you. – equivalent8 Oct 02 '14 at 16:23
  • 2
    .@Woahdae does this still work (sorry been away for awhile -- happy to make this the correct answer) but `Capybara.current_session.server` returns `nil` for me – Jonathan Feb 06 '18 at 14:42
  • @Jonathan I'm using `Capybara.current_session.server` with Capybara 2.15, but I see that 3.0.0.dev is under development, so if you're using that version I'd say there was a change or a bug. I'll update the answer when I figure that out myself – Woahdae Feb 07 '18 at 18:31
47

You can set port that will be used to spawn an application server

Capybara.server_port = 31337
Nakilon
  • 34,866
  • 14
  • 107
  • 142
iafonov
  • 5,152
  • 1
  • 25
  • 21
0

You can use current_host but I don't know if it contains the port. Alternatively you can use current_url and strip the path (and possibly the query string).

moritz
  • 25,477
  • 3
  • 41
  • 36