Skip to content

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

DEV: Use CDP to override browser timezone in system specs (#36880)

Régis Hanol0c6aee87afb2
diff --git a/plugins/discourse-calendar/spec/system/post_event_spec.rb b/plugins/discourse-calendar/spec/system/post_event_spec.rb
index d58dd9b6db4..4a0c3ad7f9a 100644
--- a/plugins/discourse-calendar/spec/system/post_event_spec.rb
+++ b/plugins/discourse-calendar/spec/system/post_event_spec.rb
@@ -254,8 +254,8 @@ describe "Post event", type: :system do
sign_in(viewer)
- # Install Playwright clock **after** sign_in (which may navigate) but before
- # visiting the topic, so moment() calls use the frozen time
+ # Freeze browser clock **after** sign_in (which may navigate/reset state)
+ # so moment() uses the same time as the server.
page.driver.with_playwright_page { |pw_page| pw_page.clock.install(time:) }
visit(post.topic.url)
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index f51283831e3..77b39f9aca4 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -814,7 +814,9 @@ RSpec.configure do |config|
slowMo: ENV["PLAYWRIGHT_SLOW_MO_MS"].to_i, # https://playwright.dev/docs/api/class-browsertype#browser-type-launch-option-slow-mo
playwright_cli_executable_path: "./node_modules/.bin/playwright",
logger: Logger.new(IO::NULL),
- timezoneId: example.metadata[:timezone],
+ # NOTE: timezoneId is NOT set here because the driver is cached and reused,
+ # so only the first test's timezone would be applied. Instead, we use CDP
+ # to override the timezone per-test in the before(:each) hook below.
colorScheme: example.metadata[:color_scheme],
}
@@ -880,6 +882,15 @@ RSpec.configure do |config|
page.driver.with_playwright_page do |pw_page|
$playwright_logger = PlaywrightLogger.new(pw_page)
+
+ # Apply timezone override via CDP if timezone metadata is present.
+ # We use CDP instead of the driver's timezoneId option because the driver
+ # instance is cached and reused between tests, so timezoneId only affects
+ # the first test. CDP override works at runtime for each test.
+ if (tz = example.metadata[:timezone])
+ cdp = pw_page.context.new_cdp_session(pw_page)
+ cdp.send_message("Emulation.setTimezoneOverride", params: { timezoneId: tz })
+ end
end
end