Skip to content

Reap invalid session #2799

@tientt-holistics

Description

@tientt-holistics

Context

  • I am using Capybara + Rails + RSpec for end to end testing
  • Some of my tests could cause the browser to crash on CI
    • Because of limited resources, I am unable to increase the memory size on the CI environment

Problem

  • If test crashes the browser, all subsequent retry or run other tests will fail.
  • Is there a safe way to reopen the browse in my case?

Workaround

  • I am implementing the below monkey-patch on Capybara#reset!.
  • It will remove any invalid session. During the next test run, calling Capybara.current_session will run a new browser.
  • Are there any problems to this?
module Capybara
    class << self
        # MONKEY PATCH this method to reap invalid session in case of browser crashed
        # This allows subsequent test run/retry success instead of throw out invalid session id
        def reset_sessions!
          # reset in reverse so sessions that started servers are reset last
          invalid_keys = []
          session_pool.reverse_each do |key, session|
            session.reset!
          rescue => e
            puts "Encouter error while reseting session with key #{key} #{e}"
            invalid_keys << key
          end
          invalid_keys.each { |k| session_pool.delete(k) }
        end
      end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions