Skip to content

Commit 068f45f

Browse files
committed
keep browser cache between tests
1 parent 0ec392d commit 068f45f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎spec/support/capybara.rb‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ def call(message)
5252
end
5353
Ferrum::Client::Subscriber.prepend(FerrumSubscriberRescue)
5454

55+
# Cuprite's reset disposes the browser context, and Chrome's HTTP cache with it, so every example
56+
# downloaded the 100+ modules of a map page again. Swap only the page instead: a new page drops
57+
# its request handlers (CapybaraMock), headers and scripts, while the context keeps the cache.
58+
# Cookies and storage live in the context, so clear them explicitly.
59+
module CupriteKeepCacheOnReset
60+
STORAGE_TYPES = "local_storage,session_storage,indexeddb,service_workers,cache_storage"
61+
62+
def reset
63+
return super unless reusable_context?
64+
65+
old_page = @page
66+
origin = targets[old_page.target_id].url.to_s[%r{\Ahttps?://[^/]+}]
67+
old_page.command("Storage.clearDataForOrigin", origin:, storageTypes: STORAGE_TYPES) if origin
68+
command("Storage.clearCookies", browserContextId: default_context.id)
69+
@options.reset_window_size
70+
@page = attach_page(default_context.create_target.id)
71+
old_page.close
72+
end
73+
74+
private
75+
76+
def reusable_context?
77+
@page.is_a?(Ferrum::Page) && targets[@page.target_id] && targets.values.count(&:page?) == 1
78+
end
79+
end
80+
Capybara::Cuprite::Browser.prepend(CupriteKeepCacheOnReset)
81+
5582
# https://github.com/rubycdp/cuprite
5683
Capybara.javascript_driver = :cuprite
5784

0 commit comments

Comments
 (0)