Repository navigation
Port Playwright's request-continue, wait-for and network-idle tests (Refs #1060) #1083
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
Changes from all commits
8769f58
b9f92cf
a21cd42
ce17a04
4f74351
99618b1
78d9d9d
1601a02
260bf46
5c863d6
81a7877
86a0bf9
f30a380
f36e5d2
e1140d7
c780e64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| // Copyright 2026 TesterArmy. | ||
| // Modified by Dario Kondratiuk. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using E2E.Engine; | ||
| using static E2E.NUnit.Tests.Helpers; | ||
|
|
||
| namespace E2E.NUnit.Tests.NetworkIdle; | ||
|
|
||
| /// <summary> | ||
| /// <c>test.describe('page network idle')</c> of upstream's <c>apps/testbed/tests/network-idle.e2e.ts</c> (Playwright's | ||
| /// <c>page-network-idle.spec.ts</c>), where <c>page.setContent(html, { waitUntil })</c> is <c>Browser.GotoAsync(Content(html), ...)</c> | ||
| /// and a request the server holds is one a route holds. Not ported, as upstream: should wait for networkidle in waitForNavigation | ||
| /// (no <c>waitForNavigation</c>), should wait for networkidle when navigating iframe (no <c>frame.goto</c>), should wait for networkidle | ||
| /// from the popup (no popup event and <c>waitForLoadState</c>). | ||
| /// </summary> | ||
| public sealed class PageNetworkIdleTests : PlaywrightTest | ||
| { | ||
| private static readonly GotoOptions NetworkIdle = new() { WaitUntil = GotoWaitUntil.NetworkIdle }; | ||
|
|
||
| private static string EmptyPage => $"{PREFIX}/empty.html"; | ||
|
|
||
| /// <summary> | ||
| /// Playwright's <c>networkIdleTest</c>: holds <c>fetch-request-a.js</c> and <c>fetch-request-b.js</c> | ||
| /// until the test answers them, and checks <paramref name="action"/> settles only 500ms after the last. | ||
| /// </summary> | ||
| private async Task NetworkIdleTestAsync(Func<Task> action) | ||
| { | ||
| var aRequested = Deferred(); | ||
| var aAnswer = Deferred(); | ||
| var bRequested = Deferred(); | ||
| var bAnswer = Deferred(); | ||
| foreach (var (suffix, requested, answer) in new[] { ("/fetch-request-a.js", aRequested, aAnswer), ("/fetch-request-b.js", bRequested, bAnswer) }) | ||
| { | ||
| await Browser.RouteAsync($"**{suffix}", async route => | ||
| { | ||
| requested.TrySetResult(); | ||
| await answer.Task; | ||
| await route.FulfillAsync(new RouteFulfillResponse { Status = 404, Body = "File not found" }); | ||
| }); | ||
| } | ||
|
|
||
| var actionPromise = action(); | ||
| var actionFinished = false; | ||
| _ = actionPromise.ContinueWith(_ => actionFinished = true, TaskScheduler.Default); | ||
|
|
||
| await aRequested.Task; | ||
| Assert.That(actionFinished, Is.False); | ||
|
|
||
| await Browser.EvaluateAsync<object>("() => window['fetchSecond']()"); | ||
| aAnswer.TrySetResult(); | ||
|
|
||
| await bRequested.Task; | ||
| Assert.That(actionFinished, Is.False); | ||
|
|
||
| var timer = Task.Delay(500); | ||
| bAnswer.TrySetResult(); | ||
|
|
||
| await actionPromise; | ||
| Assert.That(timer.IsCompletedSuccessfully, Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_navigate_to_empty_page_with_networkidle() | ||
| { | ||
| var response = await AllAsync(Browser.WaitForResponseAsync("**/empty.html"), Browser.GotoAsync(EmptyPage, NetworkIdle)); | ||
| Assert.That(response.Status, Is.EqualTo(200)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_wait_for_networkidle_to_succeed_navigation() | ||
| { | ||
| await NetworkIdleTestAsync(() => Browser.GotoAsync("/networkidle.html", NetworkIdle)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_wait_for_networkidle_to_succeed_navigation_with_request_from_previous_navigation() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.GotoAsync(Content("<script>fetch('/never');</script>")); | ||
| await NetworkIdleTestAsync(() => Browser.GotoAsync("/networkidle.html", NetworkIdle)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_wait_for_networkidle_in_setContent() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await NetworkIdleTestAsync(() => Browser.GotoAsync(Content("<script src='networkidle.js'></script>"), NetworkIdle)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_wait_for_networkidle_in_setContent_with_request_from_previous_navigation() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.GotoAsync(Content("<script>fetch('/never');</script>")); | ||
| await NetworkIdleTestAsync(() => Browser.GotoAsync(Content("<script src='networkidle.js'></script>"), NetworkIdle)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_wait_for_networkidle_in_setContent_from_the_child_frame() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await NetworkIdleTestAsync(() => Browser.GotoAsync(Content("<iframe src='networkidle.html'></iframe>"), NetworkIdle)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_wait_for_networkidle_from_the_child_frame() | ||
| { | ||
| await NetworkIdleTestAsync(() => Browser.GotoAsync("/networkidle-frame.html", NetworkIdle)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_wait_for_networkidle_when_iframe_attaches_and_detaches() | ||
| { | ||
| var done = false; | ||
| var promise = Task.Run(async () => | ||
| { | ||
| await Browser.GotoAsync(Content($$""" | ||
| <body> | ||
| <script> | ||
| const iframe = document.createElement('iframe'); | ||
| iframe.src = {{System.Text.Json.JsonSerializer.Serialize($"{PREFIX}/never")}}; | ||
| document.body.appendChild(iframe); | ||
| </script> | ||
| </body> | ||
| """), NetworkIdle); | ||
| done = true; | ||
| }); | ||
| await Task.Delay(600); | ||
| Assert.That(done, Is.False); | ||
|
Comment on lines
+128
to
+130
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| await Browser.EvaluateAsync<object>("() => { document.querySelector('iframe').remove(); }"); | ||
| await promise; | ||
| Assert.That(done, Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_work_after_repeated_navigations_in_the_same_page() | ||
| { | ||
| var requestCount = 0; | ||
| await Browser.RouteAsync("**/empty.html", route => route.FulfillAsync(new RouteFulfillResponse | ||
| { | ||
| ContentType = "text/html", | ||
| Body = """ | ||
| <script> | ||
| fetch('http://localhost:8000/sample').then(res => console.log(res.json())) | ||
| </script> | ||
| """, | ||
| })); | ||
| await Browser.RouteAsync("**/sample", async route => | ||
| { | ||
| Interlocked.Increment(ref requestCount); | ||
| await route.FulfillAsync(new RouteFulfillResponse { ContentType = "application/json", Body = """{"content":"sample"}""" }); | ||
| }); | ||
| await Browser.GotoAsync(EmptyPage, NetworkIdle); | ||
| Assert.That(requestCount, Is.EqualTo(1)); | ||
| await Browser.GotoAsync(EmptyPage, NetworkIdle); | ||
| Assert.That(requestCount, Is.EqualTo(2)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_not_wait_for_an_open_EventSource_connection() | ||
| { | ||
| var page = Content("<script>new EventSource('/sse');</script>"); | ||
| var response = await AllAsync(Browser.WaitForResponseAsync("**/content?**"), Browser.GotoAsync(page, NetworkIdle)); | ||
| Assert.That(response.Status, Is.EqualTo(200)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_not_wait_for_an_open_EventSource_connection_in_setContent() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.GotoAsync(Content(""" | ||
| <script> | ||
| window.__sseOpened = new Promise(resolve => { | ||
| const es = new EventSource('/sse'); | ||
| es.onmessage = () => resolve(); | ||
| }); | ||
| </script> | ||
| """), NetworkIdle); | ||
| await Browser.EvaluateAsync<object>("() => window['__sseOpened'].then(() => null)"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Copyright 2026 TesterArmy. | ||
| // Modified by Dario Kondratiuk. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using E2E.Engine; | ||
| using static E2E.NUnit.Tests.Helpers; | ||
|
|
||
| namespace E2E.NUnit.Tests.RequestContinue; | ||
|
|
||
| /// <summary> | ||
| /// The nested <c>test.describe('post data')</c> of <c>page request continue</c> in upstream's <c>apps/testbed/tests/request-continue.e2e.ts</c> | ||
| /// (Playwright's <c>page-request-continue.spec.ts</c>). | ||
| /// </summary> | ||
| public sealed class PageRequestContinuePostDataTests : PlaywrightTest | ||
| { | ||
| private static string EmptyPage => $"{PREFIX}/empty.html"; | ||
|
|
||
| private async Task<Served> FetchedAsync(string url, string init) => | ||
| Served.Parse((await Browser.EvaluateAsync<string>($"(url) => fetch(url, {init}).then((response) => response.text())", url))!); | ||
|
|
||
| [Test] | ||
| public async Task Should_amend_post_data() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.RouteAsync("**/*", route => route.ContinueAsync(new RouteContinueOverrides { PostData = "doggo" })); | ||
| var serverRequest = await FetchedAsync("/cors-echo", "{ method: 'POST', body: 'birdy' }"); | ||
| Assert.That(serverRequest.Body, Is.EqualTo("doggo")); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_compute_content_length_from_post_data() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| var data = new string('a', 7500); | ||
| await Browser.RouteAsync("**/*", route => route.ContinueAsync(new RouteContinueOverrides | ||
| { | ||
| PostData = data, | ||
| Headers = WithHeaders(route.Request.Headers, ("content-type", "application/json")), | ||
| })); | ||
| var serverRequest = await FetchedAsync("/cors-echo", "{ method: 'PATCH', body: 'birdy' }"); | ||
| Assert.That(serverRequest.Body, Is.EqualTo(data)); | ||
| Assert.That(serverRequest.Header("content-length"), Is.EqualTo(data.Length.ToString(System.Globalization.CultureInfo.InvariantCulture))); | ||
| Assert.That(serverRequest.Header("content-type"), Is.EqualTo("application/json")); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_amend_method_and_post_data() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.RouteAsync("**/*", route => route.ContinueAsync(new RouteContinueOverrides { Method = "POST", PostData = "doggo" })); | ||
| var serverRequest = await FetchedAsync("/cors-echo", "{ method: 'GET' }"); | ||
| Assert.That(serverRequest.Method, Is.EqualTo("POST")); | ||
| Assert.That(serverRequest.Body, Is.EqualTo("doggo")); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_amend_utf8_post_data() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.RouteAsync("**/*", route => route.ContinueAsync(new RouteContinueOverrides { PostData = "пушкин" })); | ||
| var serverRequest = await FetchedAsync("/cors-echo", "{ method: 'POST', body: 'birdy' }"); | ||
| Assert.That(serverRequest.Method, Is.EqualTo("POST")); | ||
| Assert.That(serverRequest.Body, Is.EqualTo("пушкин")); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_amend_longer_post_data() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.RouteAsync("**/*", route => route.ContinueAsync(new RouteContinueOverrides { PostData = "doggo-is-longer-than-birdy" })); | ||
| var serverRequest = await FetchedAsync("/cors-echo", "{ method: 'POST', body: 'birdy' }"); | ||
| Assert.That(serverRequest.Method, Is.EqualTo("POST")); | ||
| Assert.That(serverRequest.Body, Is.EqualTo("doggo-is-longer-than-birdy")); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_use_content_type_from_original_request() | ||
| { | ||
| await Browser.GotoAsync(EmptyPage); | ||
| await Browser.RouteAsync($"{PREFIX}/cors-echo", route => route.ContinueAsync(new RouteContinueOverrides { PostData = """{"b":2}""" })); | ||
| var request = await FetchedAsync($"{PREFIX}/cors-echo", """{ method: 'POST', body: '{"a":1}', headers: { 'content-type': 'application/json' } }"""); | ||
| Assert.That(request.Header("content-type"), Is.EqualTo("application/json")); | ||
| Assert.That(request.Body, Is.EqualTo("""{"b":2}""")); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
rg -n 'Deferred\(|Timeout|CancelAfter|NetworkIdleTestAsync' tests/E2E.NUnit.Tests/Helpers tests/E2E.NUnit.Tests/NetworkIdle tests/E2E.NUnit.Tests/PlaywrightTest.csRepository: hardkoded/e2e-dotnet
Length of output: 1942
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 7112
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 13671
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 1047
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 5992
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 11030
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 2404
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 5836
Bound the request-signal waits to the navigation result.
If
actionPromisefaults before the route handler runs,aRequested.TaskorbRequested.Tasknever completes. The test then hangs, and the navigation error is never reported. Wait onTask.WhenAny(requested.Task, actionPromise)at both request waits. IfactionPromisefinishes first, await it so its exception surfaces.