vcr with capybara-webkit

2019-03-12 16:07发布

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?

2条回答
戒情不戒烟
2楼-- · 2019-03-12 16:46

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.

查看更多
走好不送
3楼-- · 2019-03-12 17:02

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
查看更多
登录 后发表回答