Cucumber / Capybara -- how to get the host and port of the current execution

Ruby on-RailsSeleniumCucumberCapybara

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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

Capybara.server_port = 31337

Solution 3 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathanView Question on Stackoverflow
Solution 1 - Ruby on-RailsWoahdaeView Answer on Stackoverflow
Solution 2 - Ruby on-RailsiafonovView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmoritzView Answer on Stackoverflow