From 9cb44d5130786026fe8d67f6b12f759d49909f0b Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 13 Feb 2025 15:40:23 -0500 Subject: [PATCH 1/3] [dotnet] Add SystemClock singleton --- dotnet/src/support/UI/PopupWindowFinder.cs | 2 +- .../support/UI/SlowLoadableComponent{T}.cs | 2 +- .../src/webdriver/Support/DefaultWait{T}.cs | 2 +- dotnet/src/webdriver/Support/SystemClock.cs | 5 ++++ dotnet/src/webdriver/Support/WebDriverWait.cs | 2 +- .../UI/{FakeClock.cs => HandCrankClock.cs} | 15 +++--------- .../support/UI/SlowLoadableComponentTest.cs | 24 ++++++++----------- 7 files changed, 22 insertions(+), 30 deletions(-) rename dotnet/test/support/UI/{FakeClock.cs => HandCrankClock.cs} (85%) diff --git a/dotnet/src/support/UI/PopupWindowFinder.cs b/dotnet/src/support/UI/PopupWindowFinder.cs index cb4bebcd74522..4c863e83a01d8 100644 --- a/dotnet/src/support/UI/PopupWindowFinder.cs +++ b/dotnet/src/support/UI/PopupWindowFinder.cs @@ -131,7 +131,7 @@ public string Invoke(Action popupMethod) ReadOnlyCollection existingHandles = this.driver.WindowHandles; popupMethod(); - WebDriverWait wait = new WebDriverWait(new SystemClock(), this.driver, this.timeout, this.sleepInterval); + WebDriverWait wait = new WebDriverWait(SystemClock.Instance, this.driver, this.timeout, this.sleepInterval); string popupHandle = wait.Until((d) => { string? foundHandle = null; diff --git a/dotnet/src/support/UI/SlowLoadableComponent{T}.cs b/dotnet/src/support/UI/SlowLoadableComponent{T}.cs index ad5448c1a3a25..900e162b28c04 100644 --- a/dotnet/src/support/UI/SlowLoadableComponent{T}.cs +++ b/dotnet/src/support/UI/SlowLoadableComponent{T}.cs @@ -43,7 +43,7 @@ public abstract class SlowLoadableComponent : LoadableComponent /// /// The within which the component should be loaded. protected SlowLoadableComponent(TimeSpan timeout) - : this(timeout, new SystemClock()) + : this(timeout, SystemClock.Instance) { } diff --git a/dotnet/src/webdriver/Support/DefaultWait{T}.cs b/dotnet/src/webdriver/Support/DefaultWait{T}.cs index cd70f1d1ca384..87a376e3637a8 100644 --- a/dotnet/src/webdriver/Support/DefaultWait{T}.cs +++ b/dotnet/src/webdriver/Support/DefaultWait{T}.cs @@ -44,7 +44,7 @@ public class DefaultWait : IWait /// /// The input value to pass to the evaluated conditions. public DefaultWait(T input) - : this(input, new SystemClock()) + : this(input, SystemClock.Instance) { } diff --git a/dotnet/src/webdriver/Support/SystemClock.cs b/dotnet/src/webdriver/Support/SystemClock.cs index 5b8afc1105cd6..f6fee38c9b637 100644 --- a/dotnet/src/webdriver/Support/SystemClock.cs +++ b/dotnet/src/webdriver/Support/SystemClock.cs @@ -28,6 +28,11 @@ namespace OpenQA.Selenium.Support.UI /// public class SystemClock : IClock { + /// + /// An instance of the type. + /// + public static SystemClock Instance { get; } = new(); + /// /// Gets the current date and time values. /// diff --git a/dotnet/src/webdriver/Support/WebDriverWait.cs b/dotnet/src/webdriver/Support/WebDriverWait.cs index e5acc8e75ddc9..d1cbbbbe174c5 100644 --- a/dotnet/src/webdriver/Support/WebDriverWait.cs +++ b/dotnet/src/webdriver/Support/WebDriverWait.cs @@ -40,7 +40,7 @@ public class WebDriverWait : DefaultWait /// The WebDriver instance used to wait. /// The timeout value indicating how long to wait for the condition. public WebDriverWait(IWebDriver driver, TimeSpan timeout) - : this(new SystemClock(), driver, timeout, DefaultSleepTimeout) + : this(SystemClock.Instance, driver, timeout, DefaultSleepTimeout) { } diff --git a/dotnet/test/support/UI/FakeClock.cs b/dotnet/test/support/UI/HandCrankClock.cs similarity index 85% rename from dotnet/test/support/UI/FakeClock.cs rename to dotnet/test/support/UI/HandCrankClock.cs index fac13d78238c4..e46c0d5bcad96 100644 --- a/dotnet/test/support/UI/FakeClock.cs +++ b/dotnet/test/support/UI/HandCrankClock.cs @@ -21,18 +21,10 @@ namespace OpenQA.Selenium.Support.UI { - - public class FakeClock : IClock + public class HandCrankClock : IClock { - private DateTime fakeNow = new DateTime(50000); - public DateTime Now - { - get - { - return fakeNow; - } - } + public DateTime Now => fakeNow; public DateTime LaterBy(TimeSpan delay) { @@ -45,10 +37,9 @@ public bool IsNowBefore(DateTime otherDateTime) return Now < otherDateTime; } - public void TimePasses(TimeSpan timespan) + public void MoveTime(TimeSpan timespan) { fakeNow = fakeNow + timespan; } } - } diff --git a/dotnet/test/support/UI/SlowLoadableComponentTest.cs b/dotnet/test/support/UI/SlowLoadableComponentTest.cs index bc198f7e8cccb..c94090aaea062 100644 --- a/dotnet/test/support/UI/SlowLoadableComponentTest.cs +++ b/dotnet/test/support/UI/SlowLoadableComponentTest.cs @@ -43,7 +43,7 @@ public void TestShouldDoNothingIfComponentIsAlreadyLoaded() public void TestShouldCauseTheLoadMethodToBeCalledIfTheComponentIsNotAlreadyLoaded() { int numberOfTimesThroughLoop = 1; - SlowLoading slowLoading = new SlowLoading(TimeSpan.FromSeconds(1), new SystemClock(), numberOfTimesThroughLoop).Load(); + SlowLoading slowLoading = new SlowLoading(TimeSpan.FromSeconds(1), SystemClock.Instance, numberOfTimesThroughLoop).Load(); Assert.That(slowLoading.GetLoopCount(), Is.EqualTo(numberOfTimesThroughLoop)); } @@ -53,7 +53,7 @@ public void TestTheLoadMethodShouldOnlyBeCalledOnceIfTheComponentTakesALongTimeT { try { - new OnlyOneLoad(TimeSpan.FromSeconds(5), new SystemClock(), 5).Load(); + new OnlyOneLoad(TimeSpan.FromSeconds(5), SystemClock.Instance, 5).Load(); } catch (Exception) { @@ -64,7 +64,7 @@ public void TestTheLoadMethodShouldOnlyBeCalledOnceIfTheComponentTakesALongTimeT [Test] public void TestShouldThrowAnErrorIfCallingLoadDoesNotCauseTheComponentToLoadBeforeTimeout() { - FakeClock clock = new FakeClock(); + HandCrankClock clock = new HandCrankClock(); Assert.That( () => new BasicSlowLoader(TimeSpan.FromSeconds(2), clock).Load(), @@ -85,7 +85,7 @@ public void TestShouldCancelLoadingIfAnErrorIsDetected() private class DetonatingSlowLoader : SlowLoadableComponent { - public DetonatingSlowLoader() : base(TimeSpan.FromSeconds(1), new SystemClock()) { } + public DetonatingSlowLoader() : base(TimeSpan.FromSeconds(1), SystemClock.Instance) { } protected override void ExecuteLoad() { @@ -152,11 +152,11 @@ protected override void ExecuteLoad() private class BasicSlowLoader : SlowLoadableComponent { - private readonly FakeClock clock; - public BasicSlowLoader(TimeSpan timeOut, FakeClock clock) + private readonly HandCrankClock handCrankClock; + public BasicSlowLoader(TimeSpan timeOut, HandCrankClock clock) : base(timeOut, clock) { - this.clock = clock; + this.handCrankClock = clock; } protected override void ExecuteLoad() @@ -168,7 +168,7 @@ protected override bool EvaluateLoadedStatus() { // Cheat and increment the clock here, because otherwise it's hard to // get to. - clock.TimePasses(TimeSpan.FromSeconds(1)); + handCrankClock.MoveTime(TimeSpan.FromSeconds(1)); return false; // Never loads } } @@ -176,7 +176,7 @@ protected override bool EvaluateLoadedStatus() private class HasError : SlowLoadableComponent { - public HasError() : base(TimeSpan.FromSeconds(1000), new FakeClock()) { } + public HasError() : base(TimeSpan.FromSeconds(1000), new HandCrankClock()) { } protected override void ExecuteLoad() { @@ -194,10 +194,6 @@ protected override void HandleErrors() } } - private class CustomException : Exception - { - - } + private class CustomException : Exception; } - } From 1dbc277267a52eed09781ac9bccc31d8c5a2739b Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 13 Feb 2025 16:00:08 -0500 Subject: [PATCH 2/3] Fix copyright comment --- dotnet/test/support/UI/HandCrankClock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/test/support/UI/HandCrankClock.cs b/dotnet/test/support/UI/HandCrankClock.cs index e46c0d5bcad96..1b102ff62cb78 100644 --- a/dotnet/test/support/UI/HandCrankClock.cs +++ b/dotnet/test/support/UI/HandCrankClock.cs @@ -1,4 +1,4 @@ -// +// // Licensed to the Software Freedom Conservancy (SFC) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information From 058b228238ce15086d4aab401e938f4c300eb45e Mon Sep 17 00:00:00 2001 From: Michael Render Date: Tue, 25 Feb 2025 14:27:02 -0500 Subject: [PATCH 3/3] Fix support UI test build --- dotnet/test/support/UI/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/test/support/UI/BUILD.bazel b/dotnet/test/support/UI/BUILD.bazel index ef626213b0ebf..0d155ae0a066e 100644 --- a/dotnet/test/support/UI/BUILD.bazel +++ b/dotnet/test/support/UI/BUILD.bazel @@ -2,7 +2,7 @@ load("//dotnet:defs.bzl", "dotnet_nunit_test_suite", "framework") SMALL_TESTS = [ "DefaultWaitTest.cs", - "FakeClock.cs", + "HandCrankClock.cs", "LoadableComponentTests.cs", "SelectTests.cs", "SlowLoadableComponentTest.cs",