capybara
capybara copied to clipboard
Reap invalid session
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_sessionwill 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