Skip to content
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
31 changes: 29 additions & 2 deletions tests/E2E.Agent.Tests/PlaywrightRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ namespace E2E.Playground;
/// The routes of the tests ported from Playwright, a port of upstream's <c>apps/testbed/app/playwright/routes.mjs</c>:
/// its test assets (copied from microsoft/playwright <c>tests/assets</c> at v1.64.1, Apache-2.0, under
/// <c>Testbed/playwright/assets</c> next to the test assembly) and the dynamic routes its test server sets per test.
/// The playground serves them for any path it has no page for. This port has <c>/content</c> and the assets so far.
/// The playground serves them for any path it has no page for. This port has <c>/content</c>, <c>/never</c> and the assets so far.
/// </summary>
internal static class PlaywrightRoutes
{
private static readonly string Root = Path.Combine(AppContext.BaseDirectory, "Testbed", "playwright", "assets");
private static readonly string Root = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "Testbed", "playwright", "assets"));

/// <summary>Cancelled when the process exits, which releases the requests a route holds.</summary>
private static readonly CancellationTokenSource Shutdown = CreateShutdown();

private static readonly Dictionary<string, string> Types = new(StringComparer.Ordinal)
{
Expand All @@ -45,6 +48,23 @@ public static async Task HandleAsync(HttpListenerContext context)
return;
}

if (path == "/never")
{
// Holds the response and never answers, as upstream's /never. It ends when the run ends (the testbed never stops before that)
// or after upstream's longest delay, so a held request cannot outlive the run.
try
{
await Task.Delay(TimeSpan.FromSeconds(30), Shutdown.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// The run is ending.
}

response.Abort();
return;
}

if (path == "/respond")
{
var query = context.Request.QueryString;
Expand Down Expand Up @@ -88,6 +108,13 @@ public static async Task HandleAsync(HttpListenerContext context)
await WriteAsync(response, await File.ReadAllBytesAsync(file).ConfigureAwait(false)).ConfigureAwait(false);
}

private static CancellationTokenSource CreateShutdown()
{
var source = new CancellationTokenSource();
AppDomain.CurrentDomain.ProcessExit += (_, _) => source.Cancel();
return source;
}

/// <summary>Answers a multipart form with the files it carried as JSON, as upstream's <c>/upload</c>: name, filename, mimetype, size, and the bytes (base64) of a file up to 64 KB.</summary>
private static async Task UploadAsync(HttpListenerContext context)
{
Expand Down
60 changes: 58 additions & 2 deletions tests/E2E.Agent.Tests/Testbed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,17 @@ function submitForm() {
/// <summary>The base URL of the running playground, started on first use.</summary>
public static string Url => Server.Value;

/// <summary>Whether the app also answers on <c>localhost</c>, the second origin of the cross-origin cases. Starts the app when it is not running yet.</summary>
public static bool CrossOriginAvailable => Url.Length > 0 && s_crossOrigin;

private static bool s_crossOrigin;

private static string Start()
{
// HttpListener cannot bind port 0: take a free port, and try again when another test takes it first.
string url;
HttpListener listener;
bool crossOrigin;
for (var attempt = 0; ; attempt++)
{
var probe = new System.Net.Sockets.TcpListener(IPAddress.Loopback, 0);
Expand All @@ -933,7 +939,6 @@ private static string Start()
try
{
listener.Start();
break;
}
catch (HttpListenerException)
{
Expand All @@ -945,8 +950,24 @@ private static string Start()

continue;
}

// The second origin of the cross-origin cases (upstream's CROSS), the same app under another host name. A host that cannot
// bind it (no mapping, no URL ACL) still serves the app; only the cross-origin tests skip themselves.
try
{
listener.Prefixes.Add("http://localhost:" + port.ToString(CultureInfo.InvariantCulture) + "/");
crossOrigin = true;
}
catch (Exception ex) when (ex is HttpListenerException or System.Net.Sockets.SocketException or ArgumentException)
{
crossOrigin = false;
}

break;
}

s_crossOrigin = crossOrigin;

_ = Task.Run(() => ListenAsync(listener));
return url.TrimEnd('/');
}
Expand Down Expand Up @@ -1015,7 +1036,42 @@ await WriteHtmlAsync(response, "Dashboard", """
}
else
{
await PlaywrightRoutes.HandleAsync(context).ConfigureAwait(false);
// Its own task: a route that holds its response (upstream's /never) must not stall the other requests.
_ = Task.Run(async () =>
{
try
{
await PlaywrightRoutes.HandleAsync(context).ConfigureAwait(false);
}
catch (Exception ex) when (ex is HttpListenerException or InvalidOperationException or IOException or ObjectDisposedException)
{
// The client went away.
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
// A route bug is a 500 the test can see, not an empty 200.
try
{
response.StatusCode = 500;
}
catch (Exception inner) when (inner is InvalidOperationException or ObjectDisposedException or System.Net.ProtocolViolationException)
{
// The response was already sent.
}
}
finally
{
try
{
response.Close();
}
catch (Exception ex) when (ex is HttpListenerException or InvalidOperationException or IOException or ObjectDisposedException)
{
// The response was aborted.
}
}
});
continue;
}

response.Close();
Expand Down
34 changes: 34 additions & 0 deletions tests/E2E.NUnit.Tests/FrameGoto/FrameGotoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.FrameGoto;

/// <summary>
/// <c>test.describe('frame goto')</c> of upstream's <c>apps/testbed/tests/frame-goto.e2e.ts</c> (Playwright's
/// <c>frame-goto.spec.ts</c>). The stalled <c>/frames/script.js</c> is a route that sends the request to <c>/never</c>. Not ported,
/// as upstream: should navigate subframes, should reject when frame detaches, should return matching responses (no
/// <c>frame.goto</c> and no Frame objects).
/// </summary>
public sealed class FrameGotoTests : PlaywrightTest
{
// Known difference, hardkoded/e2e-dotnet#1068: the timeout message has no `timed out in browser.goto` label.
[Test]
public async Task Should_continue_after_client_redirect()
{
await ExpectedToFailAsync("child-redirect.html: Timeout", async () =>
{
await Browser.RouteAsync("**/frames/script.js", route => route.ContinueAsync(new RouteContinueOverrides { Url = $"{PREFIX}/never" }));
var url = $"{PREFIX}/frames/child-redirect.html";
var error = await Record(() => Browser.GotoAsync(url, new GotoOptions { Timeout = TimeSpan.FromSeconds(5), WaitUntil = GotoWaitUntil.NetworkIdle }));

if (error is null || !error.Message.Contains("timed out in browser.goto", StringComparison.Ordinal))
{
throw new InvalidOperationException("Expected a message with 'timed out in browser.goto', but was: " + (error?.Message ?? "no error"));
}
});
}
}
54 changes: 54 additions & 0 deletions tests/E2E.NUnit.Tests/FrameHierarchy/FrameHierarchyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2026 TesterArmy.
// Modified by Dario Kondratiuk.
// SPDX-License-Identifier: Apache-2.0

using static E2E.NUnit.Tests.Helpers;

namespace E2E.NUnit.Tests.FrameHierarchy;

/// <summary>
/// <c>test.describe('frame hierarchy')</c> of upstream's <c>apps/testbed/tests/frame-hierarchy.e2e.ts</c> (Playwright's
/// <c>frame-hierarchy.spec.ts</c>). A frame the original finds in <c>page.frames()</c> is reached through <c>FrameLocator</c>. Not
/// ported, as upstream: should send events when frames are manipulated dynamically, should persist mainFrame on cross-process
/// navigation, should not send attach/detach events for main frame, should detach child frames on navigation, should support
/// framesets, should report frame.name(), should report frame.parent(), should report different frame instance when frame
/// re-attaches, should refuse to display x-frame-options:deny iframe, should return frame.page() (frame events, Frame objects,
/// console events).
/// </summary>
public sealed class FrameHierarchyTests : PlaywrightTest
{
[Test]
public async Task Should_handle_nested_frames()
{
await Browser.GotoAsync("/frames/nested-frames.html");
await Expect.That(Browser.FrameLocator("[name=aframe]").Locator("div")).ToHaveTextAsync("Hi, I'm frame");
await Expect.That(Browser.FrameLocator("[name=\"2frames\"]").FrameLocator("[name=dos]").Locator("div")).ToHaveTextAsync("Hi, I'm frame");
await Expect.That(Browser.FrameLocator("[name=\"2frames\"]").FrameLocator("[name=uno]").Locator("div")).ToHaveTextAsync("Hi, I'm frame");
await Expect.That(Browser.FrameLocator("[name=\"2frames\"]").Locator("iframe")).ToHaveCountAsync(2);
await Expect.That(Browser.Locator("iframe")).ToHaveCountAsync(2);
}

[Test]
public async Task Partial_Should_send_framenavigated_when_navigating_on_anchor_URLs()
{
await Browser.GotoAsync("/empty.html");
await Browser.GotoAsync("/empty.html#foo");
Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/empty.html#foo"));
}

[Test]
public async Task Partial_Should_report_frame_from_inside_shadow_DOM()
{
await Browser.GotoAsync("/shadow.html");
await Browser.EvaluateAsync<object>("""
async (url) => {
const frame = document.createElement('iframe');
frame.src = url;
document.body.shadowRoot.appendChild(frame);
await new Promise((x) => (frame.onload = x));
}
""", $"{PREFIX}/empty.html");
await Expect.That(Browser.Locator("iframe")).ToHaveCountAsync(1);
await Expect.That(Browser.FrameLocator("iframe").Locator("body")).ToHaveCountAsync(1);
}
}
23 changes: 22 additions & 1 deletion tests/E2E.NUnit.Tests/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,34 @@ namespace E2E.NUnit.Tests;

/// <summary>
/// The helpers of the tests ported from Playwright, upstream's <c>apps/testbed/tests/helpers.ts</c>:
/// <c>content</c> and <c>PREFIX</c>. <see cref="ExpectedToFailAsync"/> is .NET-only (see its summary).
/// <c>content</c>, <c>PREFIX</c> and <c>CROSS</c>. <see cref="ExpectedToFailAsync"/> is .NET-only (see its summary).
/// </summary>
internal static class Helpers
{
/// <summary>The app's origin, Playwright's <c>server.PREFIX</c> in the tests ported from it.</summary>
public static string PREFIX => E2E.Playground.Testbed.Url;

/// <summary>Upstream's <c>delay(milliseconds).then(next)</c>.</summary>
public static async Task After(int milliseconds, Func<Task> next)
{
await Task.Delay(milliseconds).ConfigureAwait(false);
await next().ConfigureAwait(false);
}

/// <summary>The error <paramref name="act"/> threw, or null when it did not (upstream's <c>failure</c>).</summary>
public static async Task<Exception?> Record(Func<Task> act)
{
try
{
await act().ConfigureAwait(false);
return null;
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
return ex;
}
}

/// <summary>Another origin on the same app, Playwright's <c>server.CROSS_PROCESS_PREFIX</c>.</summary>
public static string CROSS => PREFIX.Replace("127.0.0.1", "localhost", StringComparison.Ordinal);

Expand Down
Loading
Loading