vcr with capybara-webkit
Asked Answered
T

2

14

I'm using capybara-webkit to test integration with a third party website (I need javascript).

I want to use vcr to record requests made during the integration test but capybara-webkit doesn't go over net http so vcr is unable to record them. How would I go about writing an adaptor for vcr that would allow me to record the reqeusts?

Trigraph answered 26/6, 2012 at 19:13 Comment(0)
C
15

Unfortunately, VCR is very much incompatible with capybara-webkit. The fact is that capybara webkit is using webkit, which is in c. Webmock and Fakeweb, which are the basis for VCR, can only be used for Ruby web requests. Making the two work together would likely be a monumental task.

I've solved this problem two ways:

The first (hacky, but valid) is to add a new javascript file to the application that is only included in the test environment. This file stubs out the JS classes which make external web requests. Aside from the pure hackatude of this approach, it requires that every time a new request is added or changed you must change the stubs as well.

The second approach is to route all external requests through my own server, effectively proxying all external requests through my server. This has the huge disadvantage that you have to have an action for everything you want to consume (you could genericize it, with some work). It also suffers from the fact that it could as much as double the time for the request to complete. However, since the requests are now being made by Ruby you can use VCR in all it's glory.

In my situations, approach #2 has been much more to my advantage thanks to the fact that I need ruby to manipulate the data so that I can keep my javascript source-agnostic. I was, however, using approach #1 for quite a while successfully.

Charcot answered 26/6, 2012 at 19:57 Comment(9)
You can run a general-purpose proxy server pretty easily. I couldn't find many in Ruby. I'd suggest mousehole if you wanted to stick in the Ruby ecosystem.Raman
Right, you can. We threw that out because of the OS dependencies and configuration nightmare we saw coming with that approach.Charcot
what sort of difficulties did you encounter with proxy-server based analysis? Just having to change proxy settings on the box running capybara-webkit?Raman
Our process involves all of our dev machines running tests as well as a continuous integration server. In other words, we would have to deal with configuration on at least 8 machines and 2 operating systems. We threw it out because we didn't want to add (yet another) OS level dependency to our application development stack, more specifically, we didn't want to have to configure and maintain it. There were some ancillary reasons, but that was the crux of it. So to say that we had problems is inaccurate, we honestly never even tried it.Charcot
FWIW, I've been thinking of building a VCR proxy server for a while. Please comment on github.com/myronmarston/vcr/issues/187 if you want to see this.Feverish
Would you happen to have a sample code for the first method? I want to implement this for Cucumber. I'd like to get an idea of how you would inject the file on a per test basis... I know there are Before hooks, but those are run before the page is even loaded, right...?Pocosin
In order to make #1 work reliably I actually had to include a new JS file in the app, that is ignored in all other environments. Trying to use before hooks never worked cleanly, because the whole page loads and all the javascript executes before you have a chance to stub it all out.Charcot
@MyronMarston whatever happened with the vcr proxy server? That sounds like a great approach.Mackenziemackerel
@PeterP.: nothing. No one has stepped up to build it. I'm also not involved in VCR anymore.Feverish
Z
10

I've written a small ruby library (puffing-billy) for rspec+capybara that does exactly this -- it injects a proxy in between your browser and the outside world and allows you to fake responses to specific requests.

Example:

describe 'fetching badges from stackoverflow API' do
  it 'should show a nice message when you have no badges' do
    # stub some JSONP
    proxy.stub('http://api.stackoverflow.com/1.1/users/1/badges',
               :jsonp => { :badges => [] })

    visit '/my_badges'
    page.should have_content("You don't have any badges :(")
  end
end
Zeba answered 15/10, 2012 at 14:31 Comment(1)
Ha, yeah, I saw you mention it on the vcr thread, looks very intersted.Trigraph

© 2022 - 2024 — McMap. All rights reserved.