Skip to content

Instantly share code, notes, and snippets.

@tychobrailleur
Last active July 11, 2020 17:23
Show Gist options
  • Save tychobrailleur/5712504 to your computer and use it in GitHub Desktop.
Save tychobrailleur/5712504 to your computer and use it in GitHub Desktop.
Capybara/Selenium/Firefox/Proxy.
require 'capybara/firebug'
# Disable Webmock if needed.
WebMock.disable_net_connect!(allow_localhost: true)
# Same thing for VCR if it has been imported somewhere.
VCR.configure do |c|
c.ignore_localhost = true
end
ENV['NO_PROXY'] = ENV['no_proxy'] = '127.0.0.1'
Capybara.app_host = 'http://127.0.0.1:3000'
Capybara.server_port = 3000
Capybara.default_driver = :selenium
Capybara.default_selector = :css
Capybara.default_wait_time = 10
Capybara.register_driver :selenium do |app|
# Use an existing profile
profile = Selenium::WebDriver::Firefox::Profile.new('/path/to/Firefox/Profiles/blahblah.test')
profile.enable_firebug
# Configure proxy manually.
profile["network.proxy.type"] = 1
# Auto-detect proxy = 4
# No proxy = 3
# Use system proxy settings = 5
profile["network.proxy.http"] = 'localhost'
profile["network.proxy.http_port"] = 3128
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
end
# Uncomment to run against standalone server.
#Capybara.run_server = false
Copy link

ghost commented Apr 17, 2020

Is there a way to do basic proxy auth with user/pwd?

@RishiHQ
Copy link

RishiHQ commented Jul 11, 2020

Here's my code that works in July 2020:

  # entrypoint
  def start_capybara_with_proxy(url)
    proxy_address = format(
      '%s:%s',
      BLAZING_SEO_PROXY_HOST,
      BLAZING_SEO_PROXY_PORTS.sample
    )
    proxy = Selenium::WebDriver::Proxy.new(
      http: proxy_address,
      ssl: proxy_address
    )
    start_capybara(url, proxy)
  end

  def start_capybara(url, proxy = nil)
    configure_driver(proxy)
    @driver = Capybara::Session.new(:selenium_headless)
    @driver.visit(url)
  end

  def configure_driver(proxy)
    Selenium::WebDriver::Firefox::Binary.path = '/app/vendor/firefox/firefox' if Rails.env.production?

    Capybara.register_driver :selenium_headless do |app|
      browser_options = Selenium::WebDriver::Firefox::Options.new
      browser_options.args << '--headless' unless ENV['HEADLESS'] == 'false'
      desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(
        proxy: proxy
      )
      Capybara::Selenium::Driver.new(
        app,
        browser: :firefox,
        desired_capabilities: desired_caps,
        options: browser_options
      )
    end
  end

Unfortunately, I realize I need to get proxy working with username and password (like @tchret), which has been a headache.

Does anyone know how to do this on either Chrome/Chromedriver or on Firefox/geckodriver? On Firefox, I believe it involves downloading an extension, and setting username/password through that. I was unable to fill out the popup username/password prompt through Capybara

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment