1. ホーム
  2. ruby-on-rails

[解決済み] Selenium WebDriver エラー: 無効なセッション ID

2022-02-19 21:32:37

質問

Selenium WebDriverとChromeを使って、アプリの機能テストを行っています。すでにrspec、capybara、seleniumで複数のrailsアプリのセットアップに成功していますが、なぜこのアプリがうまくいかないのかがわかりません。

ここで問題なのは、初めて機能テストを実行したとき、計画通りに動作することです。しかし、2回目に実行すると、seleniumは次のようなメッセージを出してクラッシュします。

Selenium::WebDriver::Error::NoSuchDriverError: 無効なセッションID (ドライバ情報: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.3 x86_64))。

機能テストは他のアプリでも完璧に動作するので、これはChromeとchromedriverの互換性の問題ではないことを付け加えておきます。

もう何日もこの作業をしていますが、何が起こっているのかがわかりません

以下は私のコンフィグです。

  • selenium-webdriver (3.141.0)
  • カピバラ (2.7.1)
  • Chrome 74とchromedriver 74.0.3729.6

support/capyabara.rb

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.register_driver :headless_chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w(headless disable-gpu) }
  )

  Capybara::Selenium::Driver.new app,
    browser: :chrome,
    desired_capabilities: capabilities
end

Capybara.javascript_driver = :headless_chrome
Capybara.default_max_wait_time = 15

rails_helper.rb

# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
require File.expand_path("../../config/environment", __FILE__)

require "spec_helper"
require "rspec/rails"
require 'capybara/rails'
require 'capybara/rspec'
require 'selenium-webdriver'
# https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
require "devise"

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false

  config.include Devise::TestHelpers, type: :controller

  config.infer_spec_type_from_file_location!

  # Only retry when Selenium raises Net::ReadTimeout
  config.exceptions_to_retry = [Net::ReadTimeout]
end

support/database_cleander.rb

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with :truncation

    # Seed database
    load "#{Rails.root}/db/seeds.rb"
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, type: :feature) do
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

    if !driver_shares_db_connection_with_specs
      DatabaseCleaner.strategy = :truncation
    end
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end
end


スタックトレース

# ./spec/features/test_spec.rb:8:in `block (2 levels) in <top (required)>'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `new'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `create_response'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/default.rb:104:in `request'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:62:in `call'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/bridge.rb:166:in `execute'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/oss/bridge.rb:584:in `execute'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/oss/bridge.rb:299:in `delete_all_cookies'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/options.rb:81:in `delete_all_cookies'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/capybara-2.7.1/lib/capybara/selenium/driver.rb:102:in `reset!'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:109:in `reset!'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/capybara-2.7.1/lib/capybara.rb:331:in `block in reset_sessions!'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/capybara-2.7.1/lib/capybara.rb:331:in `reverse_each'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/capybara-2.7.1/lib/capybara.rb:331:in `reset_sessions!'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/capybara-2.7.1/lib/capybara/rspec.rb:21:in `block (2 levels) in <top (required)>'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:357:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:507:in `block in run_owned_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:506:in `each'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:506:in `run_owned_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:593:in `block in run_example_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:592:in `each'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:592:in `run_example_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:463:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:507:in `run_after_example'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:273:in `block in run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:500:in `block in with_around_and_singleton_context_hooks'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `block in run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:604:in `block in run_around_example_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-rails-3.6.0/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:606:in `block (2 levels) in run_around_example_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:123:in `block in run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:110:in `loop'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:110:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-retry-0.6.1/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-retry-0.6.1/lib/rspec/retry.rb:37:in `block (2 levels) in setup'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:606:in `block (2 levels) in run_around_example_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:607:in `run_around_example_hooks_for'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:251:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:627:in `block in run_examples'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `map'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `run_examples'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:589:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `map'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/configuration.rb:1894:in `with_suite_hooks'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/reporter.rb:79:in `report'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:112:in `run_specs'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:87:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:71:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:45:in `invoke'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/rspec-core-3.6.0/exe/rspec:4:in `<top (required)>'
# /Users/vincentrolea/.rbenv/versions/2.3.7/bin/rspec:22:in `load'
# /Users/vincentrolea/.rbenv/versions/2.3.7/bin/rspec:22:in `<top (required)>'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `load'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `kernel_load'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:28:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/cli.rb:463:in `exec'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/cli.rb:27:in `dispatch'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/cli.rb:18:in `start'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/exe/bundle:30:in `block in <top (required)>'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
# /Users/vincentrolea/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/bundler-1.17.3/exe/bundle:22:in `<top (required)>'
# /Users/vincentrolea/.rbenv/versions/2.3.7/bin/bundle:22:in `load'

