Reputation: 5962
I am using Capybara with selenium AND/OR capybara-webkit driver and Webmock as a stubbing framework.
When I run my integration tests for google maps (places) autocomplete input using selenium (firefox) the browser makes a few real calls to google and receives JSONs with the place components.
When I change to capybara-webkit driver (muuuuch faster) to do the same I get webmock message that I should stub an external requests.
Can someone please explain me why there's difference between the two?
Note: I set WebMock.disable_net_connect!( :allow_localhost => true )
to allow calls to the local server...
EDIT:
Tools like WebMock are great, but when testing JavaScript, it’s a seperate browser process that loads the page, and not your Ruby test process. That means that the request to your build server isn’t going through Net::HTTP; the requests are coming from Firefox or capybara-webkit, and those tools are gleefully unaware of your feeble attempts to reroute HTTP traffic
quote from http://robots.thoughtbot.com/using-capybara-to-test-javascript-that-makes-http
This explains why some calls to a payment system (done in the Rails controller) need to be stubbed and why ajax doesn't, but it doesn't explain why Firefox reaches Google and webkit asks for a stub
SOLUTION:
TL;DR; keep :selenium and make a real call in tests that need to make ajax call to external APIs
I tried mocking with puffing-billy that is used for JS/browser external calls, but this one doesn't "cooperate well" with Webmock. Also I have like 25 stubs for the payment system alredy written in Webmock so no way I am switching...
I also tried VRC which is recording framework, but this one is also for calls made by your application (Ruby NET library)
In the end I simply decided to keep running these couple of tests that need ajax API responses using :selenium driver which starts firefox and makes a real call to gMaps
#spec_helper
...
WebMock.disable_net_connect!( :allow_localhost => true ) # Ask to stub all requests except to localhost
...
# Use headless capybara-webkit that is way faster!
Capybara.javascript_driver = :webkit
config.before(:each, js: true) do
...
end
# And when you need :selenium and firefox mark the spec with :force_selenium => true
config.before(:each, force_selenium: true) do
Capybara.current_driver = :selenium
end
Upvotes: 3
Views: 1068
Reputation: 16514
These warnings are risen not from webmock
, but from capybara-webkit
itself, and it wishes you to declare the behaviour for access to a host explicitly like follows:
Capybara::Webkit.configure do |config|
config.allow_url("fonts.googleapis.com")
config.allow_url("www.sandbox.paypal.com")
config.allow_url("altfarm.mediaplex.com")
end
Upvotes: 0