Skip to content

Commit

Permalink
chore: Clock API roll follow-ups (#2953)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Jul 3, 2024
1 parent 880775a commit c4b3dc4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/Common/Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyVersion>1.45.0</AssemblyVersion>
<PackageVersion>$(AssemblyVersion)</PackageVersion>
<DriverVersion>1.45.0-beta-1719505820000</DriverVersion>
<DriverVersion>1.45.1-beta-1719996498000</DriverVersion>
<ReleaseVersion>$(AssemblyVersion)</ReleaseVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<NoDefaultExcludes>true</NoDefaultExcludes>
Expand Down
74 changes: 37 additions & 37 deletions src/Playwright.Tests/PageClockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class RunForTests : PageClockTests
[SetUp]
public async Task RunForSetUp()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
}

[PlaywrightTest("page-clock.spec.ts", "triggers immediately without specified delay")]
Expand Down Expand Up @@ -120,7 +120,7 @@ public async Task TriggersWhenSomeThrow()
[PlaywrightTest("page-clock.spec.ts", "creates updated Date while ticking")]
public async Task CreatesUpdatedDateWhileTicking()
{
await Page.Clock.SetSystemTimeAsync(0);
await Page.Clock.SetSystemTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime);
await Page.EvaluateAsync("() => { setInterval(() => { window.stubWithNumberValue(new Date().getTime()); }, 10); }");
await Page.Clock.RunForAsync(100);
Assert.AreEqual(new[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }, _calls.Select(c => c[0]));
Expand Down Expand Up @@ -161,7 +161,7 @@ public async Task ThrowsForInvalidFormat()
[PlaywrightTest("page-clock.spec.ts", "returns the current now value")]
public async Task ReturnsTheCurrentNowValue()
{
await Page.Clock.SetSystemTimeAsync(0);
await Page.Clock.SetSystemTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime);
var value = 200;
await Page.Clock.RunForAsync(value);
Assert.AreEqual(value, await Page.EvaluateAsync<int>("Date.now()"));
Expand All @@ -173,8 +173,8 @@ public class FastForwardTests : PageClockTests
[SetUp]
public async Task FastForwardSetUp()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
}

[PlaywrightTest("page-clock.spec.ts", "ignores timers which wouldn't be run")]
Expand Down Expand Up @@ -209,14 +209,14 @@ public class StubTimersTests : PageClockTests
[SetUp]
public async Task StubTimersSetUp()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
}

[PlaywrightTest("page-clock.spec.ts", "sets initial timestamp")]
public async Task SetsInitialTimestamp()
{
await Page.Clock.SetSystemTimeAsync(1400);
await Page.Clock.SetSystemTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(1400).UtcDateTime);
Assert.AreEqual(1400, await Page.EvaluateAsync<int>("Date.now()"));
}

Expand Down Expand Up @@ -288,8 +288,8 @@ public class StubTimerTests : PageClockTests
[PlaywrightTest("page-clock.spec.ts", "replaces global performance.timeOrigin")]
public async Task ReplacesGlobalPerformanceTimeOrigin()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 1000 });
await Page.Clock.PauseAtAsync(2000);
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime });
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(2000).UtcDateTime);
var promise = Page.EvaluateAsync<JsonElement>("async () => { const prev = performance.now(); await new Promise(f => setTimeout(f, 1000)); const next = performance.now(); return { prev, next }; }");
await Page.Clock.RunForAsync(1000);
Assert.AreEqual(1000, await Page.EvaluateAsync<int>("performance.timeOrigin"));
Expand All @@ -304,7 +304,7 @@ public class PopupTests : PageClockTests
[PlaywrightTest("page-clock.spec.ts", "should tick after popup")]
public async Task ShouldTickAfterPopup()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
var now = new DateTime(2015, 9, 25);
await Page.Clock.PauseAtAsync(now);
var popupTask = Page.WaitForPopupAsync();
Expand All @@ -320,7 +320,7 @@ public async Task ShouldTickAfterPopup()
[PlaywrightTest("page-clock.spec.ts", "should tick before popup")]
public async Task ShouldTickBeforePopup()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
var now = new DateTime(2015, 9, 25);
await Page.Clock.PauseAtAsync(now);
await Page.Clock.RunForAsync(1000);
Expand Down Expand Up @@ -357,8 +357,8 @@ public async Task ShouldNotRunTimeBeforePopupOnPause()
context.Response.Headers["Content-Type"] = "text/html";
return context.Response.WriteAsync("<script>window.time = Date.now();</script>");
});
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
await Page.GotoAsync(Server.EmptyPage);
// Wait for 2 seconds in real life to check that it is past in popup.
await Page.WaitForTimeoutAsync(2000);
Expand All @@ -375,24 +375,24 @@ public class SetFixedTimeTests : PageClockTests
[PlaywrightTest("page-clock.spec.ts", "does not fake methods")]
public async Task DoesNotFakeMethods()
{
await Page.Clock.SetFixedTimeAsync(0);
await Page.Clock.SetFixedTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime);
// Should not stall.
await Page.EvaluateAsync("() => new Promise(f => setTimeout(f, 1))");
}

