Skip to content

A platform for community discussion. Free, open, simple.

DEV: Reuse the Playwright browser context between system specs (#41304)

Alan Guo Xiang Tan9e83a1b30439
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 56d8272465f..b272224e25d 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -54,6 +54,7 @@ jobs:
CAPYBARA_DEFAULT_MAX_WAIT_TIME: 20
MINIO_RUNNER_LOG_LEVEL: WARN
DISCOURSE_TURBO_RSPEC_RETRY_AND_LOG_FLAKY_TESTS: ${{ (matrix.build_type == 'system' || matrix.build_type == 'backend') && '1' }}
+ CAPYBARA_PLAYWRIGHT_SOFT_RESET: ${{ matrix.build_type == 'system' && (matrix.target == 'core' || matrix.target == 'chat') && '1' }}
CHEAP_SOURCE_MAPS: "1"
DBUS_SESSION_BUS_ADDRESS: "unix:path=/dev/null"
MINIO_RUNNER_INSTALL_DIR: /home/discourse/.minio_runner
diff --git a/spec/support/system/capybara_patches.rb b/spec/support/system/capybara_patches.rb
index 76aab5ddc11..d41368301d7 100644
--- a/spec/support/system/capybara_patches.rb
+++ b/spec/support/system/capybara_patches.rb
@@ -187,3 +187,82 @@ module PlaywrightErrorPatch
end
end
Playwright::Error.prepend(PlaywrightErrorPatch)
+
+module PlaywrightSoftReset
+ RESET_STORAGE_TYPES = "all"
+ private_constant :RESET_STORAGE_TYPES
+
+ module Browser
+ def create_browser_context
+ # Each example registering a service worker that the soft reset then
+ # force-clears caused mass spec timeouts on CI. The exact cause requires
+ # further investigation, but system specs currently do not rely on
+ # service workers so blocking them is an acceptable trade-off.
+ @page_options = { serviceWorkers: "block" }.merge(@page_options)
+ @context_downloaded = false
+ super.tap do |browser_context|
+ browser_context.on("download", ->(_download) { @context_downloaded = true })
+ end
+ end
+
+ def soft_reset!
+ contexts = @playwright_browser.contexts
+ return false unless contexts.size == 1
+ return false if @context_downloaded
+
+ context = contexts.first
+ context.pages.each(&:close)
+ new_page = create_page(context)
+ return false if fake_clock_installed?(new_page)
+
+ clear_storage(new_page)
+ context.clear_permissions
+ @playwright_page = new_page.tap(&:bring_to_front)
+ true
+ end
+
+ private
+
+ def fake_clock_installed?(pw_page)
+ !pw_page.evaluate("() => setTimeout.toString()").include?("[native code]")
+ end
+
+ def clear_storage(pw_page)
+ cdp = pw_page.context.new_cdp_session(pw_page)
+ cdp.send_message(
+ "Storage.clearDataForOrigin",
+ params: {
+ origin: "*",
+ storageTypes: RESET_STORAGE_TYPES,
+ },
+ )
+ ensure
+ cdp&.detach
+ end
+ end
+
+ module Driver
+ def reset!
+ return super if !soft_reset_enabled?
+
+ if callback_on_save_screenshot? && (screenshot = @browser&.raw_screenshot)
+ callback_on_save_screenshot(screenshot)
+ end
+
+ return if @browser&.soft_reset!
+
+ @browser&.clear_browser_contexts
+ @browser = nil
+ end
+
+ private
+
+ def soft_reset_enabled?
+ return false if ENV["CAPYBARA_PLAYWRIGHT_SOFT_RESET"] != "1"
+ !(callback_on_save_screenrecord? || @callback_on_save_trace)
+ end
+ end
+end
+
+Capybara::Playwright::Browser.prepend(PlaywrightSoftReset::Browser)
+Capybara::Playwright::Driver.prepend(PlaywrightSoftReset::Driver)
diff --git a/spec/support/test_setup.rb b/spec/support/test_setup.rb
index 10e954d8599..0bb2777a4e1 100644
--- a/spec/support/test_setup.rb
+++ b/spec/support/test_setup.rb
@@ -47,7 +47,10 @@ module TestSetup
# Database is rolled back between specs, but I18n override cache doesn't.
# Flush it if there were any TranslationOverrides created.
overrides_by_site = I18n.instance_variable_get(:@overrides_by_site) || {}
- I18n.reload! if overrides_by_site.values.flat_map(&:values).any?(&:any?)
+ if overrides_by_site.values.flat_map(&:values).any?(&:any?)
+ I18n.reload!
+ ExtraLocalesController.clear_cache!
+ end
RspecErrorTracker.clear_exceptions
diff --git a/spec/system/helpers/downloads.rb b/spec/system/helpers/downloads.rb
index ca4f397f6e4..0bfac805653 100644
--- a/spec/system/helpers/downloads.rb
+++ b/spec/system/helpers/downloads.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Downloads
- FOLDER = Rails.root.join("tmp/downloads")
+ FOLDER = Rails.root.join("tmp/downloads#{ENV["TEST_ENV_NUMBER"]}")
def self.clear
FileUtils.rm_rf(FOLDER)
diff --git a/spec/system/locale_spec.rb b/spec/system/locale_spec.rb
index 79f09fc6999..a579c120e6b 100644
--- a/spec/system/locale_spec.rb
+++ b/spec/system/locale_spec.rb
@@ -122,6 +122,10 @@ RSpec.describe "Locale choice" do
value: "{ count, plural, one {返信 # 件、} other {返信 # 件、} }",
)
overriden_translation_zh_tw.update_columns(value: "{ count, plural, ")
+
+ # update_columns skips the model layer, so burst the bundle digest cache
+ # like TranslationOverride.upsert! would have.
+ ExtraLocalesController.clear_cache!
end
it "works for english" do