Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve timing related tests #2107

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/PuppeteerSharp.Tests/PageTests/WaitForNetworkIdleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ public WaitForNetworkIdleTests(ITestOutputHelper output) : base(output)
[PuppeteerFact]
public async Task ShouldWork()
{
var t1 = DateTime.Now;
var t2 = DateTime.Now;
var t1 = DateTime.UtcNow;
var t2 = DateTime.UtcNow;

await Page.GoToAsync(TestConstants.EmptyPage);
var task = Page.WaitForNetworkIdleAsync().ContinueWith(x => t1 = DateTime.Now);
var task = Page.WaitForNetworkIdleAsync()
.ContinueWith(x =>
{
if (x.IsFaulted) throw x.Exception;
return t1 = DateTime.UtcNow;
});

await Task.WhenAll(
task,
Expand All @@ -35,7 +40,11 @@ await Promise.all([
await fetch('/digits/3.png');
await new Promise((resolve) => setTimeout(resolve, 200));
await fetch('/digits/4.png');
}").ContinueWith(x => t2 = DateTime.Now)
}").ContinueWith(x =>
{
if (x.IsFaulted) throw x.Exception;
t2 = DateTime.UtcNow;
})
);

Assert.True(t1 > t2);
Expand All @@ -59,11 +68,16 @@ public async Task ShouldRespectTimeout()
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldRespectIdleTimeout()
{
var t1 = DateTime.Now;
var t2 = DateTime.Now;
var t1 = DateTime.UtcNow;
var t2 = DateTime.UtcNow;

await Page.GoToAsync(TestConstants.EmptyPage);
var task = Page.WaitForNetworkIdleAsync(new WaitForNetworkIdleOptions { IdleTime = 10 }).ContinueWith(x => t1 = DateTime.Now);
var task = Page.WaitForNetworkIdleAsync(new WaitForNetworkIdleOptions { IdleTime = 10 })
.ContinueWith(x =>
{
if (x.IsFaulted) throw x.Exception;
return t1 = DateTime.UtcNow;
});

await Task.WhenAll(
task,
Expand All @@ -73,7 +87,11 @@ await Promise.all([
fetch('/digits/2.png'),
]);
await new Promise((resolve) => setTimeout(resolve, 250));
}").ContinueWith(x => t2 = DateTime.Now)
}").ContinueWith(x =>
{
if (x.IsFaulted) throw x.Exception;
return t2 = DateTime.UtcNow;
})
);

Assert.True(t2 > t1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ await Page.WaitForFunctionAsync(@"() =>
public async Task ShouldPollOnInterval()
{
var success = false;
var startTime = DateTime.Now;
var startTime = DateTime.UtcNow;
var polling = 100;
var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling })
.ContinueWith(_ => success = true);
await Page.EvaluateExpressionAsync("window.__FOO = 'hit'");
Assert.False(success);
await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
await watchdog;
Assert.True((DateTime.Now - startTime).TotalMilliseconds > polling / 2);
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds > polling / 2);
}

[PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on interval async")]
[PuppeteerFact]
public async Task ShouldPollOnIntervalAsync()
{
var success = false;
var startTime = DateTime.Now;
var startTime = DateTime.UtcNow;
var polling = 100;
var watchdog = Page.WaitForFunctionAsync("async () => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling })
.ContinueWith(_ => success = true);
await Page.EvaluateFunctionAsync("async () => window.__FOO = 'hit'");
Assert.False(success);
await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
await watchdog;
Assert.True((DateTime.Now - startTime).TotalMilliseconds > polling / 2);
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds > polling / 2);
}

[PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on mutation")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public FrameWaitForTimeoutTests(ITestOutputHelper output) : base(output)
public async Task WaitsForTheGivenTimeoutBeforeResolving()
{
await Page.GoToAsync(TestConstants.EmptyPage);
var startTime = DateTime.Now;
var startTime = DateTime.UtcNow;
await Page.MainFrame.WaitForTimeoutAsync(1000);
Assert.True((DateTime.Now - startTime).TotalMilliseconds > 700);
Assert.True((DateTime.Now - startTime).TotalMilliseconds < 1300);
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds > 700);
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds < 1300);
}
}
}
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp.Tests/WaitTaskTests/PageWaitForTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public async Task ShouldNotAllowYouToSelectAnElementWithSingleSlashXpath()
[PuppeteerFact]
public async Task ShouldTimeout()
{
var startTime = DateTime.Now;
var startTime = DateTime.UtcNow;
var timeout = 42;
await Page.WaitForTimeoutAsync(timeout);
Assert.True((DateTime.Now - startTime).TotalMilliseconds > timeout / 2);
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds > timeout / 2);
}

[PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should work with multiline body")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public PageWaitForTimeoutTests(ITestOutputHelper output) : base(output)
public async Task WaitsForTheGivenTimeoutBeforeResolving()
{
await Page.GoToAsync(TestConstants.EmptyPage);
var startTime = DateTime.Now;
var startTime = DateTime.UtcNow;
await Page.WaitForTimeoutAsync(1000);
Assert.True((DateTime.Now - startTime).TotalMilliseconds > 700);
Assert.True((DateTime.Now - startTime).TotalMilliseconds < 1300);
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds > 700);
Assert.True((DateTime.UtcNow - startTime).TotalMilliseconds < 1300);
}
}
}