mardi 27 janvier 2015

Rails: Use Capybara to browse both local and remote pages in a multi-threaded environment

In a Rails app I'm using Capybara with Poltergeist and PhantomJS to take snapshots of assorted URLs defined by the user, and save those snapshots for later comparison. Because the snapshot execution works in the background using Sidekiq, using the default (global) Capybara session will lead to race condition errors, so I've written a helper method to initialize a new Capybara session for each thread:



def new_capybara_session
# When using Capybara / PhantomJS in a threaded environment like Sidekiq,
# each job needs its own separate session to prevent race conditions and
# the consequent bizarre errors.
Capybara::Session.new(:poltergeist)
end


As long as the snapshot code is set up to use this new session exclusively, I don't run into problems when taking multiple snapshots at the same time as long as the URLs are remote.


However, in my test suite I've exercised this snapshot code by visiting a local URL (ie. from Chrome I saved a few random webpages to /public/test_sites/foo.html and so forth). These tests break; tt turns out my new Capybara session object (intialized using the above method) can't load local URLs, even though it can load remote ones fine. However, if I include Capybara::DSL and use the global / default Capybara session object, I can load both remote and local URLs fine.


Why is this? How can I load local URLs when using a manually created Capybara::Session object? If I can't do this, I can't write thorough tests for the snapshot code.


Aucun commentaire:

Enregistrer un commentaire