Created
May 3, 2020 01:11
-
-
Save dugancathal/7ab4417b567001c9c434614dec7c5f60 to your computer and use it in GitHub Desktop.
Had a project where I was developing on both Windows Subsystem for Linux & a Mac. On the windows box, make sure you setup chromedriver and set the port to 9515 (should be the default)
This file contains hidden or 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
module CapybaraJsDriverSetup | |
module_function | |
def is_running_on_windows_subsystem_for_linux? | |
if which('cmd.exe') | |
os_version = `cmd.exe /c "systeminfo" | grep "^OS Version"` | |
!os_version.chomp.empty? | |
end | |
end | |
def create_wsl_driver | |
Capybara.register_driver :windows_chrome do |app| | |
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( | |
'goog:chromeOptions': { | |
args: %w(no-sandbox headless disable-gpu), | |
}, | |
) | |
Capybara::Selenium::Driver.new( | |
app, | |
browser: :chrome, | |
url: 'http://localhost:9515', | |
desired_capabilities: capabilities, | |
) | |
end | |
end | |
def which(cmd) | |
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] | |
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| | |
exts.each do |extension| | |
exe = File.join(path, "#{cmd}#{extension}") | |
return exe if File.executable?(exe) && !File.directory?(exe) | |
end | |
end | |
end | |
def setup_driver | |
driver = if is_running_on_windows_subsystem_for_linux? | |
require 'selenium-webdriver' | |
create_wsl_driver | |
:windows_chrome | |
else | |
require 'capybara/apparition' | |
:apparition | |
end | |
Capybara.javascript_driver = driver | |
end | |
end | |
CapybaraJsDriverSetup.setup_driver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment