How to Use a Proxy With Capybara

How to Use a Proxy With Capybara

Capybara is one of my favorite libraries for simulating user actions on a website. It’s perfect for automating tests that would be tiring to do by hand. But sometimes, we need extra help to fully test a site. For example, you might want to see how the site looks in another country, manage rate limits, or hide your IP for privacy. That’s when using a proxy can help.

Setting up a proxy with Capybara isn’t tricky. It can make your testing much more flexible. Here, I’ll walk you through easy steps to set up and use a proxy with Capybara. This will help you take your testing to the next level.

Learn how to connect a proxy with the Capybara library. Easy!

Why Use a Proxy in Capybara?

Proxies are intermediaries between your test script and the web server, and they offer several advantages:

  1. Geolocation Testing: Simulate access from different countries or regions.
  2. Bypassing Restrictions: Bypass IP-based restrictions or rate-limits imposed on certain websites.
  3. Anonymity: Hide your real IP address during testing to avoid being flagged as a bot.
  4. Monitoring Traffic: Log and inspect HTTP requests for debugging purposes.

Recommended Residential Proxy Services

  • Bright Data — Largest provider, precise targeting, Proxy Manager tool, starting at $5.04/GB
  • Oxylabs — Extensive network, precise targeting, dedicated support, starting at $4/GB
  • Smartproxy — Large pool, broad locations, self-service, starting at $2.2/GB
  • Webshare — Customization options, self-service, affordable, starting at $4.5/GB
  • SOAX — Flexible rotation, precise targeting, 24/7 support, starting at $2.2/GB

I am not affiliated with any of the providers mentioned above.

Prerequisites

Before setting up a proxy with Capybara, make sure you have the following:

  • Ruby installed: Capybara runs on Ruby, so ensure you have the latest version installed.
  • Capybara: The core library used for web testing. It can be installed with the following command:
gem install capybara
  • A proxy server: You can use free or paid proxy services (like hte ones I mentioned above) depending on your needs. Make sure you have the proxy IP, port, and, if needed, authentication details (username and password).

Proxy Setup in Capybara

Capybara interacts with web drivers such as Selenium, RackTest, and others. However, to use a proxy, we need to configure the driver accordingly.

Step 1: Install the Required Web Driver

To use a proxy, Selenium is the most suitable driver. If you don’t already have Selenium installed, you can do so by running:

gem install selenium-webdriver

Step 2: Configure Capybara to Use Selenium

Next, you need to configure Capybara to use Selenium with the proxy. You can define your settings directly in your test setup or configuration file. Here’s an example using Chrome as the browser:

require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
Capybara.register_driver :selenium_with_proxy do |app|
proxy = Selenium::WebDriver::Proxy.new(
http: 'your-proxy-address:your-port',
ssl: 'your-proxy-address:your-port'
)
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
proxy: proxy
)
options = Selenium::WebDriver::Chrome::Options.new
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: [capabilities], options: options)
end
Capybara.default_driver = :selenium_with_proxy

This setup configures Capybara to use Selenium with Chrome, and the HTTP and SSL proxy settings are provided. Replace ‘your-proxy-address’ and ‘your-port’ with your proxy server details.

Step 3: Handling Proxy Authentication

Some proxies require authentication (username and password). You can handle this by configuring the Selenium WebDriver capabilities as shown below:

proxy = Selenium::WebDriver::Proxy.new(
http: 'username:password@your-proxy-address:your-port',
ssl: 'username:password@your-proxy-address:your-port'
)
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
proxy: proxy
)

In this setup, you include the username and password directly in the proxy string. Be cautious when using sensitive information like credentials in your code. Consider using environment variables to store credentials securely.

Step 4: Using the Proxy in Tests

Once the proxy setup is complete, you can start writing your Capybara tests as usual. For example:

describe 'Proxy Test', type: :feature do
it 'browses through a proxy' do
visit 'http://example.com'
expect(page).to have_content('Example Domain')
end
end

This test will now use the proxy settings configured in the driver. The traffic will pass through the proxy server, allowing you to perform tests from a different IP address.

Proxy Settings for Firefox

If you prefer using Firefox over Chrome, here’s how you can modify the configuration:

Capybara.register_driver :selenium_with_proxy do |app|
proxy = Selenium::WebDriver::Proxy.new(
http: 'your-proxy-address:your-port',
ssl: 'your-proxy-address:your-port'
)
options = Selenium::WebDriver::Firefox::Options.new
options.add_argument(' - proxy-server=your-proxy-address:your-port')
Capybara::Selenium::Driver.new(app, browser: :firefox, options: options)
end
Capybara.default_driver = :selenium_with_proxy

The structure is similar to the Chrome setup, but the options are tailored for Firefox.

Troubleshooting Tips

  • Proxy connection errors: Double-check your proxy address, port, and authentication details.
  • Selenium WebDriver updates: Keep Selenium and Capybara libraries up to date, as web driver APIs can change.
  • Performance issues: Using a proxy may slow down your tests, especially if the proxy server has high latency.

Conclusion

Using a proxy with Capybara gives me more flexibility when testing websites. Whether I need to access content specific to a particular region, circumvent restrictions, or just add a bit of privacy, setting up a proxy improves my testing process. Following the earlier steps, I can easily add a proxy to my Capybara tests using Selenium.

Similar Posts