EDIT

Chromedriverはテスト中に同じセッションIDを何度も何度も再利用しています。このため、最初のテストは成功しましたが、再度開始すると、chromedriverはまったく同じセッションIDを使用し、これが問題の原因だと思われます。私はSeleniumのアクションを記録し、それはDELETEを送信するので、私は理解していない。

2019-05-10 08:24:53 INFO Selenium -> POST session
2019-05-10 08:24:53 INFO Selenium    >>> http://127.0.0.1:9517/session | {"desiredCapabilities":{"browserName":"chrome","version":"","platform":"ANY","javascriptEnabled":true,"cssSelectorsEnabled":true,"takesScreenshot":false,"nativeEvents":false,"rotatable":false},"capabilities":{"firstMatch":[{"browserName":"chrome"}]}}
2019-05-10 08:24:53 INFO Selenium <- {"sessionId":"b8a41525be337e1a029cf574285d2031","status":0,"value":{"acceptInsecureCerts":false,"acceptSslCerts":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29})","userDataDir":"/var/folders/_x/xj6qbw1n30z7pr18t52y1yjh0000gn/T/.com.google.Chrome.IyCrBu"},"cssSelectorsEnabled":true,"databaseEnabled":false,"goog:chromeOptions":{"debuggerAddress":"localhost:64888"},"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platform":"Mac OS X","proxy":{},"rotatable":false,"setWindowRect":true,"strictFileInteractability":false,"takesHeapSnapshot":true,"takesScreenshot":true,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unexpectedAlertBehaviour":"ignore","version":"74.0.3729.131","webStorageEnabled":true}}
2019-05-10 08:24:53 INFO Selenium Detected OSS dialect.
2019-05-10 08:24:53 INFO Selenium -> POST session/b8a41525be337e1a029cf574285d2031/url
2019-05-10 08:24:53 INFO Selenium    >>> http://127.0.0.1:9517/session/b8a41525be337e1a029cf574285d2031/url | {"url":"http://127.0.0.1:50711/"}
2019-05-10 08:24:53 INFO Selenium <- {"sessionId":"b8a41525be337e1a029cf574285d2031","status":6,"value":{"message":"invalid session id\n  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.3 x86_64)"}}
2019-05-10 08:24:53 INFO Selenium -> DELETE session/b8a41525be337e1a029cf574285d2031/cookie
2019-05-10 08:24:53 INFO Selenium <- {"sessionId":"b8a41525be337e1a029cf574285d2031","status":0,"value":null}
2019-05-10 08:24:53 INFO Selenium -> POST session/b8a41525be337e1a029cf574285d2031/url
2019-05-10 08:24:53 INFO Selenium    >>> http://127.0.0.1:9517/session/b8a41525be337e1a029cf574285d2031/url | {"url":"about:blank"}
2019-05-10 08:24:53 INFO Selenium <- {"sessionId":"b8a41525be337e1a029cf574285d2031","status":0,"value":null}
2019-05-10 08:24:53 INFO Selenium -> POST session/b8a41525be337e1a029cf574285d2031/elements
2019-05-10 08:24:53 INFO Selenium    >>> http://127.0.0.1:9517/session/b8a41525be337e1a029cf574285d2031/elements | {"using":"xpath","value":"/html/body/*"}
2019-05-10 08:24:53 INFO Selenium <- {"sessionId":"b8a41525be337e1a029cf574285d2031","status":0,"value":[]}
  User log in (FAILED - 1)

Failures:

  1) Test Feature User log in
     Failure/Error: visit root_path

     Selenium::WebDriver::Error::NoSuchDriverError:
       invalid session id
         (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.3 x86_64)
     # ./spec/features/test_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 4.85 seconds (files took 11.27 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/test_spec.rb:4 # Test Feature User log in

2019-05-10 08:24:53 INFO Selenium -> DELETE session/b8a41525be337e1a029cf574285d2031
2019-05-10 08:24:53 INFO Selenium <- {"sessionId":"b8a41525be337e1a029cf574285d2031","status":0,"value":null}

解決方法は?

何が問題だったのかがわかりました。

私たちは、テスト中のAPIのレスポンスを、初めて作成された後にキャッシュするためのライブラリを使っています。

問題は、ドライバがセッションIDを取得するためのリクエストを送信することです。このため、初回のテストは合格していました。ライブラリは、毎回同じセッションIDを持つドライバからの応答をキャッシュし、その結果、クロームからエラーが発生しました。

もしこのようなライブラリ(VCR、ephemeral_response)を使っていて、同様のエラーが発生する場合は、次のことを調査してみるとよいかもしれません。