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

3

54

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

Matrix answered 22/7, 2011 at 14:36 Comment(1)
Outside Capybara: #6807768Bugeye
P
48

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

Capybara.server_port = 31337
Pitching answered 23/7, 2011 at 13:50 Comment(2)
@iafonov, +1 for 31337Patchwork
I wanted capybara to listen all interfaces so I set the server host ruby Capybara.server_host = '0.0.0.0' Cystine
L
94

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.

Lennalennard answered 1/3, 2012 at 23:18 Comment(6)
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.Chadchadabe
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>Rhodia
Updated for 2.0 (it's even simpler, yay!)Lennalennard
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.Luby
.@Lennalennard does this still work (sorry been away for awhile -- happy to make this the correct answer) but Capybara.current_session.server returns nil for meMatrix
@Matrix 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 myselfLennalennard
P
48

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

Capybara.server_port = 31337
Pitching answered 23/7, 2011 at 13:50 Comment(2)
@iafonov, +1 for 31337Patchwork
I wanted capybara to listen all interfaces so I set the server host ruby Capybara.server_host = '0.0.0.0' Cystine
I
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).

Immunogenic answered 22/7, 2011 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.