Skip to content

Instantly share code, notes, and snippets.

@alessandrostein
Last active September 30, 2022 04:56
Show Gist options
  • Save alessandrostein/2a0ba84387555d2ef0b7a742f45bc6b3 to your computer and use it in GitHub Desktop.
Save alessandrostein/2a0ba84387555d2ef0b7a742f45bc6b3 to your computer and use it in GitHub Desktop.
Selenium::WebDriver::Error::UnknownError: unknown error: Chrome failed to start: crashed (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /app/.apt/opt/google/chrome/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

Ruby on Rails Project

Rails 5.2.3 ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

Using webdrivers and capybara

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
  gem 'faker'
  gem 'guard-minitest'
  gem 'webdrivers'
end

Heroku CI environment

"environments": {
    "test": {
      "buildpacks": [{
          "url": "heroku/ruby"
        },
        {
          "url": "heroku/nodejs"
        },
        {
          "url": "heroku-community/apt"
        },
        {
          "url": "https://github.com/heroku/heroku-buildpack-chromedriver"
        },
        {
          "url": "https://github.com/heroku/heroku-buildpack-google-chrome"
        }
      ],
      "addons": [
        "heroku-redis:in-dyno",
        "heroku-postgresql:in-dyno"
      ],
      "scripts": {
        "test-setup": "bin/rails assets:precompile",
        "test": "rake"
      }
    }
  }

Capybara and Webdrivers settings

require 'webdrivers'
require 'capybara/rails'
require 'capybara/minitest'

Capybara.server = :puma, { Silent: true }
Capybara.server_port = 55_707
Capybara.default_max_wait_time = 10

Webdrivers.cache_time = 86_400
Webdrivers.logger.level = :DEBUG

Selenium::WebDriver::Chrome.path = ENV.fetch('GOOGLE_CHROME_BIN', nil)

chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)

chrome_opts = chrome_bin ? { 'chromeOptions' => { 'binary' => chrome_bin }, 'args' => ['--headless', '--no-sandbox', '--disable-gpu', '--remote-debugging-port=9222'] } : {}
Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chrome_opts)
  )
end

Capybara.javascript_driver = :chrome

class ActionDispatch::SystemTestCase
  # Make the Capybara DSL available in all integration tests
  include Capybara::DSL
  # Make `assert_*` methods behave like Minitest assertions
  include Capybara::Minitest::Assertions

  driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400] unless ENV['CI']

  # Reset sessions and driver between tests
  teardown do
    Capybara.reset_sessions!
    Capybara.use_default_driver
  end
end

Running

