Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Last active August 12, 2023 01:06
Show Gist options
  • Save Luiyit/fe008aeb03cbea5a299e7f34e0265161 to your computer and use it in GitHub Desktop.
Save Luiyit/fe008aeb03cbea5a299e7f34e0265161 to your computer and use it in GitHub Desktop.
System test with Capybara | Ruby 7 on WSL2

How to fix the error Webdrivers::BrowserNotFound on WSL2

Last update: 11/08/2023.

The error looks like something like this: Failed to determine Chrome binary location. (Webdrivers::BrowserNotFound) Under WSL, we are running the Linux version of Ruby, which won't be able to communicate via WebDriver with the Windows Chrome executable. So, we need to change the default browser driver used by Capybara.

  1. Open the file application_system_test_case.rb inside your rails project, remove the configuration for driven_by and make sure to copy and pase the new driver configuration.
require "test_helper"

WINDOWS_HOST = `cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'`.strip
CHROMEDRIVER_URL = "http://#{WINDOWS_HOST}:9515/"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium_remote_chrome

  Capybara.register_driver :selenium_remote_chrome do |app|
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument('--start-maximized')

    Capybara::Selenium::Driver.new(
      app,
      browser: :remote,
      url: CHROMEDRIVER_URL,
      options: options
    )
  end

  Capybara.configure do |config|
    # Match what's set for URL options in test.rb so we
    # can test mailers that contain links.
    config.server_host = 'localhost'
    config.server_port = '3000'
  end

end
  1. Go to ChromeDriver and download the last version of the driver. On my case I download de version 114.0.5735.90 for win32 => chromedriver_win32.zip
  2. Now, Rename it from chromedriver.exe to chromedriver. move it to a folder that you already have in the path system like c:/windows. You can also move it to another folder, but fon't forget to add it to the path system.
  3. Open a WSL console and try to run this: chromedriver -v. You should be able to use the driver from WSL
ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052})
  1. Now it time to start the ChromeDriver. Run: chromedriver --allowed-ips
Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 9515
All remote connections are allowed. Use an allowlist instead!
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

At this point you should be able to run your system test rails test:system.

Side Notes

  1. Add a Windows firewall rule to allow incoming connections for port 9515 (only if you need it).
  2. If the WINDOWS_HOST is wrong, you can replace this line by your local IP.

This instructions is a sumary of this discutions.

  1. Can't run Rails system test on WSL Ubuntu 18.
  2. System Tests With Capybara and Selenium | Ruby On Rails 7 Tutorial.
  3. How do I specify the path to chrome.exe....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment