diff --git a/tests/E2E.Agent.Tests/PlaywrightRoutes.cs b/tests/E2E.Agent.Tests/PlaywrightRoutes.cs index 344f39e90..3cf9a77bf 100644 --- a/tests/E2E.Agent.Tests/PlaywrightRoutes.cs +++ b/tests/E2E.Agent.Tests/PlaywrightRoutes.cs @@ -14,11 +14,14 @@ namespace E2E.Playground; /// The routes of the tests ported from Playwright, a port of upstream's apps/testbed/app/playwright/routes.mjs: /// its test assets (copied from microsoft/playwright tests/assets at v1.64.1, Apache-2.0, under /// Testbed/playwright/assets 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 /content and the assets so far. +/// The playground serves them for any path it has no page for. This port has /content, /never and the assets so far. /// 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")); + + /// Cancelled when the process exits, which releases the requests a route holds. + private static readonly CancellationTokenSource Shutdown = CreateShutdown(); private static readonly Dictionary Types = new(StringComparer.Ordinal) { @@ -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; @@ -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; + } + /// Answers a multipart form with the files it carried as JSON, as upstream's /upload: name, filename, mimetype, size, and the bytes (base64) of a file up to 64 KB. private static async Task UploadAsync(HttpListenerContext context) { diff --git a/tests/E2E.Agent.Tests/Testbed.cs b/tests/E2E.Agent.Tests/Testbed.cs index 02118f198..2e78b38bf 100644 --- a/tests/E2E.Agent.Tests/Testbed.cs +++ b/tests/E2E.Agent.Tests/Testbed.cs @@ -915,11 +915,17 @@ function submitForm() { /// The base URL of the running playground, started on first use. public static string Url => Server.Value; + /// Whether the app also answers on localhost, the second origin of the cross-origin cases. Starts the app when it is not running yet. + 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); @@ -933,7 +939,6 @@ private static string Start() try { listener.Start(); - break; } catch (HttpListenerException) { @@ -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('/'); } @@ -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(); diff --git a/tests/E2E.NUnit.Tests/FrameGoto/FrameGotoTests.cs b/tests/E2E.NUnit.Tests/FrameGoto/FrameGotoTests.cs new file mode 100644 index 000000000..bf3d7df0f --- /dev/null +++ b/tests/E2E.NUnit.Tests/FrameGoto/FrameGotoTests.cs @@ -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; + +/// +/// test.describe('frame goto') of upstream's apps/testbed/tests/frame-goto.e2e.ts (Playwright's +/// frame-goto.spec.ts). The stalled /frames/script.js is a route that sends the request to /never. Not ported, +/// as upstream: should navigate subframes, should reject when frame detaches, should return matching responses (no +/// frame.goto and no Frame objects). +/// +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")); + } + }); + } +} diff --git a/tests/E2E.NUnit.Tests/FrameHierarchy/FrameHierarchyTests.cs b/tests/E2E.NUnit.Tests/FrameHierarchy/FrameHierarchyTests.cs new file mode 100644 index 000000000..572c479e7 --- /dev/null +++ b/tests/E2E.NUnit.Tests/FrameHierarchy/FrameHierarchyTests.cs @@ -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; + +/// +/// test.describe('frame hierarchy') of upstream's apps/testbed/tests/frame-hierarchy.e2e.ts (Playwright's +/// frame-hierarchy.spec.ts). A frame the original finds in page.frames() is reached through FrameLocator. 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). +/// +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(""" + 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); + } +} diff --git a/tests/E2E.NUnit.Tests/Helpers/Helpers.cs b/tests/E2E.NUnit.Tests/Helpers/Helpers.cs index 3626c5ec8..1112f1d7a 100644 --- a/tests/E2E.NUnit.Tests/Helpers/Helpers.cs +++ b/tests/E2E.NUnit.Tests/Helpers/Helpers.cs @@ -8,13 +8,34 @@ namespace E2E.NUnit.Tests; /// /// The helpers of the tests ported from Playwright, upstream's apps/testbed/tests/helpers.ts: -/// content and PREFIX. is .NET-only (see its summary). +/// content, PREFIX and CROSS. is .NET-only (see its summary). /// internal static class Helpers { /// The app's origin, Playwright's server.PREFIX in the tests ported from it. public static string PREFIX => E2E.Playground.Testbed.Url; + /// Upstream's delay(milliseconds).then(next). + public static async Task After(int milliseconds, Func next) + { + await Task.Delay(milliseconds).ConfigureAwait(false); + await next().ConfigureAwait(false); + } + + /// The error threw, or null when it did not (upstream's failure). + public static async Task Record(Func act) + { + try + { + await act().ConfigureAwait(false); + return null; + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + return ex; + } + } + /// Another origin on the same app, Playwright's server.CROSS_PROCESS_PREFIX. public static string CROSS => PREFIX.Replace("127.0.0.1", "localhost", StringComparison.Ordinal); diff --git a/tests/E2E.NUnit.Tests/LocatorFrame/LocatorFrameTests.cs b/tests/E2E.NUnit.Tests/LocatorFrame/LocatorFrameTests.cs new file mode 100644 index 000000000..aeaed09b9 --- /dev/null +++ b/tests/E2E.NUnit.Tests/LocatorFrame/LocatorFrameTests.cs @@ -0,0 +1,266 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.LocatorFrame; + +/// +/// test.describe('locator frame') of upstream's apps/testbed/tests/locator-frame.e2e.ts (Playwright's +/// locator-frame.spec.ts). Not ported, as upstream: should wait for frame (no locator.frameLocator), should non work +/// for non-frame (Playwright's error text), locator.frameLocator should work for iframe, locator.frameLocator should throw on +/// ambiguity, locator.frameLocator should not throw on first/last/nth, should work with COEP/COOP/CORP isolated iframe, +/// locator.contentFrame should work, frameLocator.owner should work. The getBy coverage test keeps the role, text, test id, label and +/// placeholder queries (no getByAltText or getByTitle). +/// +public sealed class LocatorFrameTests : PlaywrightTest +{ + + private static readonly string Nested = Content(""); + + private static readonly string Iframe = Content($""" + +
+ + +
+ 1 + 2 + + + """); + + private static readonly string Routed = Content($""""""); + + [Test] + public async Task Should_work_for_iframe() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button"); + await button.WaitForAsync(); + Assert.That(await button.TextContentAsync(), Is.EqualTo("Hello iframe")); + await Expect.That(button).ToHaveTextAsync("Hello iframe"); + await button.TapAsync(); + } + + [Test] + public async Task Should_work_for_nested_iframe() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").FrameLocator("iframe").Locator("button"); + await button.WaitForAsync(); + Assert.That(await button.TextContentAsync(), Is.EqualTo("Hello nested iframe")); + await Expect.That(button).ToHaveTextAsync("Hello nested iframe"); + await button.TapAsync(); + } + + [Test] + public async Task Should_work_for_Dollar_and_DollarDollar() + { + await Browser.GotoAsync(Routed); + var locator = Browser.FrameLocator("iframe").Locator("button"); + await Expect.That(locator).ToHaveTextAsync("Hello iframe"); + var spans = Browser.FrameLocator("iframe").Locator("span"); + await Expect.That(spans).ToHaveCountAsync(2); + } + + [Test] + public async Task Should_wait_for_frame_2() + { + await Browser.GotoAsync("about:blank"); + var navigated = After(300, () => Browser.GotoAsync(Routed)); + await Browser.FrameLocator("iframe").Locator("button").TapAsync(); + await navigated; + } + + [Test] + public async Task Should_wait_for_frame_to_go() + { + await Browser.GotoAsync(Routed); + var removed = After(300, () => Browser.EvaluateAsync("() => { document.querySelector('iframe').remove(); }")); + await Expect.That(Browser.FrameLocator("iframe").Locator("button")).ToBeHiddenAsync(); + await removed; + } + + [Test] + public async Task Should_not_wait_for_frame() + { + await Browser.GotoAsync("/empty.html"); + await Expect.That(Browser.FrameLocator("iframe").Locator("span")).ToBeHiddenAsync(); + } + + [Test] + public async Task Should_not_wait_for_frame_2() + { + await Browser.GotoAsync("/empty.html"); + await Expect.That(Browser.FrameLocator("iframe").Locator("span")).ToBeVisibleAsync(false); + } + + [Test] + public async Task Should_not_wait_for_frame_3() + { + await Browser.GotoAsync("/empty.html"); + await Expect.That(Browser.FrameLocator("iframe").Locator("span")).ToHaveCountAsync(0); + } + + [Test] + public async Task Should_click_in_lazy_iframe() + { + var lazy = Content(""); + await Browser.GotoAsync("/empty.html"); + var attached = After(500, async () => + { + await Browser.EvaluateAsync("() => { document.body.appendChild(document.createElement('iframe')); }"); + await Task.Delay(500); + await Browser.EvaluateAsync("(src) => { document.querySelector('iframe').src = src; }", lazy); + }); + var button = Browser.FrameLocator("iframe").Locator("button"); + var tap = button.TapAsync(); + var text = button.TextContentAsync(); + var hasText = Expect.That(button).ToHaveTextAsync("Hello iframe"); + await Task.WhenAll(tap, text, hasText, attached); + Assert.That(await text, Is.EqualTo("Hello iframe")); + } + + [Test] + public async Task WaitFor_should_survive_frame_reattach() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button").Filter(hasText: "Hello nested iframe"); + var promise = button.WaitForAsync(); + await Browser.EvaluateAsync("() => { document.querySelector('iframe').remove(); }"); + await Browser.EvaluateAsync("(src) => { const iframe = document.createElement('iframe'); iframe.src = src; document.body.appendChild(iframe); }", Nested); + await promise; + } + + [Test] + public async Task Click_should_survive_frame_reattach() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button").Filter(hasText: "Hello nested iframe"); + var promise = button.TapAsync(); + await Browser.EvaluateAsync("() => { document.querySelector('iframe').remove(); }"); + await Browser.EvaluateAsync("(src) => { const iframe = document.createElement('iframe'); iframe.src = src; document.body.appendChild(iframe); }", Nested); + await promise; + } + + [Test] + public async Task Click_should_survive_iframe_navigation() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button").Filter(hasText: "Hello nested iframe"); + var promise = button.TapAsync(); + var navigated = Browser.EvaluateAsync("(src) => { document.querySelector('iframe').src = src; }", Nested); + await promise; + await navigated; + } + + // Known difference, hardkoded/e2e-dotnet#1066: a frame-scoped getByRole also matches the nested frame's button. + [Test] + public async Task GetBy_coverage_role_text_test_id_label_placeholder() + { + await ExpectedToFailAsync("observed matched 2 nodes", async () => + { + await Browser.GotoAsync(Routed); + var button1 = Browser.FrameLocator("iframe").GetByRole("button"); + var button2 = Browser.FrameLocator("iframe").GetByText("Hello", exact: false); + var button3 = Browser.FrameLocator("iframe").GetByTestId("buttonId"); + await Expect.That(button1).ToHaveTextAsync("Hello iframe"); + await Expect.That(button2).ToHaveTextAsync("Hello iframe"); + await Expect.That(button3).ToHaveTextAsync("Hello iframe"); + var input1 = Browser.FrameLocator("iframe").GetByLabel("Name"); + await Expect.That(input1).ToHaveValueAsync(""); + var input2 = Browser.FrameLocator("iframe").GetByPlaceholder("Placeholder"); + await Expect.That(input2).ToHaveValueAsync(""); + }); + } + + [Test] + public async Task Wait_for_hidden_should_succeed_when_frame_is_not_in_dom() + { + await Browser.GotoAsync("about:blank"); + var button = Browser.FrameLocator("iframe1").Locator("button"); + Assert.That(await button.IsHiddenAsync(), Is.True); + await button.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Hidden, Timeout = TimeSpan.FromSeconds(1) }); + await button.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Detached, Timeout = TimeSpan.FromSeconds(1) }); + var error = await Assert.CatchAsync(() => button.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Attached, Timeout = TimeSpan.FromSeconds(1) })); + // Upstream only expects the error to be defined; the port's message names the missing frame. + Assert.That(error!.Message, Does.Contain("no frame matches iframe1")); + } + + [Test] + public async Task Should_treat_elements_inside_hidden_iframe_as_hidden() + { + await Browser.GotoAsync(Content(""" + +
+ + +
+ + """)); + + foreach (var button in new[] + { + Browser.FrameLocator("[name=hidden]").Locator("button"), + Browser.FrameLocator("[name=hidden-parent]").Locator("button"), + Browser.FrameLocator("[name=outer]").FrameLocator("[name=inner]").Locator("button"), + }) + { + await Expect.That(button).ToBeAttachedAsync(); + Assert.That(await button.IsVisibleAsync(), Is.False); + Assert.That(await button.IsHiddenAsync(), Is.True); + await Expect.That(button).ToBeHiddenAsync(); + await Expect.That(button).ToBeVisibleAsync(false); + await button.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Hidden }); + } + + var visible = Browser.FrameLocator("[name=visible-override]").Locator("button"); + Assert.That(await visible.IsVisibleAsync(), Is.True); + await Expect.That(visible).ToBeVisibleAsync(); + await visible.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Visible }); + } + + [Test] + public async Task Should_wait_for_hidden_iframe_to_become_visible() + { + await Browser.GotoAsync(Content("""""")); + var button = Browser.FrameLocator("[name=frame]").Locator("button"); + await Expect.That(button).ToBeHiddenAsync(); + + var error = await Record(() => button.TapAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(1) })); + Assert.That(error, Is.Not.Null); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('iframe').contentWindow.__clicked === true"), Is.False); + + var clickPromise = button.TapAsync(); + var visiblePromise = Expect.That(button).ToBeVisibleAsync(); + await Browser.EvaluateAsync("() => { document.querySelector('iframe').style.visibility = 'visible'; }"); + await Task.WhenAll(clickPromise, visiblePromise); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('iframe').contentWindow.__clicked"), Is.True); + + await Browser.EvaluateAsync("() => { document.querySelector('iframe').style.visibility = 'hidden'; }"); + await Expect.That(button).ToBeHiddenAsync(); + Assert.That(await button.IsVisibleAsync(), Is.False); + } + + [Test] + public async Task Should_treat_elements_inside_hidden_cross_origin_iframe_as_hidden() + { + if (!E2E.Playground.Testbed.CrossOriginAvailable) + { + Assert.Ignore("The testbed could not bind localhost, the second origin of this test (CROSS)."); + } + + await Browser.GotoAsync("/empty.html"); + await Browser.GotoAsync(Content($"""""")); + var div = Browser.FrameLocator("iframe").Locator("div"); + await Expect.That(div).ToHaveTextAsync("Hi, I'm frame"); + Assert.That(await div.IsVisibleAsync(), Is.False); + await Expect.That(div).ToBeHiddenAsync(); + + await Browser.EvaluateAsync("() => { document.querySelector('iframe').style.visibility = 'visible'; }"); + await Expect.That(div).ToBeVisibleAsync(); + Assert.That(await div.IsVisibleAsync(), Is.True); + } +} diff --git a/tests/E2E.NUnit.Tests/SelectorsFrame/SelectorsFrameTests.cs b/tests/E2E.NUnit.Tests/SelectorsFrame/SelectorsFrameTests.cs new file mode 100644 index 000000000..6e468c92f --- /dev/null +++ b/tests/E2E.NUnit.Tests/SelectorsFrame/SelectorsFrameTests.cs @@ -0,0 +1,170 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.SelectorsFrame; + +/// +/// test.describe('selectors frame') of upstream's apps/testbed/tests/selectors-frame.e2e.ts (Playwright's +/// selectors-frame.spec.ts). iframe >> internal:control=enter-frame >> x is +/// FrameLocator("iframe").Locator(x). Not ported, as upstream: the (handle) tests (element handles), $eval should throw for +/// missing frame (no $eval), should not allow dangling/leading enter-frame, should not allow capturing before enter-frame, +/// should capture after the enter-frame (Playwright selector syntax), should non work for non-frame, and the nine any-frame +/// tests (no counterpart). +/// +public sealed class SelectorsFrameTests : PlaywrightTest +{ + private static readonly string Iframe2 = Content(""); + + private static readonly string Iframe = Content($""" + +
+ + +
+ 1 + 2 + + """); + + private static readonly string Routed = Content($""""""); + + [Test] + public async Task Should_work_for_iframe() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button"); + await button.WaitForAsync(); + Assert.That(await button.TextContentAsync(), Is.EqualTo("Hello iframe")); + await Expect.That(button).ToHaveTextAsync("Hello iframe"); + await button.TapAsync(); + } + + [Test] + public async Task Should_work_for_nested_iframe() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").FrameLocator("iframe").Locator("button"); + await button.WaitForAsync(); + Assert.That(await button.TextContentAsync(), Is.EqualTo("Hello nested iframe")); + await Expect.That(button).ToHaveTextAsync("Hello nested iframe"); + await button.TapAsync(); + } + + [Test] + public async Task Should_work_for_Dollar_and_DollarDollar() + { + await Browser.GotoAsync(Routed); + var element = Browser.FrameLocator("iframe").Locator("button"); + await element.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Attached }); + Assert.That(await element.TextContentAsync(), Is.EqualTo("Hello iframe")); + await Browser.FrameLocator("iframe").Locator("span").Nth(1).WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Attached }); + Assert.That(await Browser.FrameLocator("iframe").Locator("span").CountAsync(), Is.EqualTo(2)); + } + + [Test] + public async Task Dollar_should_not_wait_for_frame() + { + await Browser.GotoAsync("/empty.html"); + Assert.That(await Browser.FrameLocator("iframe").Locator("canvas").CountAsync(), Is.EqualTo(0)); + } + + [Test] + public async Task DollarDollar_should_not_wait_for_frame() + { + await Browser.GotoAsync("/empty.html"); + Assert.That(await Browser.FrameLocator("iframe").Locator("canvas").AllAsync(), Has.Count.EqualTo(0)); + } + + [Test] + public async Task DollarDollarEval_should_not_throw_for_missing_frame() + { + await Browser.GotoAsync("/empty.html"); + Assert.That(await Browser.FrameLocator("iframe").Locator("canvas").AllTextContentsAsync(), Has.Count.EqualTo(0)); + } + + // Known difference, hardkoded/e2e-dotnet#1066: a frame-scoped getByRole also matches the nested frame's button. + [Test] + public async Task Should_work_for_DollarEval() + { + await ExpectedToFailAsync("observed count 2", async () => + { + await Browser.GotoAsync(Routed); + await Expect.That(Browser.FrameLocator("iframe").GetByRole("button")).ToHaveCountAsync(1); + await Expect.That(Browser.FrameLocator("iframe").Locator("button")).ToHaveCountAsync(1); + }); + } + + [Test] + public async Task Should_work_for_DollarDollarEval() + { + await Browser.GotoAsync(Routed); + await Expect.That(Browser.FrameLocator("iframe").Locator("span")).ToHaveCountAsync(2); + Assert.That(await Browser.FrameLocator("iframe").Locator("span").AllTextContentsAsync(), Is.EqualTo(new[] { "1", "2" })); + } + + [Test] + public async Task Should_click_in_lazy_iframe() + { + var lazy = Content(""); + await Browser.GotoAsync("/empty.html"); + var attached = After(500, async () => + { + await Browser.EvaluateAsync("() => { document.body.appendChild(document.createElement('iframe')); }"); + await Task.Delay(500); + await Browser.EvaluateAsync("(src) => { document.querySelector('iframe').src = src; }", lazy); + }); + var button = Browser.FrameLocator("iframe").Locator("button"); + var tap = button.TapAsync(); + var text = button.TextContentAsync(); + var hasText = Expect.That(button).ToHaveTextAsync("Hello iframe"); + await Task.WhenAll(tap, text, hasText, attached); + Assert.That(await text, Is.EqualTo("Hello iframe")); + } + + [Test] + public async Task WaitFor_should_survive_frame_reattach() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button").Filter(hasText: "Hello nested iframe"); + var promise = button.WaitForAsync(); + await Browser.EvaluateAsync("() => { document.querySelector('iframe').remove(); }"); + await Browser.EvaluateAsync("(src) => { const iframe = document.createElement('iframe'); iframe.src = src; document.body.appendChild(iframe); }", Iframe2); + await promise; + } + + [Test] + public async Task Click_should_survive_frame_reattach() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button").Filter(hasText: "Hello nested iframe"); + var promise = button.TapAsync(); + await Browser.EvaluateAsync("() => { document.querySelector('iframe').remove(); }"); + await Browser.EvaluateAsync("(src) => { const iframe = document.createElement('iframe'); iframe.src = src; document.body.appendChild(iframe); }", Iframe2); + await promise; + } + + [Test] + public async Task Click_should_survive_iframe_navigation() + { + await Browser.GotoAsync(Routed); + var button = Browser.FrameLocator("iframe").Locator("button").Filter(hasText: "Hello nested iframe"); + var promise = button.TapAsync(); + var navigated = Browser.EvaluateAsync("(src) => { document.querySelector('iframe').src = src; }", Iframe2); + await promise; + await navigated; + } + + [Test] + public async Task Click_should_survive_navigation() + { + await Browser.GotoAsync(Iframe); + var promise = Browser.Locator("button").Filter(hasText: "Hello nested iframe").TapAsync(); + // Upstream waits 100 ms; the page being loaded and the tap polling for a button it does not have yet is the condition. + await Expect.That(Screen.GetByRole("button", "Hello iframe")).ToBeVisibleAsync(); + await Browser.GotoAsync(Iframe2); + await promise; + } +} diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/child-redirect.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/child-redirect.html new file mode 100644 index 000000000..d4806e7f6 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/child-redirect.html @@ -0,0 +1 @@ + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/nested-frames.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/nested-frames.html new file mode 100644 index 000000000..d858d3285 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/nested-frames.html @@ -0,0 +1,30 @@ + + + + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/one-frame.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/one-frame.html new file mode 100644 index 000000000..e941d795a --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/one-frame.html @@ -0,0 +1 @@ + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/redirect-my-parent.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/redirect-my-parent.html new file mode 100644 index 000000000..3beb0aac1 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/redirect-my-parent.html @@ -0,0 +1,3 @@ + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/shadow.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/shadow.html new file mode 100644 index 000000000..d1fd331eb --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/shadow.html @@ -0,0 +1,17 @@ +