diff --git a/COMPATIBILITY.md b/COMPATIBILITY.md index a2757e433..ca442079e 100644 --- a/COMPATIBILITY.md +++ b/COMPATIBILITY.md @@ -162,7 +162,7 @@ Differences: - Upstream's docs site pages that have no counterpart in the port: the page objects page (`docs/page-objects.mdx`, moved to `/page-object-model` in `d30114a`), `e2e feedback` and the `docs/reference/cli.mdx` text about its opt-out variables (`4c1e2e8`), and `examples/` with its registry (`b3ae3a4`, `38b802d`, `a29edcb`, `b54646f`). The migration guides (`docs/migrate/*.mdx`, `8537abc`) are ported: see [Migration guides](#migration-guides). The docs site has .NET-only guides for upstream's five example apps of `caf51eb` (tester-army/e2e#1497: i18n, file upload, location, biometrics, deep links), under [Examples](docs-site/src/content/docs/examples/index.mdx). They are not ports: that commit only adds registry entries that link to separate repositories, so there was no guide text. Each page writes C# tests for the upstream app. The self-healing deep dive (`6fa99ea`) is ported as the [Self-healing tests](README.md#self-healing-tests) section of the guide, since the port has no docs pages for the cache, the trace page, or executors to link to. Known gap: only `e2e explore` prints a run summary, since it is the one command that feeds a run to `ListReporter` (see [List reporter](#list-reporter)): the `Cache 4 replayed · 1 handed off · 1 missed` row is `Format.CacheText` over `Format.StepsCacheTally` (`E2E.Cli`, upstream's `cacheText` and `stepsCacheTally` of `report/format.ts`), which an exploration, with its cache off, never fills, and a test run does not print ([#799](https://github.com/hardkoded/e2e-dotnet/issues/799)) - Parallel workers and serial suites - `test.fail` and the `fail` test option (tester-army/e2e#1539, `732ea8a`): a test expected to fail, whose product failure passes and whose pass fails with `UNEXPECTED_PASS`. It lives in upstream's test registry, scheduler, retry, and reporters (`settleExpectedFailure` runs on a finished attempt), and the port has none of them. NUnit and xUnit v3 own the verdict and the retries, and neither has an expected-failure outcome, so a port would need a new public API of its own -- Playwright's page tests in the testbed suite (tester-army/e2e#1505, `bdf769b`) are ported in slices (hardkoded/e2e-dotnet#1060), as the `playwright` category of `tests/E2E.NUnit.Tests`, against the playground's `/content` route and Playwright's test assets (Apache-2.0, `Testbed/playwright/`, with Playwright's `LICENSE` and `NOTICE`). A test upstream marks `{ fail: 'reason' }` keeps its assertions inside the test-only `Helpers.ExpectedToFailAsync`, which passes while the body fails and fails once it passes. Ported so far: `check`, `wait-for-url`, `locator-list`, `selectors-text`, `fill`, `select-option`, `set-input-files`, and `filechooser`. Upstream's `select-option` test marked `fail` (a bare string selects by label only) passes in the port, which matches a value or a label, so it has no marker. A missing file reports .NET's `Could not find file`, where upstream shows Node's `ENOENT`. +- Playwright's page tests in the testbed suite (tester-army/e2e#1505, `bdf769b`) are ported in slices (hardkoded/e2e-dotnet#1060), as the `playwright` category of `tests/E2E.NUnit.Tests`, against the playground's `/content` route and Playwright's test assets (Apache-2.0, `Testbed/playwright/`, with Playwright's `LICENSE` and `NOTICE`). A test upstream marks `{ fail: 'reason' }` keeps its assertions inside the test-only `Helpers.ExpectedToFailAsync`, which passes while the body fails and fails once it passes. Ported so far: `check`, `wait-for-url`, `locator-list`, `selectors-text`, `fill`, `select-option`, `set-input-files`, `filechooser`, `click`, `click-scroll`, and `elementhandle-scroll-into-view` (three known differences: hardkoded/e2e-dotnet#1079, #1080, #1081). Upstream's `select-option` test marked `fail` (a bare string selects by label only) passes in the port, which matches a value or a label, so it has no marker. A missing file reports .NET's `Could not find file`, where upstream shows Node's `ENOENT`. - Serial groups (upstream tester-army/e2e#1056, #1470): serial runs, group retries, and skipping the rest of a group after a failure belong to the test runner, which is NUnit or xUnit here, so the port keeps no serial groups. The schema has `serialAttempt.cutRetry` and the serial verdict is read from it, but nothing in the port writes a serial attempt: see [Report document](#report-document) - The out-of-process engine protocol (upstream tester-army/e2e#1232, [#873](https://github.com/hardkoded/e2e-dotnet/issues/873)): the port has no runner and no worker pool, so there is no engine process to talk to. An engine is written in .NET against `IEngine` - Diff-only observations for a custom executor's own model turns. The built-in agent has them (#1022): `#id` lines on the opening screen and on `observe`, and a diff after each action (see [MCP server](#mcp-server) for the presenter). `ExecutorObservation.Text`, which an executor reads from `Observe`, keeps the full `[ref=…]` snapshot, cut at the observation byte budget (see [Agents](#agents)), and a node is addressed by its ref there (`new ExecutorTarget("e12")`), so a custom executor has no presenter of its own. Upstream gives every executor the `#id` view. diff --git a/tests/E2E.NUnit.Tests/Click/PageClickTests.cs b/tests/E2E.NUnit.Tests/Click/PageClickTests.cs new file mode 100644 index 000000000..7f340513a --- /dev/null +++ b/tests/E2E.NUnit.Tests/Click/PageClickTests.cs @@ -0,0 +1,708 @@ +// 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.Click; + +/// +/// test.describe('page click') of upstream's apps/testbed/tests/click.e2e.ts (Playwright's page-click.spec.ts). +/// TapAsync stands for page.click. Not ported, as upstream: should click button inside frameset; should issue clicks in +/// parallel in page and popup; should select the text by triple clicking; should waitFor visible when already visible; should not wait with +/// force; should click the button with fixed position inside an iframe; should click the button with px border with offset; should click the +/// button with em border with offset; should click a very large button with offset; should click a button in scrolling container with +/// offset; should wait for becoming hit target with trial run; trial run should work with short timeout; trial run should not click; trial +/// run should not double click; should fail when obscured and not waiting for hit target; should wait for LABEL to be clickable when it has +/// pointer-events:none; should report nice error when element is detached and force-clicked; should fail when element detaches after +/// animation; should retry when element detaches after animation; should retry when element is animating from outside the viewport; should +/// fail when element is animating from outside the viewport with force; should dispatch microtasks in order; should not throw protocol error +/// when navigating during the click; should retry when navigating during the click; should not hang when frame is detached; should click in +/// an iframe with border 2; should not retain removed iframe after clicking inside it; should click a button that is overlaid by a +/// permission popup; should click in a transformed iframe with force; ensure events are dispatched in the individual tasks; should set +/// PointerEvent.pressure on pointerdown; should set PointerEvent.pressure on pointermove; should click with tweened mouse movement; should +/// abort via signal; should throw an Error when aborted in-flight with a string reason; should abort via already-aborted signal; should +/// throw an Error when aborted via an already-aborted signal with a string reason (no force, trial, position scrolling, +/// clickCount, steps, element handles, Frame objects, events, or abort signals; some upstream left undone). +/// +public sealed class PageClickTests : PlaywrightTest +{ + /// Two animation frames and a task, Playwright's rafraf, so a pending tap has had its chance. + private const string GiveItAChanceToClick = + "() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(resolve, 0))))"; + + private static readonly ActionOptions Ten = new() { Timeout = TimeSpan.FromSeconds(10) }; + + private async Task ResultAsync() => (await Browser.EvaluateAsync("result"))!; + + [Test] + public async Task Should_click_the_button() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.Locator("button").TapAsync(); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_svg() + { + await Browser.GotoAsync(Content(""" + + + + """)); + await Browser.Locator("circle").TapAsync(); + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.EqualTo(42)); + } + + [Test] + public async Task Should_click_the_button_if_window_Node_is_removed() + { + // Known difference (hardkoded/e2e-dotnet#1079): the page script reads window.Node. + await ExpectedToFailAsync("Node is not defined", async () => + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync("() => { delete window.Node; }"); + await Browser.Locator("button").TapAsync(); + Equal("Clicked", await ResultAsync()); + }); + } + + [Test] + public async Task Should_click_on_a_span_with_an_inline_element_inside() + { + await Browser.GotoAsync(Content(""" + + + """)); + await Browser.Locator("span").TapAsync(); + Assert.That(await Browser.EvaluateAsync("CLICKED"), Is.EqualTo(42)); + } + + [TestCase("aligned", "")] + [TestCase("half-aligned", "margin-left: 20.5px; margin-top: 11.5px;")] + [TestCase("unaligned v1", "margin-left: 20.23px; margin-top: 11.65px;")] + [TestCase("unaligned v2", "margin-left: 20.68px; margin-top: 11.13px;")] + [TestCase("unaligned v3", "margin-left: 20.68px; margin-top: 11.52px;")] + [TestCase("unaligned v4", "margin-left: 20.15px; margin-top: 11.24px;")] + public async Task Should_click_the_1x1_div(string name, string style) + { + await Browser.GotoAsync(Content($"""
""")); + await Browser.Locator("div").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window.__clicked"), Is.True, $"should click the {name} 1x1 div"); + } + + [Test] + public async Task Should_click_the_button_after_navigation() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.Locator("button").TapAsync(); + await Browser.GotoAsync("/input/button.html"); + await Browser.Locator("button").TapAsync(); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_the_button_after_a_cross_origin_navigation() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.Locator("button").TapAsync(); + await Browser.GotoAsync($"{CROSS}/input/button.html"); + await Browser.Locator("button").TapAsync(); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_when_one_of_inline_box_children_is_outside_of_viewport() + { + await Browser.GotoAsync(Content(""" + + woofdoggo + """)); + await Browser.Locator("span").TapAsync(); + Assert.That(await Browser.EvaluateAsync("CLICKED"), Is.EqualTo(42)); + } + + [Test] + public async Task Should_click_offscreen_buttons() + { + await Browser.GotoAsync("/offscreenbuttons.html"); + await Browser.EvaluateAsync("() => { window.messages = []; console.log = (text) => window.messages.push(text); }"); + for (var index = 0; index < 11; ++index) + { + await Browser.EvaluateAsync("() => window.scrollTo(0, 0)"); + await Browser.Locator($"#btn{index}").TapAsync(); + } + + Assert.That( + await Browser.EvaluateAsync("messages"), + Is.EqualTo(Enumerable.Range(0, 11).Select(index => $"button #{index} clicked").ToArray())); + } + + [Test] + public async Task Should_waitFor_display_none_to_be_gone() + { + await WaitForPropertyToBeGoneAsync("display", "none", "block"); + } + + [Test] + public async Task Should_waitFor_visibility_hidden_to_be_gone() + { + await WaitForPropertyToBeGoneAsync("visibility", "hidden", "visible"); + } + + private async Task WaitForPropertyToBeGoneAsync(string property, string hidden, string shown) + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync($"() => {{ document.querySelector('button').style.{property} = '{hidden}'; }}"); + var done = false; + var clicked = Task.Run(async () => + { + await Browser.Locator("button").TapAsync(Ten); + done = true; + }); + await Browser.EvaluateAsync(GiveItAChanceToClick); + Assert.That(await ResultAsync(), Is.EqualTo("Was not clicked")); + Assert.That(done, Is.False); + await Browser.EvaluateAsync($"() => {{ document.querySelector('button').style.{property} = '{shown}'; }}"); + await clicked; + Assert.That(done, Is.True); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_waitFor_visible_when_parent_is_hidden() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync("() => { document.querySelector('button').parentElement.style.display = 'none'; }"); + var done = false; + var clicked = Task.Run(async () => + { + await Browser.Locator("button").TapAsync(Ten); + done = true; + }); + await Browser.EvaluateAsync(GiveItAChanceToClick); + Assert.That(done, Is.False); + await Browser.EvaluateAsync("() => { document.querySelector('button').parentElement.style.display = 'block'; }"); + await clicked; + Assert.That(done, Is.True); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_wrapped_links() + { + await Browser.GotoAsync("/wrappedlink.html"); + await Browser.Locator("a").TapAsync(); + Assert.That(await Browser.EvaluateAsync("__clicked"), Is.True); + } + + [Test] + public async Task Should_click_on_checkbox_input_and_toggle() + { + await Browser.GotoAsync("/input/checkbox.html"); + Assert.That(await Browser.EvaluateAsync("() => window.result.check"), Is.Null); + await Browser.Locator("input#agree").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.result.check"), Is.True); + Assert.That( + await Browser.EvaluateAsync("() => window.result.events"), + Is.EqualTo(new[] { "mouseover", "mouseenter", "mousemove", "mousedown", "mouseup", "click", "input", "change" })); + await Browser.Locator("input#agree").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.result.check"), Is.False); + } + + [Test] + public async Task Should_click_on_checkbox_label_and_toggle() + { + await Browser.GotoAsync("/input/checkbox.html"); + Assert.That(await Browser.EvaluateAsync("() => window.result.check"), Is.Null); + await Browser.Locator("label[for=\"agree\"]").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.result.check"), Is.True); + Assert.That(await Browser.EvaluateAsync("() => window.result.events"), Is.EqualTo(new[] { "click", "input", "change" })); + await Browser.Locator("label[for=\"agree\"]").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.result.check"), Is.False); + } + + [Test] + public async Task Should_scroll_and_click_the_button() + { + await Browser.GotoAsync("/input/scrollable.html"); + await Browser.Locator("#button-5").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('#button-5').textContent"), Is.EqualTo("clicked")); + await Browser.Locator("#button-80").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('#button-80').textContent"), Is.EqualTo("clicked")); + } + + [Test] + public async Task Should_scroll_and_click_the_button_with_smooth_scroll_behavior() + { + await Browser.GotoAsync("/input/scrollable.html"); + await Browser.EvaluateAsync("() => { const style = document.createElement('style'); style.textContent = 'html { scroll-behavior: smooth; }'; document.head.append(style); }"); + for (var index = 0; index < 5; index++) + { + await Browser.Locator("#button-80").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('#button-80').textContent"), Is.EqualTo("clicked")); + await Browser.Locator("#button-20").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('#button-20').textContent"), Is.EqualTo("clicked")); + } + } + + [Test] + public async Task Should_double_click_the_button() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync("() => { window.double = false; document.querySelector('button').addEventListener('dblclick', () => { window.double = true; }); }"); + await Browser.Locator("button").DoubleTapAsync(); + Assert.That(await Browser.EvaluateAsync("double"), Is.True); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_a_partially_obscured_button() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync("() => { const button = document.querySelector('button'); button.textContent = 'Some really long text that will go offscreen'; button.style.position = 'absolute'; button.style.left = '368px'; }"); + await Browser.Locator("button").TapAsync(); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_a_rotated_button() + { + await Browser.GotoAsync("/input/rotatedButton.html"); + await Browser.Locator("button").TapAsync(); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_fire_contextmenu_event_on_right_click() + { + await Browser.GotoAsync("/input/scrollable.html"); + await Browser.Locator("#button-8").SecondaryTapAsync(); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('#button-8').textContent"), Is.EqualTo("context menu")); + } + + [Test] + public async Task Should_click_links_which_cause_navigation() + { + await Browser.GotoAsync(Content("empty.html")); + await Browser.Locator("a").TapAsync(); + await Browser.WaitForURLAsync("/empty.html"); + } + + [Test] + public async Task Should_click_the_button_inside_an_iframe() + { + await Browser.GotoAsync(Content("
spacer
")); + var frame = Browser.FrameLocator("iframe"); + await frame.Locator("button").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('iframe').contentWindow.result"), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_the_button_behind_sticky_header() + { + await Browser.SetViewportAsync(500, 240); + await Browser.GotoAsync(Content(""" + +
+
    +
  1. hi1
  2. hi2
  3. hi3
  4. hi4
  5. hi5
  6. hi6
  7. hi7
  8. hi8
  9. +
  10. hi9
  11. +
  12. hi10
  13. hi11
  14. hi12
  15. hi13
  16. hi14
  17. +
+ """)); + await Browser.EvaluateAsync("() => document.querySelector('#li14').scrollIntoView()"); + await Browser.Locator("#target").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.__clicked"), Is.True); + } + + [Test] + public async Task Should_click_the_button_behind_position_absolute_header() + { + await Browser.SetViewportAsync(500, 240); + await Browser.GotoAsync(Content(""" + +
    +
  1. hi1
  2. hi2
  3. hi3
  4. hi4
  5. hi5
  6. hi6
  7. hi7
  8. hi8
  9. +
  10. hi9
  11. +
  12. hi10
  13. hi11
  14. hi12
  15. hi13
  16. hi14
  17. +
+
Overlay
+ """)); + await Browser.EvaluateAsync("() => { const target = document.querySelector('#target'); document.querySelector('ol').scrollTo({ top: target.offsetTop, behavior: 'instant' }); }"); + await Browser.Locator("#target").TapAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.__clicked"), Is.True); + } + + [Test] + public async Task Should_wait_for_stable_position() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync(""" + () => { + const button = document.querySelector('button'); + button.style.transition = 'margin 500ms linear 0s'; + button.style.marginLeft = '200px'; + button.style.borderWidth = '0'; + button.style.width = '200px'; + button.style.height = '20px'; + button.style.display = 'block'; + document.body.style.margin = '0'; + } + """); + await Browser.EvaluateAsync(GiveItAChanceToClick); + await Browser.Locator("button").TapAsync(); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + Assert.That(await Browser.EvaluateAsync("pageX"), Is.EqualTo(300)); + Assert.That(await Browser.EvaluateAsync("pageY"), Is.EqualTo(10)); + } + + [Test] + public async Task Should_wait_for_becoming_hit_target() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync(""" + () => { + const button = document.querySelector('button'); + button.style.borderWidth = '0'; + button.style.width = '200px'; + button.style.height = '20px'; + document.body.style.margin = '0'; + document.body.style.position = 'relative'; + const flyOver = document.createElement('div'); + flyOver.className = 'flyover'; + flyOver.style.position = 'absolute'; + flyOver.style.width = '400px'; + flyOver.style.height = '20px'; + flyOver.style.left = '-200px'; + flyOver.style.top = '0'; + flyOver.style.background = 'red'; + document.body.appendChild(flyOver); + } + """); + var clicked = false; + var tap = Task.Run(async () => + { + await Browser.Locator("button").TapAsync(Ten); + clicked = true; + }); + Assert.That(clicked, Is.False); + await Browser.EvaluateAsync("() => { document.querySelector('.flyover').style.left = '0'; }"); + await Browser.EvaluateAsync(GiveItAChanceToClick); + Assert.That(clicked, Is.False); + await Browser.EvaluateAsync("() => { document.querySelector('.flyover').style.left = '200px'; }"); + await tap; + Assert.That(clicked, Is.True); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [TestCase("button", "", "document.querySelector('button').removeAttribute('disabled')")] + [TestCase("input", "", "document.querySelector('input').removeAttribute('disabled')")] + public async Task Should_wait_for_element_to_be_enabled(string name, string html, string enable) + { + await Browser.GotoAsync(Content(html)); + var done = false; + var target = name == "button" ? Screen.GetByText("Click target") : Browser.Locator(name); + var tap = Task.Run(async () => + { + await target.TapAsync(Ten); + done = true; + }); + await Browser.EvaluateAsync(GiveItAChanceToClick); + Assert.That(await Browser.EvaluateAsync("() => window.__CLICKED"), Is.Null); + Assert.That(done, Is.False); + await Browser.EvaluateAsync($"() => {{ {enable}; }}"); + await tap; + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True, $"should wait for {name} to be enabled"); + } + + [Test] + public async Task Should_wait_for_select_to_be_enabled() + { + await Browser.GotoAsync(Content(""" + + + """)); + var done = false; + var tap = Task.Run(async () => + { + await Browser.Locator("select").TapAsync(Ten); + done = true; + }); + await Browser.EvaluateAsync(GiveItAChanceToClick); + Assert.That(await Browser.EvaluateAsync("() => window.__CLICKED"), Is.Null); + Assert.That(done, Is.False); + await Browser.EvaluateAsync("() => { document.querySelector('select').removeAttribute('disabled'); }"); + await tap; + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True); + } + + [Test] + public async Task Should_click_disabled_div() + { + await Browser.GotoAsync(Content("
Click target
")); + await Screen.GetByText("Click target").TapAsync(); + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True); + } + + [Test] + public async Task Should_wait_for_BUTTON_to_be_clickable_when_it_has_pointer_events_none() + { + await Browser.GotoAsync(Content("")); + var done = false; + var tap = Task.Run(async () => + { + await Screen.GetByText("Click target").TapAsync(Ten); + done = true; + }); + await Browser.EvaluateAsync(GiveItAChanceToClick); + Assert.That(await Browser.EvaluateAsync("() => window.__CLICKED"), Is.Null); + Assert.That(done, Is.False); + await Browser.EvaluateAsync("() => { document.querySelector('button').style.removeProperty('pointer-events'); }"); + await tap; + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True); + } + + [Test] + public async Task Should_update_modifiers_correctly() + { + await Browser.GotoAsync("/input/button.html"); + var button = Browser.Locator("button"); + await button.TapAsync(new ClickOptions { Modifiers = [KeyModifier.Shift] }); + Assert.That(await Browser.EvaluateAsync("shiftKey"), Is.True); + await button.TapAsync(); + Assert.That(await Browser.EvaluateAsync("shiftKey"), Is.False); + await Browser.Keyboard.DownAsync("Shift"); + await button.TapAsync(); + Assert.That(await Browser.EvaluateAsync("shiftKey"), Is.True); + await Browser.Keyboard.UpAsync("Shift"); + await button.TapAsync(); + Assert.That(await Browser.EvaluateAsync("shiftKey"), Is.False); + } + + [Test] + public async Task Should_click_an_offscreen_element_when_scroll_behavior_is_smooth() + { + await Browser.GotoAsync(Content(""" +
+ +
+ """)); + await Browser.Locator("button").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window.clicked"), Is.True); + } + + [Test] + public async Task Should_click_the_button_when_window_innerWidth_is_corrupted() + { + await Browser.GotoAsync("/input/button.html"); + await Browser.EvaluateAsync("() => { Object.defineProperty(window, 'innerWidth', { value: 0 }); }"); + await Browser.Locator("button").TapAsync(); + Assert.That(await ResultAsync(), Is.EqualTo("Clicked")); + } + + [Test] + public async Task Should_click_zero_sized_input_by_label() + { + // Known difference (hardkoded/e2e-dotnet#1080): the label is not actionable. + await ExpectedToFailAsync("not visible", async () => + { + await Browser.GotoAsync(Content(""" + + """)); + await Screen.GetByText("Click me").TapAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(3) }); + Equal(true, await Browser.EvaluateAsync("window.__clicked")); + }); + } + + [Test] + public async Task Should_climb_dom_for_inner_label_with_pointer_events_none() + { + await Browser.GotoAsync(Content("")); + await Screen.GetByText("Click target").TapAsync(); + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True); + } + + [Test] + public async Task Should_climb_up_to_role_button() + { + await Browser.GotoAsync(Content("
Click target
")); + await Screen.GetByText("Click target").TapAsync(); + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True); + } + + [Test] + public async Task Should_climb_up_to_a_anchor() + { + await Browser.GotoAsync(Content("
Inner
")); + await Browser.Locator("#inner").TapAsync(); + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True); + } + + [Test] + public async Task Should_climb_up_to_a_role_link() + { + await Browser.GotoAsync(Content("
Inner
")); + await Browser.Locator("#inner").TapAsync(); + Assert.That(await Browser.EvaluateAsync("__CLICKED"), Is.True); + } + + [Test] + public async Task Should_click_in_an_iframe_with_border() + { + await Browser.GotoAsync(Content(""" + + + """)); + await Browser.FrameLocator("iframe").Locator("div").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window._clicked"), Is.True); + } + + [Test] + public async Task Should_click_in_a_transformed_iframe() + { + await Browser.GotoAsync(Content(""" + + + """)); + await Browser.FrameLocator("iframe").Locator("div").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window._clicked"), Is.True); + } + + [Test] + public async Task Should_click_in_a_nested_transformed_iframe() + { + await Browser.GotoAsync(Content(""" + + + """)); + await Browser.FrameLocator("iframe").FrameLocator("iframe").Locator("div").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window._clicked"), Is.True); + } + + [Test] + public async Task Should_click_if_opened_select_covers_the_button() + { + await Browser.GotoAsync(Content(""" +
+ +
+
+ """)); + await Browser.Locator("select").TapAsync(); + await Browser.Locator("button").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window.__CLICKED"), Is.EqualTo(42)); + } + + [Test] + public async Task Should_fire_contextmenu_event_on_right_click_in_correct_order() + { + await Browser.GotoAsync(Content("")); + await Browser.EvaluateAsync("() => { window.entries = []; const log = (event) => window.entries.push(event.type); for (const type of ['mousedown', 'mouseup', 'contextmenu']) document.addEventListener(type, log); }"); + await Screen.GetByRole("button", "Click me").SecondaryTapAsync(); + await Expect.Poll(() => Browser.EvaluateAsync("entries")).ToMatchAsync(Is.EqualTo(new[] { "mousedown", "contextmenu", "mouseup" })); + } + + [Test] + public async Task Should_click_after_a_right_click() + { + await Browser.GotoAsync(Content(""" + + + """)); + await Screen.GetByRole("button").SecondaryTapAsync(); + await Screen.GetByRole("button").TapAsync(); + await Expect.That(Screen.GetByRole("button")).ToHaveTextAsync("Clicked!"); + } + + [Test] + public async Task Should_click_into_shadow_root_with_slotted_div() + { + await Browser.GotoAsync(Content(""" + + +
Foo
+
+ """)); + await Screen.GetByRole("button", "Foo").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window.__clicked"), Is.True); + } + + [Test] + public async Task Should_click_shadow_root_button() + { + await Browser.GotoAsync(Content(""" + + +
Foo
+
+ """)); + await Browser.Locator("my-button").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window.__clicked"), Is.True); + } +} diff --git a/tests/E2E.NUnit.Tests/ClickScroll/PageClickScrollTests.cs b/tests/E2E.NUnit.Tests/ClickScroll/PageClickScrollTests.cs new file mode 100644 index 000000000..74f43d2ed --- /dev/null +++ b/tests/E2E.NUnit.Tests/ClickScroll/PageClickScrollTests.cs @@ -0,0 +1,113 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.ClickScroll; + +/// +/// test.describe('page click scroll') of upstream's apps/testbed/tests/click-scroll.e2e.ts (Playwright's +/// page-click-scroll.spec.ts). TapAsync stands for page.click, and a text= selector is a substring +/// GetByText. Not ported, as upstream: should not crash when force-clicking hidden input (no force); should not scroll the page +/// when scroll is "none", should click in-viewport element when scroll is "none", should not scroll nested container when scroll is "none", +/// should not scroll on hover when scroll is "none" (no scroll option); should scroll into view display:contents with position +/// (Playwright skips it on Chromium). +/// +public sealed class PageClickScrollTests : PlaywrightTest +{ + [Test] + public async Task Should_not_hit_scroll_bar() + { + await Browser.GotoAsync(Content(""" + +
+ + + + + + + +
+ """)); + await Screen.GetByText("Story", exact: false).TapAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(2) }); + } + + [Test] + public async Task Should_scroll_into_view_display_contents() + { + await Browser.GotoAsync(Content(""" +
filler
+
+ Example text, and button here: + +
+ """)); + await Screen.GetByText("click me", exact: false).TapAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(5) }); + Assert.That(await Browser.EvaluateAsync("window._clicked"), Is.True); + } + + [Test] + public async Task Should_scroll_into_view_display_contents_with_a_child() + { + await Browser.GotoAsync(Content(""" +
filler
+ Example text, and button here: + + """)); + await Screen.GetByText("click me", exact: false).TapAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(5) }); + Assert.That(await Browser.EvaluateAsync("window._clicked"), Is.True); + } + + [Test] + public async Task Should_scroll_into_view_span_element() + { + await Browser.GotoAsync(Content(""" +
+ foo + """)); + await Browser.Locator("#small").ScrollIntoViewAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.scrollY"), Is.GreaterThan(9000)); + } + + [Test] + public async Task Should_scroll_into_view_element_in_iframe() + { + await Browser.GotoAsync("/empty.html"); + await Browser.GotoAsync(Content($""" +
+ + """)); + await Browser.FrameLocator("iframe").GetByRole("button").TapAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(5) }); + } + + [Test] + public async Task Should_scroll_instantly_on_retry_when_scroll_behavior_is_smooth() + { + await Browser.GotoAsync(Content(""" + + +
+ +
+ + """)); + await Browser.Locator("button").TapAsync(); + Assert.That(await Browser.EvaluateAsync("window.clicked"), Is.True); + var scrolls = await Browser.EvaluateAsync("() => window['scrolls']"); + Assert.That(scrolls!.Length, Is.LessThanOrEqualTo(2)); + } +} diff --git a/tests/E2E.NUnit.Tests/ElementhandleScrollIntoView/ElementhandleScrollIntoViewTests.cs b/tests/E2E.NUnit.Tests/ElementhandleScrollIntoView/ElementhandleScrollIntoViewTests.cs new file mode 100644 index 000000000..20a95d6d7 --- /dev/null +++ b/tests/E2E.NUnit.Tests/ElementhandleScrollIntoView/ElementhandleScrollIntoViewTests.cs @@ -0,0 +1,131 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.ElementhandleScrollIntoView; + +/// +/// test.describe('elementhandle scroll into view') of upstream's apps/testbed/tests/elementhandle-scroll-into-view.e2e.ts +/// (Playwright's elementhandle-scroll-into-view.spec.ts). An element handle's scrollIntoViewIfNeeded is +/// ScrollIntoViewAsync on a locator. Not ported, as upstream: should throw for detached element (a handle to a removed element; a +/// locator re-resolves). +/// +public sealed class ElementhandleScrollIntoViewTests : PlaywrightTest +{ + /// + /// Starts scrolling the page's one div into view, checks it is still waiting a second later, runs on the + /// div in the page, and checks the scroll then finishes: Playwright's testWaiting. + /// + private async Task TestWaitingAsync(string after) + { + var done = false; + var promise = Task.Run(async () => + { + await Browser.Locator("div").ScrollIntoViewAsync(); + done = true; + }); + await Task.Delay(1000); + Assert.That(done, Is.False); + await Browser.EvaluateAsync($"() => {{ const div = document.querySelector('div'); ({after})(div); }}"); + await promise; + Assert.That(done, Is.True); + } + + [Test] + public async Task Should_work_smoke() + { + await Browser.GotoAsync("/offscreenbuttons.html"); + const string overflow = "(id) => document.getElementById(id).getBoundingClientRect().right - window.innerWidth"; + for (var i = 0; i < 11; ++i) + { + var before = await Browser.EvaluateAsync(overflow, $"btn{i}"); + Assert.That(before, Is.EqualTo(10 * i)); + await Browser.Locator($"#btn{i}").ScrollIntoViewAsync(); + var after = await Browser.EvaluateAsync(overflow, $"btn{i}"); + Assert.That(after <= 0, Is.True); + await Browser.EvaluateAsync("() => window.scrollTo(0, 0)"); + } + } + + [Test] + public async Task Should_wait_for_display_none_to_become_visible() + { + await Browser.GotoAsync(Content("
Hello
")); + await TestWaitingAsync("div => div.style.display = 'block'"); + } + + [Test] + public async Task Should_scroll_display_contents_into_view() + { + await Browser.GotoAsync(Content(""" + +
+
+
+
Hello
+
+
+
+ """)); + await Browser.Locator("#target").ScrollIntoViewAsync(); + var scrollTop = await Browser.EvaluateAsync("() => document.querySelector('#container').scrollTop"); + Assert.That(Math.Abs(scrollTop - 350), Is.LessThan(1)); + } + + [Test] + public async Task Should_work_for_visibility_hidden_element() + { + // Known difference (hardkoded/e2e-dotnet#1081): ScrollIntoViewAsync refuses a hidden node. + await ExpectedToFailAsync("is hidden", async () => + { + await Browser.GotoAsync(Content("
Hello
")); + await Browser.Locator("div").ScrollIntoViewAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(3) }); + }); + } + + [Test] + public async Task Should_work_for_zero_sized_element() + { + await Browser.GotoAsync(Content("
Hello
")); + await Browser.Locator("div").ScrollIntoViewAsync(); + } + + [Test] + public async Task Should_wait_for_nested_display_none_to_become_visible() + { + await Browser.GotoAsync(Content("
Hello
")); + await TestWaitingAsync("div => div.parentElement.style.display = 'block'"); + } + + [Test] + public async Task Should_wait_for_element_to_stop_moving() + { + await Browser.GotoAsync(Content(""" + +
moving
+ """)); + await TestWaitingAsync("div => div.classList.remove('animated')"); + } + + [Test] + public async Task Should_timeout_waiting_for_visible() + { + await Browser.GotoAsync(Content("
Hello
")); + var error = await Assert.CatchAsync(() => Browser.Locator("div").ScrollIntoViewAsync(new ActionOptions { Timeout = TimeSpan.FromSeconds(3) })); + Assert.That(error, Is.InstanceOf()); + } +} diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/button.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/button.html new file mode 100644 index 000000000..bfce25ee0 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/button.html @@ -0,0 +1,32 @@ + + + + Button test + + + + + + + \ No newline at end of file diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/checkbox.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/checkbox.html new file mode 100644 index 000000000..ca56762e2 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/checkbox.html @@ -0,0 +1,42 @@ + + + + Selection Test + + + + + + + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/mouse-helper.js b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/mouse-helper.js new file mode 100644 index 000000000..3c4d57033 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/mouse-helper.js @@ -0,0 +1,62 @@ +// This injects a box into the page that moves with the mouse; +// Useful for debugging +(function(){ + const box = document.createElement('div'); + box.classList.add('mouse-helper'); + const styleElement = document.createElement('style'); + styleElement.innerHTML = ` + .mouse-helper { + pointer-events: none; + position: absolute; + top: 0; + left: 0; + width: 20px; + height: 20px; + background: rgba(0,0,0,.4); + border: 1px solid white; + border-radius: 10px; + margin-left: -10px; + margin-top: -10px; + transition: background .2s, border-radius .2s, border-color .2s; + } + .mouse-helper.button-1 { + transition: none; + background: rgba(0,0,0,0.9); + } + .mouse-helper.button-2 { + transition: none; + border-color: rgba(0,0,255,0.9); + } + .mouse-helper.button-3 { + transition: none; + border-radius: 4px; + } + .mouse-helper.button-4 { + transition: none; + border-color: rgba(255,0,0,0.9); + } + .mouse-helper.button-5 { + transition: none; + border-color: rgba(0,255,0,0.9); + } + `; + document.head.appendChild(styleElement); + document.body.appendChild(box); + document.addEventListener('mousemove', event => { + box.style.left = event.pageX + 'px'; + box.style.top = event.pageY + 'px'; + updateButtons(event.buttons); + }, true); + document.addEventListener('mousedown', event => { + updateButtons(event.buttons); + box.classList.add('button-' + event.which); + }, true); + document.addEventListener('mouseup', event => { + updateButtons(event.buttons); + box.classList.remove('button-' + event.which); + }, true); + function updateButtons(buttons) { + for (let i = 0; i < 5; i++) + box.classList.toggle('button-' + i, buttons & (1 << i)); + } +})(); diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/rotatedButton.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/rotatedButton.html new file mode 100644 index 000000000..1bce66cf5 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/rotatedButton.html @@ -0,0 +1,21 @@ + + + + Rotated button test + + + + + + + + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/scrollable.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/scrollable.html new file mode 100644 index 000000000..885d3739d --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/input/scrollable.html @@ -0,0 +1,23 @@ + + + + Scrollable test + + + + + + \ No newline at end of file diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/wrappedlink.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/wrappedlink.html new file mode 100644 index 000000000..429b6e915 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/wrappedlink.html @@ -0,0 +1,32 @@ + +
+ 123321 +
+