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

21,486

Solution 1

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

Capybara.server_port = 31337

Solution 2

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.

Share:
21,486
Jonathan
Author by

Jonathan

I’m Jonathan Tushman I am a pragmatic technologist (bio) I work at InsightSquared I love to build things I like music and playing board games I read the Economist Follow me if you like @tushman

Updated on October 01, 2020

Comments

  • Jonathan
    Jonathan over 3 years

    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

  • Anatortoise House
    Anatortoise House over 11 years
    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.
  • pupeno
    pupeno over 11 years
    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 #<Capybara::Selenium::Driver:0x007fa371676d00>
  • Woahdae
    Woahdae over 11 years
    Updated for 2.0 (it's even simpler, yay!)
  • equivalent8
    equivalent8 over 9 years
    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.
  • Roman Pushkin
    Roman Pushkin almost 9 years
    @iafonov, +1 for 31337
  • bonyiii
    bonyiii over 7 years
    I wanted capybara to listen all interfaces so I set the server host ruby Capybara.server_host = '0.0.0.0'
  • Jonathan
    Jonathan about 6 years
    .@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
  • Woahdae
    Woahdae about 6 years
    @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