Skip to content

Using with VCR or WebMock

Andrew edited this page Sep 6, 2023 · 14 revisions

Update

The solutions here are not working for webdrivers 5.3 because chrome now has 2 "base_url" values, and for chromedriver 115+ it is looking for googlechromelabs.github.io. You will need to add this yourself, or switch to Selenium 4.11 and set the mocks on the DriverFinder class.

Description

You may get this error when using webdrivers with VCR or WebMock gem:

Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver

This is because the request to the following download URLs is being blocked and the driver fails to download:

  • Chromedriver - chromedriver.storage.googleapis.com or googlechromelabs.github.io
  • Geckodriver - github.com/mozilla/geckodriver/releases
  • IEDriverServer - selenium-release.storage.googleapis.com
  • MicrosoftWebDriver - developer.microsoft.com/en-us/microsoft-edge/tools/webdriver

Use the gem specific code below to grab a list of these URLs and add them to the list of ignore_hosts or allowed_sites to unblock them:

VCR

require 'uri'

# With activesupport gem
driver_hosts = Webdrivers::Common.subclasses.map { |driver| URI(driver.base_url).host }


# Without activesupport gem
driver_hosts = (ObjectSpace.each_object(Webdrivers::Common.singleton_class).to_a - [Webdrivers::Common]).map { |driver| URI(driver.base_url).host }

VCR.configure { |config| config.ignore_hosts(*driver_hosts) }

WebMock

# With activesupport gem
driver_urls = Webdrivers::Common.subclasses.map(&:base_url)

# Without activesupport gem
driver_urls = (ObjectSpace.each_object(Webdrivers::Common.singleton_class).to_a - [Webdrivers::Common]).map(&:base_url)

driver_urls << /geckodriver/

# We've seen [a redirect](https://github.com/titusfortner/webdrivers/issues/204) to this domain
driver_urls += ["github-releases.githubusercontent.com"] # this is added

WebMock.disable_net_connect!(allow_localhost: true, allow: driver_urls)

Sources

https://github.com/titusfortner/webdrivers/issues/204

https://github.com/titusfortner/webdrivers/issues/109

https://github.com/titusfortner/webdrivers/issues/66

https://github.com/titusfortner/webdrivers/issues/57

Clone this wiki locally