[PlaywrightTest("page-clock.spec.ts", "allows setting time multiple times")]
public async Task AllowsSettingTimeMultipleTimes()
{
await Page.Clock.SetFixedTimeAsync(100);
await Page.Clock.SetFixedTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(100).UtcDateTime);
Assert.AreEqual(100, await Page.EvaluateAsync<long>("Date.now()"));
await Page.Clock.SetFixedTimeAsync(200);
await Page.Clock.SetFixedTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(200).UtcDateTime);
Assert.AreEqual(200, await Page.EvaluateAsync<long>("Date.now()"));
}

[PlaywrightTest("page-clock.spec.ts", "fixed time is not affected by clock manipulation")]
public async Task FixedTimeIsNotAffectedByClockManipulation()
{
await Page.Clock.SetFixedTimeAsync(100);
await Page.Clock.SetFixedTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(100).UtcDateTime);
Assert.AreEqual(100, await Page.EvaluateAsync<long>("Date.now()"));
await Page.Clock.FastForwardAsync(20);
Assert.AreEqual(100, await Page.EvaluateAsync<long>("Date.now()"));
Expand All @@ -401,9 +401,9 @@ public async Task FixedTimeIsNotAffectedByClockManipulation()
[PlaywrightTest("page-clock.spec.ts", "allows installing fake timers after setting time")]
public async Task AllowsInstallingFakeTimersAfterSettingTime()
{
await Page.Clock.SetFixedTimeAsync(100);
await Page.Clock.SetFixedTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(100).UtcDateTime);
Assert.AreEqual(100, await Page.EvaluateAsync<long>("Date.now()"));
await Page.Clock.SetFixedTimeAsync(200);
await Page.Clock.SetFixedTimeAsync(DateTimeOffset.FromUnixTimeMilliseconds(200).UtcDateTime);
await Page.EvaluateAsync("() => { setTimeout(() => window.stubWithNumberValue(Date.now()), 0); }");
await Page.Clock.RunForAsync(0);
Assert.AreEqual(1, _calls.Count);
Expand All @@ -416,7 +416,7 @@ public class WhileRunningTests : PageClockTests
[PlaywrightTest("page-clock.spec.ts", "should progress time")]
public async Task ShouldProgressTime()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.WaitForTimeoutAsync(1000);
var now = await Page.EvaluateAsync<long>("Date.now()");
Expand All @@ -427,7 +427,7 @@ public async Task ShouldProgressTime()
[PlaywrightTest("page-clock.spec.ts", "should runFor")]
public async Task ShouldRunFor()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.RunForAsync(10000);
var now = await Page.EvaluateAsync<long>("Date.now()");
Expand All @@ -438,7 +438,7 @@ public async Task ShouldRunFor()
[PlaywrightTest("page-clock.spec.ts", "should fastForward")]
public async Task ShouldFastForward()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.FastForwardAsync(10000);
var now = await Page.EvaluateAsync<long>("Date.now()");
Expand All @@ -449,7 +449,7 @@ public async Task ShouldFastForward()
[PlaywrightTest("page-clock.spec.ts", "should fastForwardTo")]
public async Task ShouldFastForwardTo()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.FastForwardAsync(10000);
var now = await Page.EvaluateAsync<long>("Date.now()");
Expand All @@ -460,9 +460,9 @@ public async Task ShouldFastForwardTo()
[PlaywrightTest("page-clock.spec.ts", "should pause")]
public async Task ShouldPause()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
await Page.WaitForTimeoutAsync(1000);
await Page.Clock.ResumeAsync();
var now = await Page.EvaluateAsync<long>("Date.now()");
Expand All @@ -473,9 +473,9 @@ public async Task ShouldPause()
[PlaywrightTest("page-clock.spec.ts", "should pause and fastForward")]
public async Task ShouldPauseAndFastForward()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
await Page.Clock.FastForwardAsync(1000);
var now = await Page.EvaluateAsync<long>("Date.now()");
Assert.AreEqual(2000, now);
Expand All @@ -484,9 +484,9 @@ public async Task ShouldPauseAndFastForward()
[PlaywrightTest("page-clock.spec.ts", "should set system time on pause")]
public async Task ShouldSetSystemTimeOnPause()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
var now = await Page.EvaluateAsync<long>("Date.now()");
Assert.AreEqual(1000, now);
}
Expand All @@ -497,9 +497,9 @@ public class WhileOnPauseTests : PageClockTests
[PlaywrightTest("page-clock.spec.ts", "fastForward should not run nested immediate")]
public async Task FastForwardShouldNotRunNestedImmediate()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
await Page.EvaluateAsync("() => { setTimeout(() => { window.stubWithStringValue('outer'); setTimeout(() => window.stubWithStringValue('inner'), 0); }, 1000); }");
await Page.Clock.FastForwardAsync(1000);
Assert.AreEqual(1, _calls.Count);
Expand All @@ -512,9 +512,9 @@ public async Task FastForwardShouldNotRunNestedImmediate()
[PlaywrightTest("page-clock.spec.ts", "runFor should not run nested immediate")]
public async Task RunForShouldNotRunNestedImmediate()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
await Page.EvaluateAsync("() => { setTimeout(() => { window.stubWithStringValue('outer'); setTimeout(() => window.stubWithStringValue('inner'), 0); }, 1000); }");
await Page.Clock.RunForAsync(1000);
Assert.AreEqual(1, _calls.Count);
Expand All @@ -527,9 +527,9 @@ public async Task RunForShouldNotRunNestedImmediate()
[PlaywrightTest("page-clock.spec.ts", "runFor should not run nested immediate from microtask")]
public async Task RunForShouldNotRunNestedImmediateFromMicrotask()
{
await Page.Clock.InstallAsync(new() { TimeInt64 = 0 });
await Page.Clock.InstallAsync(new() { TimeDate = DateTimeOffset.FromUnixTimeMilliseconds(0).UtcDateTime });
await Page.GotoAsync("data:text/html,");
await Page.Clock.PauseAtAsync(1000);
await Page.Clock.PauseAtAsync(DateTimeOffset.FromUnixTimeMilliseconds(1000).UtcDateTime);
await Page.EvaluateAsync("() => { setTimeout(() => { window.stubWithStringValue('outer'); Promise.resolve().then(() => setTimeout(() => window.stubWithStringValue('inner'), 0)); }, 1000); }");
await Page.Clock.RunForAsync(1000);
Assert.AreEqual(1, _calls.Count);
Expand Down
62 changes: 5 additions & 57 deletions src/Playwright/API/Generated/IClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ public partial interface IClock
/// await page.Clock.PauseAtAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">
/// </param>
Task PauseAtAsync(long time);
/// <param name="time">Time to pause at.</param>
Task PauseAtAsync(DateTime time);

/// <summary>
/// <para>
Expand All @@ -170,30 +169,9 @@ public partial interface IClock
/// await page.Clock.PauseAtAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">
/// </param>
/// <param name="time">Time to pause at.</param>
Task PauseAtAsync(string time);

/// <summary>
/// <para>
/// Advance the clock by jumping forward in time and pause the time. Once this method
/// is called, no timers are fired unless <see cref="IClock.RunForAsync"/>, <see cref="IClock.FastForwardAsync"/>,
/// <see cref="IClock.PauseAtAsync"/> or <see cref="IClock.ResumeAsync"/> is called.
/// </para>
/// <para>
/// Only fires due timers at most once. This is equivalent to user closing the laptop
/// lid for a while and reopening it at the specified time and pausing.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.PauseAtAsync(DateTime.Parse("2020-02-02"));<br/>
/// await page.Clock.PauseAtAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">
/// </param>
Task PauseAtAsync(DateTime time);

/// <summary>
/// <para>
/// Resumes timers. Once this method is called, time resumes flowing, timers are fired
Expand All @@ -202,21 +180,6 @@ public partial interface IClock
/// </summary>
Task ResumeAsync();

/// <summary>
/// <para>
/// Makes <c>Date.now</c> and <c>new Date()</c> return fixed fake time at all times,
/// keeps all the timers running.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.SetFixedTimeAsync(DateTime.Now);<br/>
/// await page.Clock.SetFixedTimeAsync(new DateTime(2020, 2, 2));<br/>
/// await page.Clock.SetFixedTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">Time to be set.</param>
Task SetFixedTimeAsync(long time);

/// <summary>
/// <para>
/// Makes <c>Date.now</c> and <c>new Date()</c> return fixed fake time at all times,
Expand Down Expand Up @@ -256,21 +219,7 @@ public partial interface IClock
/// await page.Clock.SetSystemTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">
/// </param>
Task SetSystemTimeAsync(long time);

/// <summary>
/// <para>Sets current system time but does not trigger any timers.</para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.SetSystemTimeAsync(DateTime.Now);<br/>
/// await page.Clock.SetSystemTimeAsync(new DateTime(2020, 2, 2));<br/>
/// await page.Clock.SetSystemTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">
/// </param>
/// <param name="time">Time to be set.</param>
Task SetSystemTimeAsync(string time);

/// <summary>
Expand All @@ -282,8 +231,7 @@ public partial interface IClock
/// await page.Clock.SetSystemTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">
/// </param>
/// <param name="time">Time to be set.</param>
Task SetSystemTimeAsync(DateTime time);
}

Expand Down
Loading

0 comments on commit c4b3dc4

Please sign in to comment.