Last active
July 11, 2020 17:23
-
-
Save tychobrailleur/5712504 to your computer and use it in GitHub Desktop.
Capybara/Selenium/Firefox/Proxy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
It changed
profile = Selenium::WebDriver::Firefox::Profile.new
proxy = Selenium::WebDriver::Proxy.new(http: "proxy.org:8080")
profile.proxy = proxy
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
driver = Selenium::WebDriver.for :firefox, options: options
https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
(Edit: this and variations of this doesn't work, moving back to the also buggy chrome-driver)
Is there a way to do basic proxy auth with user/pwd?
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
I did as follows
#my requires
require 'capybara'
require 'byebug' #for debug as needed
require 'browsermob/proxy'
require 'selenium/webdriver'
require 'chromedriver/helper'
#I turn on the use of proxy via environment variable
@proxy_on = ENV['USEPROXY'] || "false"
if @proxy_on == "true"
server = BrowserMob::Proxy::Server.new("/tools/browsermob-proxy-2.1.6-SNAPSHOT/bin/browsermob-proxy")
server.start
proxy = server.create_proxy
@selenium_proxy = proxy.selenium_proxy(:ssl, :http)
@proxy_server = server
@Proxy = proxy
end
Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--ignore-certificate-errors")
options.add_argument("--allow-insecure-localhost")
caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => @selenium_proxy)
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options, :desired_capabilities => caps)
end