Zack
Zack

Reputation: 2497

Ruby Capybara::ExpectationNotMet: Timed out waiting for Selenium session reset Failure/Error

I am continually getting flakey errors on CI where my Selenium session is not restarting. I'm not having this issue when I run tests locally. It only ever happens on my CI server.

I'm using Capybara 2.18.0, rspec 3.7.0, slenium-webdriver 3.9.0 and site_prism 2.13. Is there

Capybara::ExpectationNotMet: Timed out waiting for Selenium session reset
Failure/Error: raise Capybara::ExpectationNotMet.new('Timed out waiting for Selenium session reset') if (Capybara::Helpers.monotonic_time - start_time) >= 10

Capybara::ExpectationNotMet:
  Timed out waiting for Selenium session reset
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara/selenium/driver.rb:145:in `reset!'
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara/session.rb:127:in `reset!'
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/capybara-2.18.0/lib/capybara.rb:314:in `block in reset_sessions!'

my spec_helper.rb looks like this:

require 'rspec'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'site_prism'

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

Capybara.save_path = "#{Dir.pwd}/screenshots"

Capybara.default_driver = :selenium

Capybara.app_host = ENV['url']

if ENV['timeout']
  Capybara.default_max_wait_time = ENV['timeout'].to_i
else
  Capybara.default_max_wait_time = 15
end

RSpec.configure do |config|
  config.full_backtrace = true

  config.before(:each) do
    config.include Capybara::DSL
  end

  config.after(:each) do |example|
    if example.exception
      name = example.example_group.to_s
      name.slice!('RSpec::ExampleGroups::')
      name.gsub!('::', '#')
      whole_page.save_screenshot("#{name}##{example.description.tr(' ', '_')}-#{Time.now.strftime('%H_%M_%S')}.png")
    end
  end
end

Upvotes: 0

Views: 1237

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

When Capybara resets the driver, it tells the browser to go to about:blank and then waits up to 10 seconds for there to be no elements on the page matching CSS '/html/body/*'. In your case that isn't happening within 10 seconds.

One reason for this could be you having onunload handlers that are doing something that could take longer than 10 seconds on your CI hardware (or opening alert messages, etc)?? If that's the case a workaround is to have the test visit a page that doesn't have an onunload handler and check for something visible on that page at the end of the test (potentially in an after block to keep the test clean). Another thing to verify is that the versions of Chrome and chromedriver are the same between local and CI.

Upvotes: 3

Related Questions