# Running:
2019-06-12 20:16:20 DEBUG Webdrivers Checking current version
2019-06-12 20:16:20 DEBUG Webdrivers /app/.webdrivers/chromedriver is not already downloaded
2019-06-12 20:16:20 DEBUG Webdrivers making System call: /app/.apt/opt/google/chrome/chrome --product-version
2019-06-12 20:16:20 DEBUG Webdrivers Browser version: 75.0.3770.80
2019-06-12 20:16:20 DEBUG Webdrivers making System call: /app/.apt/opt/google/chrome/chrome --product-version
2019-06-12 20:16:20 DEBUG Webdrivers Browser version: 75.0.3770.80
2019-06-12 20:16:20 DEBUG Webdrivers Making network call to https://chromedriver.storage.googleapis.com/LATEST_RELEASE_75.0.3770
2019-06-12 20:16:21 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-06-12 20:16:21 DEBUG Webdrivers Latest version available: 75.0.3770.8
2019-06-12 20:16:21 DEBUG Webdrivers Deleting /app/.webdrivers/chromedriver.version
2019-06-12 20:16:21 DEBUG Webdrivers Deleting /app/.webdrivers/chromedriver
2019-06-12 20:16:21 DEBUG Webdrivers making System call: /app/.apt/opt/google/chrome/chrome --product-version
2019-06-12 20:16:21 DEBUG Webdrivers Browser version: 75.0.3770.80
2019-06-12 20:16:21 DEBUG Webdrivers making System call: /app/.apt/opt/google/chrome/chrome --product-version
2019-06-12 20:16:21 DEBUG Webdrivers Browser version: 75.0.3770.80
2019-06-12 20:16:21 DEBUG Webdrivers Making network call to https://chromedriver.storage.googleapis.com/LATEST_RELEASE_75.0.3770
2019-06-12 20:16:21 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-06-12 20:16:21 DEBUG Webdrivers Latest version available: 75.0.3770.8
2019-06-12 20:16:21 DEBUG Webdrivers chromedriver URL: https://chromedriver.storage.googleapis.com/75.0.3770.8/chromedriver_linux64.zip
2019-06-12 20:16:21 DEBUG Webdrivers Making network call to https://chromedriver.storage.googleapis.com/75.0.3770.8/chromedriver_linux64.zip
2019-06-12 20:16:21 DEBUG Webdrivers Get response: #<Net::HTTPOK 200 OK readbody=true>
2019-06-12 20:16:21 DEBUG Webdrivers Successfully downloaded /tmp/20190612-7728-42bkkfchromedriver_linux64.zip
2019-06-12 20:16:21 DEBUG Webdrivers Decompressing /tmp/20190612-7728-42bkkfchromedriver_linux64.zip
2019-06-12 20:16:21 DEBUG Webdrivers Deleting /app/.webdrivers/chromedriver
2019-06-12 20:16:21 DEBUG Webdrivers Completed download and processing of /app/.webdrivers/chromedriver
2019-06-12 20:16:22 DEBUG Webdrivers Checking current version
2019-06-12 20:16:22 DEBUG Webdrivers /app/.webdrivers/chromedriver is already downloaded
2019-06-12 20:16:22 DEBUG Webdrivers making System call: /app/.webdrivers/chromedriver --version
2019-06-12 20:16:22 DEBUG Webdrivers Current version of /app/.webdrivers/chromedriver is ChromeDriver 75.0.3770.8 (681f24ea911fe754973dda2fdc6d2a2e159dd300-refs/branch-heads/3770@{#40})
2019-06-12 20:16:22 DEBUG Webdrivers A working webdriver version is already on the system
2019-06-12 20:16:22 DEBUG Webdrivers Checking current version
2019-06-12 20:16:22 DEBUG Webdrivers /app/.webdrivers/chromedriver is already downloaded
2019-06-12 20:16:22 DEBUG Webdrivers making System call: /app/.webdrivers/chromedriver --version
2019-06-12 20:16:22 DEBUG Webdrivers Current version of /app/.webdrivers/chromedriver is ChromeDriver 75.0.3770.8 (681f24ea911fe754973dda2fdc6d2a2e159dd300-refs/branch-heads/3770@{#40})
2019-06-12 20:16:22 DEBUG Webdrivers A working webdriver version is already on the system

The error

Selenium::WebDriver::Error::UnknownError: unknown error: Chrome failed to start: crashed
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /app/.apt/opt/google/chrome/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
    test/system/my/profile_test.rb:19:in `block in <class:ProfileTest>'
@PaulineTW
Copy link

@alessandrostein did you ever find the solution?
I have the same issue when i run bin/rails test:system

@alessandrostein
Copy link
Author

alessandrostein commented Sep 29, 2022

@alessandrostein did you ever find the solution? I have the same issue when i run bin/rails test:system

Hi @PaulineTW, I don't remember how I solved this in the past, but I can try to help you ๐Ÿ‘‹

What about adding Capybara.default_driver = :selenium_chrome_headless in the top of your file?

# capybara.rb

require 'webdrivers'
require 'capybara/rails'
require 'capybara/minitest'

Capybara.server = :puma, { Silent: true }
Capybara.server_port = 55_707
Capybara.default_max_wait_time = 10
Capybara.default_driver = :selenium_chrome_headless
Webdrivers.cache_time = 86_400

Also, make sure the capybara and webdrivers gems are updated ๐Ÿ‘

@PaulineTW
Copy link

PaulineTW commented Sep 30, 2022

Thank you so much for your reply @alessandrostein i appreciate.
I have actually get it fixed with the below

# application_system_test_case.rb

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  # Change :chrome with :headless_chrome
  Capybara.configure do |config|
    config.always_include_port = true
  end

  driven_by :selenium, using: :headless_chrome do |option|
    option.add_argument "no-sandbox"
  end
end

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