picture of Josh Dzielak in 2016

Hi! I'm Josh Dzielak, writing here about technology and culture. Find me also on Twitter, Medium, Speakerdeck and Github.

Use an HTTP Proxy with Capybara Mechanize

Hooray for your awesomely fast and invisible capybara-mechanize HTTP-based acceptance tests! Aren’t they great? Well – until they break and you need to know just what in the Helper is going on back there.

Running your traffic through a proxy is a good start to debug this, as part of a process which typically will also involve logic, reasoning, and facepalm.

At least the first step is painless. Setup your capybara proxy as follows from within your spec_helper:

Capybara.configure do |config|
  config.default_driver = :mechanize
  if proxy = ENV['PROXY']
    proxy = URI.parse(proxy)
    Capybara.current_session.driver.browser.agent.set_proxy(proxy.host, proxy.port)

I prefer to pass my intent to proxy on the command line, hence the ENV[‘PROXY’]. But you should do what works best for you.

Note: This differs from the way it used to work, wherein the agent property was on driver, not driver.browser. Thanks to lmmcoy for publishing this general strategy.