Vcrproxy: Record Phantomjs Ajax Calls with Vcr Inside Capybara

VCRProxy: Record PhantomJS ajax calls with VCR inside Capybara

So I did some research and now I have a very basic example of a working VCR proxy server, that handles HTTPS calls as a MITM proxyserver (if you deactivate the security check in your client). I would be very happy if someone could contribute and help me to bring this thing to life.

Here is the github repo: https://github.com/23tux/vcr_proxy

Ruby, record a VCR cassette when clicking link in Cucumber

I used sleep and worked for me.

Given /$A^/ do
VCR.use_cassette 'a_cassette' do
click_link 'a_link' # This makes an AJAX request to external server
sleep 3
end
end

Update

click_link 'a_link'
page.should have_content 'some content'

# some model assertion

With page call Capybara will wait.

vcr with capybara-webkit

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.

How can I use VCR with Rails 5.1 system tests?

I'm assuming you mean Rails 5.1 since Rails 5 doesn't have system tests.

  1. The copy of the application Capybara runs for testing is run in a separate thread, not a separate process. This means they do have access to the same memory, and loaded classes

  2. There is nothing special required for configuring WebMock or VCR beyond what their READMEs already provide

  3. The setup of Capybara and how Rails handles it is irrelevant to the configuration of WebMock or VCR. Additionally, even when using Rails 5.1 system tests all of the normal Capybara configuration options are still usable.

That all being said, there are a couple of things to be aware of here. Firstly, WebMock/VCR can only deal with requests made by your app (not from the browser which you stated you don't need) and it's generally better to use faked services (if possible) rather than WebMock/VCR when doing end to end system tests since there is less interference with the code under test.

If this doesn't answer your issues, post a question with a specific issue you're having, the code that's causing your issue, and the error you're getting.



Related Topics



Leave a reply



Submit