diff --git a/COMPATIBILITY.md b/COMPATIBILITY.md index 7f77ebc47..1bdd8a6c4 100644 --- a/COMPATIBILITY.md +++ b/COMPATIBILITY.md @@ -162,6 +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`, and `selectors-text` (one test is such a known difference, hardkoded/e2e-dotnet#1062). - 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.Agent.Tests/PlaywrightRoutes.cs b/tests/E2E.Agent.Tests/PlaywrightRoutes.cs new file mode 100644 index 000000000..c27600fb8 --- /dev/null +++ b/tests/E2E.Agent.Tests/PlaywrightRoutes.cs @@ -0,0 +1,72 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Net; +using System.Text; + +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. +/// +internal static class PlaywrightRoutes +{ + private static readonly string Root = Path.Combine(AppContext.BaseDirectory, "Testbed", "playwright", "assets"); + + private static readonly Dictionary Types = new(StringComparer.Ordinal) + { + [".html"] = "text/html", + [".js"] = "text/javascript", + [".css"] = "text/css", + [".json"] = "application/json", + [".png"] = "image/png", + [".pdf"] = "application/pdf", + [".svg"] = "image/svg+xml", + [".txt"] = "text/plain", + }; + + /// Serves one request: a dynamic route, else an asset, else a 404. + public static async Task HandleAsync(HttpListenerContext context) + { + var response = context.Response; + var path = context.Request.Url!.AbsolutePath; + if (path == "/content") + { + var html = context.Request.QueryString["html"] ?? ""; + response.ContentType = "text/html; charset=utf-8"; + await WriteAsync(response, FromBase64Url(html)).ConfigureAwait(false); + return; + } + + var file = Path.GetFullPath(Path.Combine(Root, Uri.UnescapeDataString(path).TrimStart('/'))); + if (!file.StartsWith(Root + Path.DirectorySeparatorChar, StringComparison.Ordinal) || !File.Exists(file)) + { + response.StatusCode = 404; + response.ContentType = "text/plain"; + await WriteAsync(response, Encoding.UTF8.GetBytes("Not found")).ConfigureAwait(false); + return; + } + + response.ContentType = Types.GetValueOrDefault(Path.GetExtension(file), "application/octet-stream"); + response.AddHeader("Cache-Control", "no-store"); + await WriteAsync(response, await File.ReadAllBytesAsync(file).ConfigureAwait(false)).ConfigureAwait(false); + } + + private static byte[] FromBase64Url(string value) + { + var padded = value.Replace('-', '+').Replace('_', '/'); + padded = padded.PadRight(padded.Length + ((4 - (padded.Length % 4)) % 4), '='); + var bytes = new byte[padded.Length]; + return Convert.TryFromBase64String(padded, bytes, out var written) ? bytes[..written] : []; + } + + private static async Task WriteAsync(HttpListenerResponse response, byte[] bytes) + { + response.ContentLength64 = bytes.Length; + await response.OutputStream.WriteAsync(bytes).ConfigureAwait(false); + } +} diff --git a/tests/E2E.Agent.Tests/Testbed.cs b/tests/E2E.Agent.Tests/Testbed.cs index acdb010f7..d2a2336cf 100644 --- a/tests/E2E.Agent.Tests/Testbed.cs +++ b/tests/E2E.Agent.Tests/Testbed.cs @@ -1014,7 +1014,7 @@ await WriteHtmlAsync(response, "Dashboard", """ } else { - response.StatusCode = 404; + await PlaywrightRoutes.HandleAsync(context).ConfigureAwait(false); } response.Close(); diff --git a/tests/E2E.NUnit.Tests/Check/PageCheckTests.cs b/tests/E2E.NUnit.Tests/Check/PageCheckTests.cs new file mode 100644 index 000000000..0895e69e9 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Check/PageCheckTests.cs @@ -0,0 +1,160 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.Check; + +/// +/// test.describe('page check') of upstream's apps/testbed/tests/check.e2e.ts (Playwright's page-check.spec.ts). +/// Not ported, as upstream: should check the label with position (no position), trial run should not check and trial run +/// should not uncheck (no trial), should check the box using setChecked (no setChecked). +/// +public sealed class PageCheckTests : PlaywrightTest +{ + private static readonly string[] AriaRoles = ["checkbox", "menuitemcheckbox", "option", "radio", "switch", "menuitemradio", "treeitem"]; + + [Test] + public async Task Should_check_the_box() + { + await Browser.GotoAsync(Content("")); + await Browser.Locator("input").CheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.checkbox.checked"), Is.True); + } + + [Test] + public async Task Should_not_check_the_checked_box() + { + await Browser.GotoAsync(Content("")); + await Browser.Locator("input").CheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.checkbox.checked"), Is.True); + } + + [Test] + public async Task Should_uncheck_the_box() + { + await Browser.GotoAsync(Content("")); + await Browser.Locator("input").UncheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.checkbox.checked"), Is.False); + } + + [Test] + public async Task Should_not_uncheck_the_unchecked_box() + { + await Browser.GotoAsync(Content("")); + await Browser.Locator("input").UncheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.checkbox.checked"), Is.False); + } + + [Test] + public async Task Should_check_radio() + { + await Browser.GotoAsync(Content(""" + one + two + three + """)); + await Browser.Locator("#two").CheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.two.checked"), Is.True); + } + + [Test] + public async Task Should_check_radio_by_aria_role() + { + await Browser.GotoAsync(Content(""" + + + """)); + await Browser.Locator("div").CheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.checkbox.getAttribute('aria-checked')"), Is.EqualTo("true")); + } + + // Upstream: { fail: '`uncheck` refuses a `role=radio`, where Playwright clicks it and refuses only a native radio' }. + [Test] + public async Task Should_uncheck_radio_by_aria_role() + { + await ExpectedToFailAsync("uncheck", async () => + { + await Browser.GotoAsync(Content(""" + + + """)); + await Browser.Locator("div").UncheckAsync(); + Equal("false", await Browser.EvaluateAsync("() => window.checkbox.getAttribute('aria-checked')")); + }); + } + + [Test] + public async Task Should_check_the_box_by_aria_role() + { + foreach (var role in AriaRoles) + { + await Browser.GotoAsync(Content($$""" +
CHECKBOX
+ + """)); + await Browser.Locator("div").CheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => window.checkbox.getAttribute('aria-checked')"), Is.EqualTo("true")); + } + } + + // Upstream: { fail: '`uncheck` refuses a `role=radio`, where Playwright clicks it and refuses only a native radio' }. + [Test] + public async Task Should_uncheck_the_box_by_aria_role() + { + await ExpectedToFailAsync("uncheck", async () => + { + foreach (var role in AriaRoles) + { + await Browser.GotoAsync(Content($$""" +
CHECKBOX
+ + """)); + await Browser.Locator("div").UncheckAsync(); + Equal("false", await Browser.EvaluateAsync("() => window.checkbox.getAttribute('aria-checked')")); + } + }); + } + + [Test] + public async Task Should_throw_when_not_a_checkbox() + { + await Browser.GotoAsync(Content("
Check me
")); + var error = await Assert.CatchAsync(() => Browser.Locator("div").CheckAsync()); + Assert.That(error!.Message, Does.Contain("Not a checkbox or radio button")); + } + + [Test] + public async Task Should_throw_when_not_a_checkbox_2() + { + await Browser.GotoAsync(Content("
Check me
")); + var error = await Assert.CatchAsync(() => Browser.Locator("div").CheckAsync()); + Assert.That(error!.Message, Does.Contain("Not a checkbox or radio button")); + } + + [Test] + public async Task Should_check_the_box_inside_a_button() + { + await Browser.GotoAsync(Content("
")); + await Browser.Locator("input").CheckAsync(); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('input').checked"), Is.True); + Assert.That(await Browser.Locator("input").IsCheckedAsync(), Is.True); + } + + [Test] + public async Task Should_throw_when_trying_to_uncheck_radio_button() + { + await Browser.GotoAsync(Content("")); + var error = await Assert.CatchAsync(() => Browser.Locator("#radio").UncheckAsync()); + Assert.That(error!.Message, Does.Contain("uncheck cannot clear a radio button")); + } +} diff --git a/tests/E2E.NUnit.Tests/E2E.NUnit.Tests.csproj b/tests/E2E.NUnit.Tests/E2E.NUnit.Tests.csproj index ae5cb97ad..b71222b73 100644 --- a/tests/E2E.NUnit.Tests/E2E.NUnit.Tests.csproj +++ b/tests/E2E.NUnit.Tests/E2E.NUnit.Tests.csproj @@ -16,7 +16,9 @@ + + diff --git a/tests/E2E.NUnit.Tests/Helpers/Helpers.cs b/tests/E2E.NUnit.Tests/Helpers/Helpers.cs new file mode 100644 index 000000000..da38a9b5c --- /dev/null +++ b/tests/E2E.NUnit.Tests/Helpers/Helpers.cs @@ -0,0 +1,70 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Text; + +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). +/// +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; + + /// A path that serves as a page, for Playwright's page.setContent(html). + public static string Content(string html) => + "/content?html=" + Convert.ToBase64String(Encoding.UTF8.GetBytes(html)).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + + /// + /// An equality check for a body run by . It throws: an NUnit assertion inside a test body is + /// recorded as a failure of the test even when the body catches it. + /// + public static void Equal(T expected, T actual) + { + if (!EqualityComparer.Default.Equals(expected, actual)) + { + throw new InvalidOperationException($"Expected {expected}, but was {actual}."); + } + } + + /// + /// Runs the body of a test upstream marks { fail: 'reason' }, a known difference from Playwright: the test passes while + /// still fails with a message that contains , and fails once it passes, so the marker is removed. The port has no fail test option + /// (see COMPATIBILITY.md), and NUnit has no expected-failure outcome. + /// + public static async Task ExpectedToFailAsync(string failure, Func body) + { + try + { + await body().ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + Assert.That(ex.Message, Does.Contain(failure), "the body failed, but not with the known difference"); + return; + } + + Assert.Fail("UNEXPECTED_PASS: the test is marked as a known difference from Playwright, and it passed. Remove the marker."); + } +} + +/// +/// The base of the classes ported from Playwright's page tests: the playground as the base URL, the cache off, and the playwright +/// category (upstream's tags: ['playwright']). +/// +[Category("playwright")] +public abstract class PlaywrightTest : E2ETest +{ + private static readonly E2EConfig FixtureConfig = E2EConfig.Parse( + """{ "cache": { "mode": "off" } }""", + AppContext.BaseDirectory, + _ => null); + + protected override E2EConfig Config => FixtureConfig; + + protected override string? BaseUrl => E2E.Playground.Testbed.Url; +} diff --git a/tests/E2E.NUnit.Tests/LocatorConvenience/LocatorConvenienceTests.cs b/tests/E2E.NUnit.Tests/LocatorConvenience/LocatorConvenienceTests.cs new file mode 100644 index 000000000..65b74a060 --- /dev/null +++ b/tests/E2E.NUnit.Tests/LocatorConvenience/LocatorConvenienceTests.cs @@ -0,0 +1,130 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.LocatorConvenience; + +/// +/// test.describe('locator convenience') of upstream's apps/testbed/tests/locator-convenience.e2e.ts (Playwright's +/// locator-convenience.spec.ts). The page.<read>(selector) twins of each locator read are locator reads, and a +/// :text("...") selector is GetByText. Not ported, as upstream: should have a nice preview, innerHTML should work, +/// innerText should work, innerText should throw, innerText should produce log, isEditable should work, allInnerTexts should work, +/// should return page, the five description tests, and the two toString() tests (the port has none of those reads). +/// +public sealed class LocatorConvenienceTests : PlaywrightTest +{ + [Test] + public async Task GetAttribute_should_work() + { + await Browser.GotoAsync("/dom.html"); + var locator = Browser.Locator("#outer"); + Assert.That(await locator.GetAttributeAsync("name"), Is.EqualTo("value")); + Assert.That(await locator.GetAttributeAsync("foo"), Is.Null); + } + + // Upstream: { fail: '`inputValue()` on an element that is no input reads "" instead of throwing' }. + [Test] + public async Task InputValue_should_work() + { + await ExpectedToFailAsync("no input", async () => + { + await Browser.GotoAsync("/dom.html"); + + await Browser.Locator("#select").SelectOptionAsync("foo"); + Equal("foo", await Browser.Locator("#select").InputValueAsync()); + + await Browser.Locator("#textarea").FillAsync("text value"); + Equal("text value", await Browser.Locator("#textarea").InputValueAsync()); + + await Browser.Locator("#input").FillAsync("input value"); + Equal("input value", await Browser.Locator("#input").InputValueAsync()); + + await ExpectErrorAsync(() => Browser.Locator("#inner").InputValueAsync(), "no input"); + }); + } + + // Upstream: { fail: '`textContent()` normalizes whitespace, where Playwright keeps the raw text' }. + [Test] + public async Task TextContent_should_work() + { + await ExpectedToFailAsync("more text", async () => + { + await Browser.GotoAsync("/dom.html"); + var locator = Browser.Locator("#inner"); + Equal("Text,\nmore text", await locator.TextContentAsync()); + }); + } + + [Test] + public async Task IsEnabled_and_isDisabled_should_work() + { + await Browser.GotoAsync(Content(""" + + +
div
+ """)); + var div = Browser.Locator("div"); + Assert.That(await div.IsEnabledAsync(), Is.True); + Assert.That(await div.IsDisabledAsync(), Is.False); + var button1 = Screen.GetByText("button1", exact: false); + Assert.That(await button1.IsEnabledAsync(), Is.False); + Assert.That(await button1.IsDisabledAsync(), Is.True); + var button2 = Screen.GetByText("button2", exact: false); + Assert.That(await button2.IsEnabledAsync(), Is.True); + Assert.That(await button2.IsDisabledAsync(), Is.False); + } + + // Upstream: { fail: '`isChecked()` on an element that is no checkbox reads false instead of throwing' }. + [Test] + public async Task IsChecked_should_work() + { + await ExpectedToFailAsync("no checkbox", async () => + { + await Browser.GotoAsync(Content("
Not a checkbox
")); + var element = Browser.Locator("input"); + Equal(true, await element.IsCheckedAsync()); + await Browser.EvaluateAsync("() => { document.querySelector('input').checked = false; }"); + Equal(false, await element.IsCheckedAsync()); + await ExpectErrorAsync(() => Browser.Locator("div").IsCheckedAsync(), "no checkbox"); + }); + } + + [Test] + public async Task IsChecked_should_work_for_indeterminate_input() + { + await Browser.GotoAsync(Content("""""")); + await Browser.EvaluateAsync("() => { document.querySelector('input').indeterminate = true; }"); + + Assert.That(await Browser.Locator("input").IsCheckedAsync(), Is.True); + await Expect.That(Browser.Locator("input")).ToBeCheckedAsync(); + + await Browser.Locator("input").UncheckAsync(); + + Assert.That(await Browser.Locator("input").IsCheckedAsync(), Is.False); + await Expect.That(Browser.Locator("input")).Not.ToBeCheckedAsync(); + } + + [Test] + public async Task AllTextContents_should_work() + { + await Browser.GotoAsync(Content("
A
B
C
")); + Assert.That(await Browser.Locator("div").AllTextContentsAsync(), Is.EqualTo(new[] { "A", "B", "C" })); + } + + // Upstream's `catch((e) => e)` then `toEqual(expect.any(Error))`. Throws, so a body run by ExpectedToFailAsync can fail with it. + private static async Task ExpectErrorAsync(Func action, string what) + { + try + { + await action().ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + return; + } + + throw new InvalidOperationException($"Expected an error for an element that is {what}."); + } +} diff --git a/tests/E2E.NUnit.Tests/LocatorIsVisible/LocatorIsVisibleTests.cs b/tests/E2E.NUnit.Tests/LocatorIsVisible/LocatorIsVisibleTests.cs new file mode 100644 index 000000000..27d842023 --- /dev/null +++ b/tests/E2E.NUnit.Tests/LocatorIsVisible/LocatorIsVisibleTests.cs @@ -0,0 +1,187 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.LocatorIsVisible; + +/// +/// test.describe('locator is visible') of upstream's apps/testbed/tests/locator-is-visible.e2e.ts (Playwright's +/// locator-is-visible.spec.ts). The page.isVisible(selector) twins of each locator read are locator reads, and +/// locator(css, { hasText }) is a locator Filter(hasText). Not ported, as upstream: isVisible with invalid selector +/// should throw (Playwright selector engine names). +/// +public sealed class LocatorIsVisibleTests : PlaywrightTest +{ + // Known difference, hardkoded/e2e-dotnet#1075: an element with an empty box reads as visible. + [Test] + public async Task IsVisible_and_isHidden_should_work() + { + await ExpectedToFailAsync("Expected False, but was True", async () => + { + await Browser.GotoAsync(Content("
Hi
")); + + var div = Browser.Locator("div"); + Equal(true, await div.IsVisibleAsync()); + Equal(false, await div.IsHiddenAsync()); + + var span = Browser.Locator("span"); + Equal(false, await span.IsVisibleAsync()); + Equal(true, await span.IsHiddenAsync()); + + Equal(false, await Browser.Locator("no-such-element").IsVisibleAsync()); + Equal(true, await Browser.Locator("no-such-element").IsHiddenAsync()); + }); + } + + [Test] + public async Task IsVisible_should_be_true_for_opacity_0() + { + await Browser.GotoAsync(Content("""
Hi
""")); + await Expect.That(Browser.Locator("div")).ToBeVisibleAsync(); + } + + [Test] + public async Task IsVisible_should_be_true_for_element_outside_view() + { + await Browser.GotoAsync(Content("""
Hi
""")); + await Expect.That(Browser.Locator("div")).ToBeVisibleAsync(); + } + + [Test] + public async Task IsVisible_and_isHidden_should_work_with_details() + { + await Browser.GotoAsync(Content(""" +
+ click to open +
    +
  • hidden item 1
  • +
  • hidden item 2
  • +
  • hidden item 3
  • +
+ """)); + + await Expect.That(Browser.Locator("ul")).ToBeHiddenAsync(); + } + + [Test] + public async Task IsVisible_and_isHidden_should_work_with_nested_details() + { + await Browser.GotoAsync(Content(""" +
+ outer +
+ inner + +
+
+ """)); + + await Expect.That(Browser.Locator("summary").Filter(hasText: "outer")).ToBeVisibleAsync(); + await Expect.That(Browser.Locator("summary").Filter(hasText: "inner")).ToBeHiddenAsync(); + await Expect.That(Browser.Locator("button")).ToBeHiddenAsync(); + await Expect.That(Screen.GetByRole("button")).ToHaveCountAsync(0); + } + + [Test] + public async Task IsVisible_and_isHidden_should_work_with_unslotted_content() + { + await Browser.GotoAsync(Content(""" +
hidden 1
hidden 2
+
visible
+ + """)); + + await Expect.That(Browser.Locator("span").Filter(hasText: "hidden 1")).ToBeHiddenAsync(); + await Expect.That(Browser.Locator("span").Filter(hasText: "hidden 2")).ToBeHiddenAsync(); + await Expect.That(Browser.Locator("span").Filter(hasText: "visible")).ToBeVisibleAsync(); + } + + [Test] + public async Task IsVisible_and_isHidden_should_work_with_list_box_options() + { + await Browser.GotoAsync(Content(""" + + + + """)); + + foreach (var name in new[] { "visible 1", "visible 2", "visible 3" }) + { + await Expect.That(Screen.GetByRole("option", name)).ToBeVisibleAsync(); + } + + foreach (var text in new[] { "hidden 1", "hidden 2", "hidden 3" }) + { + await Expect.That(Browser.Locator("option").Filter(hasText: text)).ToBeHiddenAsync(); + } + } + + // Known difference, hardkoded/e2e-dotnet#1075: an element with an empty box reads as visible. + [Test] + public async Task IsVisible_inside_a_button() + { + await ExpectedToFailAsync("Expected False, but was True", async () => + { + await Browser.GotoAsync(Content("")); + var span = Browser.Locator("span"); + Equal(false, await span.IsVisibleAsync()); + Equal(true, await span.IsHiddenAsync()); + await Expect.That(span).Not.ToBeVisibleAsync(); + await Expect.That(span).ToBeHiddenAsync(); + await span.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Hidden }); + await Browser.Locator("button").WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Visible }); + }); + } + + // Known difference, hardkoded/e2e-dotnet#1075: an element with an empty box reads as visible. + [Test] + public async Task IsVisible_inside_a_role_button() + { + await ExpectedToFailAsync("Expected False, but was True", async () => + { + await Browser.GotoAsync(Content("
a button
")); + var span = Browser.Locator("span"); + Equal(false, await span.IsVisibleAsync()); + Equal(true, await span.IsHiddenAsync()); + await Expect.That(span).Not.ToBeVisibleAsync(); + await Expect.That(span).ToBeHiddenAsync(); + await span.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Hidden }); + await Browser.Locator("[role=button]").WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Visible }); + }); + } + + [Test] + public async Task IsVisible_during_navigation_should_not_throw() + { + for (var i = 0; i < 20; i++) + { + try + { + await Browser.GotoAsync(Content(""" + + """)); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + // Upstream ignores a goto that the navigation interrupts. + } + + Assert.That(await Browser.Locator("div").IsVisibleAsync(), Is.False); + } + } +} diff --git a/tests/E2E.NUnit.Tests/LocatorList/LocatorListTests.cs b/tests/E2E.NUnit.Tests/LocatorList/LocatorListTests.cs new file mode 100644 index 000000000..9d25b0898 --- /dev/null +++ b/tests/E2E.NUnit.Tests/LocatorList/LocatorListTests.cs @@ -0,0 +1,27 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.LocatorList; + +/// +/// test.describe('locator list') of upstream's apps/testbed/tests/locator-list.e2e.ts (Playwright's +/// locator-list.spec.ts). div >> p is the CSS div p. +/// +public sealed class LocatorListTests : PlaywrightTest +{ + [Test] + public async Task Locator_all_should_work() + { + await Browser.GotoAsync(Content("

A

B

C

")); + var texts = new List(); + foreach (var p in await Browser.Locator("div p").AllAsync()) + { + texts.Add(await p.TextContentAsync()); + } + + Assert.That(texts, Is.EqualTo(new[] { "A", "B", "C" })); + } +} diff --git a/tests/E2E.NUnit.Tests/LocatorMisc2/LocatorMisc2Tests.cs b/tests/E2E.NUnit.Tests/LocatorMisc2/LocatorMisc2Tests.cs new file mode 100644 index 000000000..9b816a509 --- /dev/null +++ b/tests/E2E.NUnit.Tests/LocatorMisc2/LocatorMisc2Tests.cs @@ -0,0 +1,159 @@ +// 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.LocatorMisc2; + +/// +/// test.describe('locator misc 2') of upstream's apps/testbed/tests/locator-misc-2.e2e.ts (Playwright's +/// locator-misc-2.spec.ts). scrollIntoViewIfNeeded is ScrollIntoViewAsync, and an element's own evaluate +/// is a page EvaluateAsync. Not ported, as upstream: should select textarea (no selectText), should type (deprecated; its +/// replacement, pressSequentially, is ported), should take screenshot (Playwright's own golden image), should combine visible with +/// other selectors (>> visible=true chains), should support filter(visible), should support visible(), and +/// Locator.locator() and FrameLocator.locator() should accept locator (no locator(locator)). +/// +public sealed class LocatorMisc2Tests : PlaywrightTest +{ + [Test] + public async Task Should_press_smoke() + { + await Browser.GotoAsync(Content("")); + await Browser.Locator("input").PressAsync("h"); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('input').value"), Is.EqualTo("h")); + } + + [Test] + public async Task Should_scroll_into_view() + { + 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_scroll_zero_sized_element_into_view() + { + await Browser.GotoAsync(Content(""" + +
+

SCROLL DOWN

+
+
+ + """)); + Assert.That(await Browser.Locator("#lazyload").BoundingBoxAsync(), Is.EqualTo(new BoundingBox(0, 2020, 1280, 0))); + await Browser.Locator("#lazyload").ScrollIntoViewAsync(); + await Expect.That(Browser.Locator("#lazyload")).ToHaveTextAsync("LAZY LOADED CONTENT"); + Assert.That(await Browser.Locator("#lazyload").BoundingBoxAsync(), Is.EqualTo(new BoundingBox(0, 720, 1280, 20))); + } + + [Test] + public async Task Should_pressSequentially() + { + await Browser.GotoAsync(Content("")); + await Browser.Locator("input").PressSequentiallyAsync("hello"); + Assert.That(await Browser.EvaluateAsync("() => document.querySelector('input').value"), Is.EqualTo("hello")); + } + + [Test] + public async Task Should_return_bounding_box() + { + await Browser.SetViewportAsync(500, 500); + await Browser.GotoAsync("/grid.html"); + var element = Browser.Locator(".box:nth-of-type(13)"); + var box = await element.BoundingBoxAsync(); + Assert.That(box, Is.EqualTo(new BoundingBox(100, 50, 50, 50))); + } + + [Test] + public async Task Should_waitFor() + { + await Browser.GotoAsync(Content("
")); + var locator = Browser.Locator("span"); + var promise = locator.WaitForAsync(); + await Browser.EvaluateAsync("() => { document.querySelector('div').innerHTML = 'target'; }"); + await promise; + await Expect.That(locator).ToHaveTextAsync("target"); + } + + [Test] + public async Task Should_waitFor_hidden() + { + await Browser.GotoAsync(Content("
target
")); + var locator = Browser.Locator("span"); + var promise = locator.WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Hidden }); + await Browser.EvaluateAsync("() => { document.querySelector('div').innerHTML = ''; }"); + await promise; + } + + // Upstream: { fail: '`browser.evaluate` and the in-page readers run in the main world, so a page that deletes `Map` breaks them; + // Playwright runs its own in a utility world' }. In the port, `EvaluateAsync` itself throws "Nullable object must have a value" once the + // page has replaced `Map`. + [Test] + public async Task Locator_count_should_work_with_deleted_Map_in_main_world() + { + await ExpectedToFailAsync("Nullable object must have a value", async () => + { + await Browser.GotoAsync("/empty.html"); + await Browser.EvaluateAsync("() => { window.Map = 1; }"); + await Browser.Locator("#searchResultTableDiv .x-grid3-row").CountAsync(); + await Expect.That(Browser.Locator("#searchResultTableDiv .x-grid3-row")).ToHaveCountAsync(0); + }); + } + + [Test] + public async Task Should_fill_programmatically_enabled_textarea() + { + await Browser.GotoAsync(Content(""" + +
+ +
+ + """)); + await Browser.Locator("button").ClickAsync(); + await Browser.Locator("#text").FillAsync("Hello"); + await Expect.That(Browser.Locator("#text")).ToHaveValueAsync("Hello"); + } + + [Test] + public async Task Press_should_throw_on_unknown_keys() + { + await Browser.GotoAsync(Content("")); + var locator = Screen.GetByRole("textbox"); + foreach (var key in new[] { "NotARealKey", "ё", "😊" }) + { + var error = await Assert.CatchAsync(() => locator.PressAsync(key)); + Assert.That(error!.Message, Does.Contain(key)); + } + } +} diff --git a/tests/E2E.NUnit.Tests/LocatorQuery/LocatorQueryTests.cs b/tests/E2E.NUnit.Tests/LocatorQuery/LocatorQueryTests.cs new file mode 100644 index 000000000..e9a4f2c46 --- /dev/null +++ b/tests/E2E.NUnit.Tests/LocatorQuery/LocatorQueryTests.cs @@ -0,0 +1,231 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Text.RegularExpressions; +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.LocatorQuery; + +/// +/// test.describe('locator query') of upstream's apps/testbed/tests/locator-query.e2e.ts (Playwright's +/// locator-query.spec.ts). locator(css, { hasText, has }) is a locator Filter, div >> p the CSS +/// div p, toHaveClass the browser matcher, and evaluate(e => e.outerHTML) a text read. JavaScript regex flags +/// u map to nothing in .NET, so those patterns carry only. Not ported, as upstream: +/// should throw on capture w/ nth(), should throw on due to strictness 2, should support locator.and, should support locator.locator +/// with and/or, should support locator.within, should keep the capture when removing a common frame prefix, and count() should not +/// throw during navigation. +/// +public sealed class LocatorQueryTests : PlaywrightTest +{ + private const string Sections = """ +
+

A

+

A

A

+

A

A

A

+
+ """; + + [Test] + public async Task Should_respect_first_and_last_smoke() + { + await Browser.GotoAsync(Content(Sections)); + Assert.That(await Browser.Locator("div p").CountAsync(), Is.EqualTo(6)); + Assert.That(await Browser.Locator("div").First().GetByText("A").CountAsync(), Is.EqualTo(1)); + Assert.That(await Browser.Locator("div").Last().GetByText("A").CountAsync(), Is.EqualTo(3)); + } + + [Test] + public async Task Should_respect_nth() + { + await Browser.GotoAsync(Content(Sections)); + Assert.That(await Browser.Locator("div p").Nth(0).CountAsync(), Is.EqualTo(1)); + Assert.That(await Browser.Locator("div").Nth(1).GetByText("A").CountAsync(), Is.EqualTo(2)); + Assert.That(await Browser.Locator("div").Nth(2).GetByText("A").CountAsync(), Is.EqualTo(3)); + } + + [Test] + public async Task Should_throw_on_due_to_strictness() + { + await Browser.GotoAsync(Content("
A
B
")); + Assert.That(await Assert.CatchAsync(() => Browser.Locator("div").IsVisibleAsync()), Is.Not.Null); + } + + [Test] + public async Task Should_filter_by_text() + { + await Browser.GotoAsync(Content("
Foobar
Bar
")); + await Expect.That(Browser.Locator("div").Filter(hasText: "Foo")).ToHaveTextAsync("Foobar"); + } + + [Test] + public async Task Should_filter_by_text_2() + { + await Browser.GotoAsync(Content("
foo hello world bar
")); + await Expect.That(Browser.Locator("div").Filter(hasText: "hello world")).ToHaveTextAsync("foo hello world bar"); + } + + [Test] + public async Task Should_filter_by_regex() + { + await Browser.GotoAsync(Content("
Foobar
Bar
")); + await Expect.That(Browser.Locator("div").Filter(hasText: new Regex("Foo.*"))).ToHaveTextAsync("Foobar"); + } + + [Test] + public async Task Should_filter_by_text_with_quotes() + { + await Browser.GotoAsync(Content("""
Hello "world"
Hello world
""")); + await Expect.That(Browser.Locator("div").Filter(hasText: "Hello \"world\"")).ToHaveTextAsync("Hello \"world\""); + } + + [Test] + public async Task Should_filter_by_regex_with_quotes() + { + await Browser.GotoAsync(Content("""
Hello "world"
Hello world
""")); + await Expect.That(Browser.Locator("div").Filter(hasText: new Regex("Hello \"world\""))).ToHaveTextAsync("Hello \"world\""); + } + + // Known difference, hardkoded/e2e-dotnet#1073: getByText finds no text inside a button. Ignored, not run behind ExpectedToFailAsync, + // because its Expect.Soft failures end the test, as upstream's expect.soft. + [Test] + [Ignore("hardkoded/e2e-dotnet#1073")] + public async Task Should_filter_by_regex_with_a_single_quote() + { + await Browser.GotoAsync(Content("")); + foreach (var pattern in new[] { "let's", "let's", "'s", "'s", "let['abc]s", "let's let's", "let's let's" }) + { + var regex = new Regex(pattern, RegexOptions.IgnoreCase); + await Expect.Soft(Browser.Locator("button").Filter(hasText: regex).GetByText("hello")).ToHaveTextAsync("hello"); + await Expect.Soft(Screen.GetByRole("button", regex).GetByText("hello")).ToHaveTextAsync("hello"); + } + + var backslash = new Regex(@"let\\'s", RegexOptions.IgnoreCase); + await Expect.Soft(Browser.Locator("button").Filter(hasText: backslash)).Not.ToBeVisibleAsync(); + await Expect.Soft(Screen.GetByRole("button", backslash)).Not.ToBeVisibleAsync(); + + await Browser.GotoAsync(Content(@"")); + var quote = new Regex("let's", RegexOptions.IgnoreCase); + await Expect.Soft(Browser.Locator("button").Filter(hasText: quote)).Not.ToBeVisibleAsync(); + await Expect.Soft(Screen.GetByRole("button", quote)).Not.ToBeVisibleAsync(); + foreach (var pattern in new[] { @"let\\'s", @"let\\'s", @"let\\'s let\\'s", @"let\\'s let\\'s" }) + { + var regex = new Regex(pattern, RegexOptions.IgnoreCase); + await Expect.Soft(Browser.Locator("button").Filter(hasText: regex).GetByText("hello")).ToHaveTextAsync("hello"); + await Expect.Soft(Screen.GetByRole("button", regex).GetByText("hello")).ToHaveTextAsync("hello"); + } + + await Browser.GotoAsync(Content("")); + await Expect.Soft(Browser.Locator("button").Filter(hasText: quote)).ToHaveTextAsync("let's hello"); + await Expect.Soft(Screen.GetByRole("button", quote)).ToHaveTextAsync("let's hello"); + } + + [Test] + public async Task Should_filter_by_regex_and_regexp_flags() + { + await Browser.GotoAsync(Content("""
Hello "world"
Hello world
""")); + await Expect.That(Browser.Locator("div").Filter(hasText: new Regex("hElLo \"world\"", RegexOptions.IgnoreCase))).ToHaveTextAsync("Hello \"world\""); + } + + [Test] + public async Task Should_filter_by_case_insensitive_regex_in_a_child() + { + await Browser.GotoAsync(Content("""
Title Text
""")); + await Expect.That(Browser.Locator("div").Filter(hasText: new Regex("^title text$", RegexOptions.IgnoreCase))).ToHaveTextAsync("Title Text"); + } + + [Test] + public async Task Should_filter_by_case_insensitive_regex_in_multiple_children() + { + await Browser.GotoAsync(Content("""
Title

Text

""")); + await Expect.That(Browser).ToHaveClassAsync(Browser.Locator("div").Filter(hasText: new Regex("^title text$", RegexOptions.IgnoreCase)), "test"); + } + + // Known difference, hardkoded/e2e-dotnet#1074: the text of a div joins two adjacent block elements with a space. + [Test] + public async Task Should_filter_by_regex_with_special_symbols() + { + await ExpectedToFailAsync("expect.toHaveClass failed", async () => + { + await Browser.GotoAsync(Content(@"
First/""and""

Second\

")); + var pattern = new Regex(@"^first/"".*""second\\$", RegexOptions.IgnoreCase | RegexOptions.Singleline); + await Expect.That(Browser).ToHaveClassAsync(Browser.Locator("div").Filter(hasText: pattern), "test"); + }); + } + + [Test] + public async Task Should_support_has_locator() + { + await Browser.GotoAsync(Content("
hello
world
")); + await Expect.That(Browser.Locator("div").Filter(has: Screen.GetByText("world", exact: false))).ToHaveCountAsync(1); + Assert.That(await Browser.Locator("div").Filter(has: Screen.GetByText("world", exact: false)).TextContentAsync(), Is.EqualTo("world")); + await Expect.That(Browser.Locator("div").Filter(has: Screen.GetByText("hello"))).ToHaveCountAsync(1); + Assert.That(await Browser.Locator("div").Filter(has: Screen.GetByText("hello")).TextContentAsync(), Is.EqualTo("hello")); + await Expect.That(Browser.Locator("div").Filter(has: Browser.Locator("span"))).ToHaveCountAsync(2); + await Expect.That(Browser.Locator("div").Filter(has: Browser.Locator("span").Filter(hasText: "wor"))).ToHaveCountAsync(1); + Assert.That(await Browser.Locator("div").Filter(has: Browser.Locator("span").Filter(hasText: "wor")).TextContentAsync(), Is.EqualTo("world")); + await Expect.That(Browser.Locator("div").Filter(has: Browser.Locator("span"), hasText: "wor")).ToHaveCountAsync(1); + } + + [Test] + public async Task Should_support_locator_filter() + { + await Browser.GotoAsync(Content("
hello
world
")); + await Expect.That(Browser.Locator("div").Filter(hasText: "hello")).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(hasText: "hello").Filter(hasText: "hello")).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(hasText: "hello").Filter(hasText: "world")).ToHaveCountAsync(0); + await Expect.That(Browser.Locator("section").Filter(hasText: "hello").Filter(hasText: "world")).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(hasText: "hello").GetByText("hello")).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(has: Browser.Locator("span").Filter(hasText: "world"))).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(has: Browser.Locator("span"))).ToHaveCountAsync(2); + await Expect.That(Browser.Locator("div").Filter(has: Browser.Locator("span"), hasText: "world")).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(hasNot: Browser.Locator("span").Filter(hasText: "world"))).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(hasNot: Browser.Locator("section"))).ToHaveCountAsync(2); + await Expect.That(Browser.Locator("div").Filter(hasNot: Browser.Locator("span"))).ToHaveCountAsync(0); + await Expect.That(Browser.Locator("div").Filter(hasNotText: "hello")).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").Filter(hasNotText: "foo")).ToHaveCountAsync(2); + } + + [Test] + public async Task Should_support_locator_or() + { + await Browser.GotoAsync(Content("
hello
world")); + await Expect.That(Browser.Locator("div").Or(Browser.Locator("span"))).ToHaveCountAsync(2); + await Expect.That(Browser.Locator("div").Or(Browser.Locator("span"))).ToHaveTextAsync(new TextMatch[] { "hello", "world" }); + await Expect.That(Browser.Locator("span").Or(Browser.Locator("article")).Or(Browser.Locator("div"))).ToHaveTextAsync(new TextMatch[] { "hello", "world" }); + await Expect.That(Browser.Locator("article").Or(Browser.Locator("something"))).ToHaveCountAsync(0); + await Expect.That(Browser.Locator("article").Or(Browser.Locator("div"))).ToHaveTextAsync("hello"); + await Expect.That(Browser.Locator("article").Or(Browser.Locator("span"))).ToHaveTextAsync("world"); + await Expect.That(Browser.Locator("div").Or(Browser.Locator("article"))).ToHaveTextAsync("hello"); + await Expect.That(Browser.Locator("span").Or(Browser.Locator("article"))).ToHaveTextAsync("world"); + } + + [Test] + public async Task Should_allow_some_but_not_all_nested_frameLocators() + { + await Browser.GotoAsync(Content("""hello""")); + var frame = Browser.FrameLocator("iframe"); + await Expect.That(frame.Locator("span").Or(frame.Locator("article"))).ToHaveTextAsync("world"); + await Expect.That(frame.Locator("article").Or(frame.Locator("span"))).ToHaveTextAsync("world"); + var error = await Assert.CatchAsync(() => Expect.That(frame.Locator("div").Or(Browser.FrameLocator("#iframe").Locator("span"))).ToHaveTextAsync("world")); + Assert.That(error, Is.Not.Null); + } + + [Test] + public async Task Should_enforce_same_frame_for_has_leftOf_rightOf_above_below_near() + { + await Browser.GotoAsync("/frames/two-frames.html"); + var child = Browser.FrameLocator("iframe[name=uno]"); + var error = await Assert.CatchAsync(() => Browser.Locator("div").Filter(has: child.Locator("span")).CountAsync()); + Assert.That(error, Is.Not.Null); + } + + [Test] + public async Task Alias_methods_coverage() + { + await Browser.GotoAsync(Content("
")); + await Expect.That(Browser.Locator("button")).ToHaveCountAsync(1); + await Expect.That(Browser.Locator("div").GetByRole("button")).ToHaveCountAsync(1); + await Expect.That(Screen.GetByRole("button")).ToHaveCountAsync(1); + } +} diff --git a/tests/E2E.NUnit.Tests/SelectorsGetBy/SelectorsGetByTests.cs b/tests/E2E.NUnit.Tests/SelectorsGetBy/SelectorsGetByTests.cs new file mode 100644 index 000000000..dcac13ae9 --- /dev/null +++ b/tests/E2E.NUnit.Tests/SelectorsGetBy/SelectorsGetByTests.cs @@ -0,0 +1,262 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Text.RegularExpressions; +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.SelectorsGetBy; + +/// +/// test.describe('selectors get by') of upstream's apps/testbed/tests/selectors-get-by.e2e.ts (Playwright's +/// selectors-get-by.spec.ts). A string query is exact by default, so Playwright's default substring match is +/// exact: false and its exact: true is the default; evaluate(e => e.outerHTML) reads become text and attribute +/// reads of the match. .NET regexes have no g, y, or v flag, so the tests that vary those flags use a plain +/// pattern. Not ported, as upstream: getByTestId with custom testId should work, getByTestId with comma-separated testIdAttributes +/// should match any, getByAltText should work, getByTitle should work, and the four getByRole with description tests. +/// +public sealed class SelectorsGetByTests : PlaywrightTest +{ + [Test] + public async Task GetByTestId_should_work() + { + await Browser.GotoAsync(Content("""
Hello world
""")); + await Expect.That(Screen.GetByTestId("Hello")).ToHaveTextAsync("Hello world"); + await Expect.That(Browser.Locator("div").GetByTestId("Hello")).ToHaveTextAsync("Hello world"); + } + + [Test] + public async Task GetByTestId_should_escape_id() + { + await Browser.GotoAsync(Content("""
Hello world
""")); + await Expect.That(Screen.GetByTestId("He\"llo")).ToHaveTextAsync("Hello world"); + } + + [Test] + public async Task GetByTestId_should_work_for_regex() + { + await Browser.GotoAsync(Content("""
Hello world
""")); + await Expect.That(Screen.GetByTestId(new Regex("He[l]*o"))).ToHaveTextAsync("Hello world"); + await Expect.That(Browser.Locator("div").GetByTestId("Hello")).ToHaveTextAsync("Hello world"); + } + + [Test] + public async Task GetByText_should_work() + { + await Browser.GotoAsync(Content("
yo
ya
\nye
")); + Assert.That(await Screen.GetByText("ye", exact: false).TextContentAsync(), Is.EqualTo("ye")); + Assert.That(await Screen.GetByText(new Regex("ye")).TextContentAsync(), Is.EqualTo("ye")); + Assert.That(await Screen.GetByText(new Regex("e")).TextContentAsync(), Is.EqualTo("ye")); + + await Browser.GotoAsync(Content("
ye
ye
")); + Assert.That(await Screen.GetByText("ye").First().BoundingBoxAsync(), Is.EqualTo(await Browser.Locator("div").First().BoundingBoxAsync())); + + await Browser.GotoAsync(Content("
Hello world
Hello
")); + Assert.That(await Screen.GetByText("Hello").TextContentAsync(), Is.EqualTo("Hello")); + } + + // Known difference, hardkoded/e2e-dotnet#1072: getByText("Name") returns the control of the label, not the label. + [Test] + public async Task GetByLabel_should_work() + { + await ExpectedToFailAsync("Expected target", async () => + { + await Browser.GotoAsync(Content("
")); + Equal("target", await Screen.GetByText("Name", exact: false).GetAttributeAsync("for")); + Equal("target", await Screen.GetByLabel("Name", exact: false).GetAttributeAsync("id")); + Equal("target", await Browser.Locator("div").GetByLabel("Name", exact: false).GetAttributeAsync("id")); + }); + } + + [Test] + public async Task GetByLabel_should_work_with_nested_elements() + { + await Browser.GotoAsync(Content("")); + + await Expect.That(Screen.GetByLabel("last name", exact: false)).ToHaveAttributeAsync("id", "target"); + await Expect.That(Screen.GetByLabel("st na", exact: false)).ToHaveAttributeAsync("id", "target"); + await Expect.That(Screen.GetByLabel("Name", exact: false)).ToHaveAttributeAsync("id", "target"); + await Expect.That(Screen.GetByLabel("Last Name")).ToHaveAttributeAsync("id", "target"); + await Expect.That(Screen.GetByLabel(new Regex(@"Last\s+name", RegexOptions.IgnoreCase))).ToHaveAttributeAsync("id", "target"); + + Assert.That(await Screen.GetByLabel("Last").CountAsync(), Is.EqualTo(0)); + Assert.That(await Screen.GetByLabel("last name").CountAsync(), Is.EqualTo(0)); + Assert.That(await Screen.GetByLabel("Name").CountAsync(), Is.EqualTo(0)); + Assert.That(await Screen.GetByLabel("what?", exact: false).CountAsync(), Is.EqualTo(0)); + Assert.That(await Screen.GetByLabel(new Regex("last name")).CountAsync(), Is.EqualTo(0)); + } + + [Test] + public async Task GetByLabel_should_work_with_multiply_labelled_input() + { + await Browser.GotoAsync(Content("")); + Assert.That(await Screen.GetByLabel("Name", exact: false).GetAttributeAsync("id"), Is.EqualTo("target")); + Assert.That(await Screen.GetByLabel("First or Last", exact: false).GetAttributeAsync("id"), Is.EqualTo("target")); + } + + [Test] + public async Task GetByLabel_should_work_with_ancestor_label_and_multiple_controls() + { + await Browser.GotoAsync(Content("")); + Assert.That(await Screen.GetByLabel("Name", exact: false).GetAttributeAsync("id"), Is.EqualTo("target")); + } + + [Test] + public async Task GetByLabel_should_work_with_ancestor_label_and_for() + { + await Browser.GotoAsync(Content(""" + + + """)); + Assert.That(await Screen.GetByLabel("Name", exact: false).GetAttributeAsync("id"), Is.EqualTo("target")); + } + + [Test] + public async Task GetByLabel_should_work_with_aria_labelledby() + { + await Browser.GotoAsync(Content("")); + Assert.That(await Screen.GetByLabel("Name", exact: false).TextContentAsync(), Is.EqualTo("Click me")); + } + + [Test] + public async Task GetByLabel_should_prioritize_aria_labelledby_over_native_label() + { + await Browser.GotoAsync(Content(""" + + + """)); + Assert.That(await Screen.GetByLabel("Name", exact: false).TextContentAsync(), Is.EqualTo("Click me")); + } + + [Test] + public async Task GetByLabel_should_work_with_aria_label() + { + await Browser.GotoAsync(Content("""""")); + Assert.That(await Screen.GetByLabel("Name", exact: false).GetAttributeAsync("id"), Is.EqualTo("target")); + } + + [Test] + public async Task GetByLabel_should_ignore_empty_aria_label() + { + await Browser.GotoAsync(Content("")); + Assert.That(await Screen.GetByLabel("Last Name", exact: false).GetAttributeAsync("id"), Is.EqualTo("target")); + } + + [Test] + public async Task GetByLabel_should_prioritize_aria_labelledby_over_aria_label() + { + await Browser.GotoAsync(Content("""""")); + Assert.That(await Screen.GetByLabel("Other", exact: false).GetAttributeAsync("id"), Is.EqualTo("target")); + } + + [Test] + public async Task GetByPlaceholder_should_work() + { + await Browser.GotoAsync(Content(""" +
+ + +
+ """)); + await Expect.That(Screen.GetByPlaceholder("hello", exact: false)).ToHaveCountAsync(2); + await Expect.That(Screen.GetByPlaceholder("Hello")).ToHaveCountAsync(1); + await Expect.That(Screen.GetByPlaceholder(new Regex("wor", RegexOptions.IgnoreCase))).ToHaveCountAsync(1); + + await Expect.That(Browser.Locator("div").GetByPlaceholder("hello", exact: false)).ToHaveCountAsync(2); + } + + // Known difference, hardkoded/e2e-dotnet#1072: getByText returns the control of a label, and matches an input's placeholder. Ignored, not + // run behind ExpectedToFailAsync, because its Expect.Soft failures end the test, as upstream's expect.soft. + [Test] + [Ignore("hardkoded/e2e-dotnet#1072")] + public async Task GetBy_escaping() + { + const string SetAttributes = "(value) => { const input = document.querySelector('input'); for (const name of ['placeholder', 'title', 'alt']) input.setAttribute(name, value); }"; + + await Browser.GotoAsync(Content("")); + await Browser.EvaluateAsync(SetAttributes, "hello my\nwo\"rld"); + await Expect.Soft(Screen.GetByText("hello my\nwo\"rld", exact: false)).ToHaveAttributeAsync("id", "label"); + await Expect.Soft(Screen.GetByText("hello my wo\"rld", exact: false)).ToHaveAttributeAsync("id", "label"); + await Expect.Soft(Screen.GetByLabel("hello my\nwo\"rld", exact: false)).ToHaveAttributeAsync("id", "control"); + await Expect.Soft(Screen.GetByPlaceholder("hello my\nwo\"rld", exact: false)).ToHaveAttributeAsync("id", "control"); + + await Browser.GotoAsync(Content("")); + await Browser.EvaluateAsync(SetAttributes, "hello my\nworld"); + await Expect.Soft(Screen.GetByText("hello my\nworld", exact: false)).ToHaveAttributeAsync("id", "label"); + await Expect.Soft(Screen.GetByText("hello my world", exact: false)).ToHaveAttributeAsync("id", "label"); + await Expect.Soft(Screen.GetByLabel("hello my\nworld", exact: false)).ToHaveAttributeAsync("id", "control"); + await Expect.Soft(Screen.GetByPlaceholder("hello my\nworld", exact: false)).ToHaveAttributeAsync("id", "control"); + + await Browser.GotoAsync(Content("")); + await Browser.EvaluateAsync(SetAttributes, "foo >> bar"); + Assert.That(await Screen.GetByText("foo >> bar", exact: false).TextContentAsync(), Is.EqualTo("foo >> bar")); + await Expect.Soft(Browser.Locator("label")).ToHaveTextAsync("foo >> bar"); + await Expect.Soft(Screen.GetByText("foo >> bar", exact: false)).ToHaveTextAsync("foo >> bar"); + Assert.That(await Screen.GetByText(new Regex("foo >> bar")).TextContentAsync(), Is.EqualTo("foo >> bar")); + await Expect.Soft(Screen.GetByLabel("foo >> bar", exact: false)).ToHaveAttributeAsync("id", "target"); + await Expect.Soft(Screen.GetByLabel(new Regex("foo >> bar"))).ToHaveAttributeAsync("id", "target"); + await Expect.Soft(Screen.GetByPlaceholder("foo >> bar", exact: false)).ToHaveAttributeAsync("id", "target"); + await Expect.Soft(Screen.GetByPlaceholder(new Regex("foo >> bar"))).ToHaveAttributeAsync("id", "target"); + } + + [Test] + public async Task GetByRole_escaping() + { + await Browser.GotoAsync(Content(""" + issues 123 + he llo 56 + + """)); + Assert.That(await Screen.GetByRole("button").AllTextContentsAsync(), Is.EqualTo(new[] { "Click me" })); + Assert.That(await Screen.GetByRole("link").AllTextContentsAsync(), Is.EqualTo(new[] { "issues 123", "he llo 56" })); + + Assert.That(await Screen.GetByRole("link", "issues 123", exact: false).AllTextContentsAsync(), Is.EqualTo(new[] { "issues 123" })); + Assert.That(await Screen.GetByRole("link", "sues", exact: false).AllTextContentsAsync(), Is.EqualTo(new[] { "issues 123" })); + Assert.That(await Screen.GetByRole("link", " he \n llo ", exact: false).AllTextContentsAsync(), Is.EqualTo(new[] { "he llo 56" })); + Assert.That(await Screen.GetByRole("button", "issues", exact: false).AllTextContentsAsync(), Is.Empty); + + Assert.That(await Screen.GetByRole("link", "sues").AllTextContentsAsync(), Is.Empty); + Assert.That(await Screen.GetByRole("link", " he \n llo 56 ").AllTextContentsAsync(), Is.EqualTo(new[] { "he llo 56" })); + + Assert.That(await Screen.GetByRole("button", "Click me").AllTextContentsAsync(), Is.EqualTo(new[] { "Click me" })); + Assert.That(await Screen.GetByRole("button", @"Click \me").AllTextContentsAsync(), Is.Empty); + Assert.That(await Screen.GetByRole("button", @"Click \\me").AllTextContentsAsync(), Is.Empty); + } + + [Test] + public async Task GetByRole_should_accept_regexp_with_v_flag() + { + await Browser.GotoAsync(Content("")); + await Expect.That(Screen.GetByRole("button", new Regex("Click me"))).ToHaveCountAsync(1); + await Expect.That(Screen.GetByRole("button", new Regex("click me", RegexOptions.IgnoreCase))).ToHaveCountAsync(1); + await Expect.That(Screen.GetByRole("button", new Regex("Missing"))).ToHaveCountAsync(0, TimeSpan.FromSeconds(1)); + } + + // Known difference, hardkoded/e2e-dotnet#1072: getByText matches an input's placeholder, so it counts 12, not 9. + [Test] + public async Task Locators_should_not_be_affected_by_global_and_sticky_regex_flags() + { + await ExpectedToFailAsync("expected count 9; observed count 12", async () => + { + await Browser.GotoAsync(Content(""" +
foo
foo
foo
+ + + + """)); + var foo = new Regex("foo"); + await Expect.That(Screen.GetByText(foo)).ToHaveCountAsync(9); + await Expect.That(Screen.GetByText(foo)).ToHaveCountAsync(9); + await Expect.That(Screen.GetByText(foo)).ToHaveCountAsync(9); + await Expect.That(Screen.GetByLabel(foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByLabel(foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByLabel(foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByRole("button", foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByRole("button", foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByRole("button", foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByPlaceholder(foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByPlaceholder(foo)).ToHaveCountAsync(3); + await Expect.That(Screen.GetByPlaceholder(foo)).ToHaveCountAsync(3); + }); + } +} diff --git a/tests/E2E.NUnit.Tests/SelectorsRole/SelectorsRoleTests.cs b/tests/E2E.NUnit.Tests/SelectorsRole/SelectorsRoleTests.cs new file mode 100644 index 000000000..fb93f0622 --- /dev/null +++ b/tests/E2E.NUnit.Tests/SelectorsRole/SelectorsRoleTests.cs @@ -0,0 +1,380 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Text.RegularExpressions; +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.SelectorsRole; + +/// +/// test.describe('selectors role') of upstream's apps/testbed/tests/selectors-role.e2e.ts (Playwright's +/// selectors-role.spec.ts). A role= selector is GetByRole with the same options, a string name is exact by +/// default so Playwright's default substring match is exact: false, and an evaluateAll(e => e.outerHTML) read is +/// the matches' texts or attributes. The include-hidden, checked="mixed", and pressed="mixed" cases are left +/// out, as upstream: GetByRole has no includeHidden or mixed state. Not ported, as upstream: errors (Playwright +/// role= selector parse errors). +/// +public sealed class SelectorsRoleTests : PlaywrightTest +{ + /// The texts as one string, for , which a body run by ExpectedToFailAsync needs. + private static string Joined(IEnumerable texts) => string.Join('|', texts); + + /// The aria-label of every current match, in document order. + private static async Task> LabelsAsync(Locator locator) + { + var labels = new List(); + foreach (var match in await locator.AllAsync()) + { + labels.Add(await match.GetAttributeAsync("aria-label")); + } + + return labels; + } + + [Test] + public async Task Should_detect_roles() + { + await Browser.GotoAsync(Content(""" + + + +

Heading

+
Hello
+
I am a dialog
+ """)); + Assert.That(await Screen.GetByRole("button").AllTextContentsAsync(), Is.EqualTo(new[] { "Hello" })); + await Expect.That(Screen.GetByRole("listbox")).ToHaveCountAsync(1); + await Expect.That(Screen.GetByRole("listbox")).ToHaveAttributeAsync("multiple", ""); + await Expect.That(Screen.GetByRole("combobox")).ToHaveCountAsync(1); + Assert.That(await Screen.GetByRole("combobox").GetAttributeAsync("multiple"), Is.Null); + Assert.That(await Screen.GetByRole("heading").AllTextContentsAsync(), Is.EqualTo(new[] { "Heading" })); + Assert.That(await Screen.GetByRole("group").AllTextContentsAsync(), Is.EqualTo(new[] { "Hello" })); + Assert.That(await Screen.GetByRole("dialog").AllTextContentsAsync(), Is.EqualTo(new[] { "I am a dialog" })); + Assert.That(await Screen.GetByRole("menuitem").AllTextContentsAsync(), Is.Empty); + } + + [Test] + public async Task Should_support_selected() + { + await Browser.GotoAsync(Content(""" + +
+
Hi
+
Hello
+
+ """)); + Assert.That(await Screen.GetByRole("option", new RoleOptions { Selected = true }).AllTextContentsAsync(), Is.EqualTo(new[] { "Hello", "Hi" })); + Assert.That(await Screen.GetByRole("option", new RoleOptions { Selected = false }).AllTextContentsAsync(), Is.EqualTo(new[] { "Hi", "Hello" })); + } + + // Known difference, hardkoded/e2e-dotnet#1076: an indeterminate checkbox matches checked: false. + [Test] + public async Task Should_support_checked() + { + await ExpectedToFailAsync("expected count 3; observed count 4", async () => + { + await Browser.GotoAsync(Content(""" + + + +
Hi
+
Hello
+
Unknown
+ """)); + await Browser.EvaluateAsync("() => { document.querySelector('[indeterminate]').indeterminate = true; }"); + + var checkedBoxes = Screen.GetByRole("checkbox", new RoleOptions { Checked = true }); + await Expect.That(checkedBoxes).ToHaveCountAsync(2); + await Expect.That(checkedBoxes.First()).ToHaveAttributeAsync("checked", ""); + await Expect.That(checkedBoxes.Nth(1)).ToHaveTextAsync("Hi"); + + var unchecked_ = Screen.GetByRole("checkbox", new RoleOptions { Checked = false }); + await Expect.That(unchecked_).ToHaveCountAsync(3); + Equal(null, await unchecked_.First().GetAttributeAsync("checked")); + Equal(null, await unchecked_.First().GetAttributeAsync("indeterminate")); + Equal("|Hello|Unknown", Joined(await unchecked_.AllTextContentsAsync())); + + await Expect.That(Screen.GetByRole("checkbox")).ToHaveCountAsync(6); + }); + } + + // Known difference, hardkoded/e2e-dotnet#1076: a button with aria-pressed="mixed" matches pressed: false. + [Test] + public async Task Should_support_pressed() + { + await ExpectedToFailAsync("Expected Hi|Bye", async () => + { + await Browser.GotoAsync(Content(""" + + + + + """)); + Equal("Hello", Joined(await Screen.GetByRole("button", new RoleOptions { Pressed = true }).AllTextContentsAsync())); + Equal("Hi|Bye", Joined(await Screen.GetByRole("button", new RoleOptions { Pressed = false }).AllTextContentsAsync())); + Equal("Hi|Hello|Bye|Mixed", Joined(await Screen.GetByRole("button").AllTextContentsAsync())); + }); + } + + // Known difference, hardkoded/e2e-dotnet#1076: a treeitem with no aria-expanded matches expanded: false. + [Test] + public async Task Should_support_expanded() + { + await ExpectedToFailAsync("Expected Bye", async () => + { + await Browser.GotoAsync(Content(""" +
Hi
+
Hello
+ + """)); + + Equal("Hi|Hello|Bye", Joined(await Screen.GetByRole("treeitem").AllTextContentsAsync())); + Equal("Hello", Joined(await Screen.GetByRole("treeitem", new RoleOptions { Expanded = true }).AllTextContentsAsync())); + Equal("Bye", Joined(await Screen.GetByRole("treeitem", new RoleOptions { Expanded = false }).AllTextContentsAsync())); + Equal("Hi", Joined(await Browser.Locator("[role=treeitem]:not([aria-expanded])").AllTextContentsAsync())); + }); + } + + [Test] + public async Task Should_support_disabled() + { + await Browser.GotoAsync(Content(""" + + + + +
+ +
+ + """)); + Assert.That(await Screen.GetByRole("button", new RoleOptions { Disabled = true }).AllTextContentsAsync(), Is.EqualTo(new[] { "Bye", "Hello", "Yay" })); + Assert.That(await Screen.GetByRole("button", new RoleOptions { Disabled = false }).AllTextContentsAsync(), Is.EqualTo(new[] { "Hi", "Oh" })); + Assert.That(await Screen.GetByRole("option", new RoleOptions { Disabled = true }).AllTextContentsAsync(), Is.EqualTo(new[] { "one", "three" })); + Assert.That(await Screen.GetByRole("option", new RoleOptions { Disabled = false }).AllTextContentsAsync(), Is.EqualTo(new[] { "two" })); + Assert.That(await Screen.GetByRole("option").AllTextContentsAsync(), Is.EqualTo(new[] { "one", "two", "three" })); + } + + [Test] + public async Task Should_inherit_disabled_from_the_ancestor() + { + await Browser.GotoAsync(Content(""" + + + + """)); + await Expect.That(Browser.Locator("button")).ToBeDisabledAsync(); + + await Browser.GotoAsync(Content(""" + +

Heading

+
+ """)); + await Expect.That(Browser.Locator("h1")).Not.ToBeDisabledAsync(); + } + + [Test] + public async Task Should_support_disabled_fieldset() + { + await Browser.GotoAsync(Content(""" +
+ + + + + +
+ +
+ +
+ +
+
+
+ +
+
+ + + + + + +
+ +
+
+ +
+
+ """)); + + await Expect.Soft(Screen.GetByTestId("inside-legend-element")).ToBeEnabledAsync(); + await Expect.Soft(Screen.GetByTestId("nested-inside-legend-element")).ToBeEnabledAsync(); + await Expect.Soft(Screen.GetByTestId("first-legend-element")).ToBeEnabledAsync(); + await Expect.Soft(Screen.GetByTestId("second-legend-element")).ToBeDisabledAsync(); + await Expect.Soft(Screen.GetByTestId("deep-button")).ToBeDisabledAsync(); + } + + // Known difference, hardkoded/e2e-dotnet#1076: a div with role="heading" and aria-level="5" does not match level: 5. + [Test] + public async Task Should_support_level() + { + await ExpectedToFailAsync("Expected Bye", async () => + { + await Browser.GotoAsync(Content(""" +

Hello

+

Hi

+
Bye
+ """)); + Equal("Hello", Joined(await Screen.GetByRole("heading", new RoleOptions { Level = 1 }).AllTextContentsAsync())); + Equal("Hi", Joined(await Screen.GetByRole("heading", new RoleOptions { Level = 3 }).AllTextContentsAsync())); + Equal("Bye", Joined(await Screen.GetByRole("heading", new RoleOptions { Level = 5 }).AllTextContentsAsync())); + }); + } + + [Test] + public async Task Should_filter_hidden_unless_explicitly_asked_for() + { + await Browser.GotoAsync(Content(""" + + + + + +
+ +
+
+ +
+ +
+ + + +
+ Open form + +
+ + + + + """)); + Assert.That(await Screen.GetByRole("button").AllTextContentsAsync(), Is.EqualTo(new[] { "Hi", "Nay", "Still here", "Shadow1" })); + await Expect.That(Screen.GetByRole("textbox")).ToHaveCountAsync(1); + await Expect.That(Screen.GetByRole("textbox")).ToHaveAttributeAsync("name", "one"); + Assert.That(await Screen.GetByRole("option").AllTextContentsAsync(), Is.EqualTo(new[] { "One", "Three" })); + } + + [Test] + public async Task Should_support_name() + { + await Browser.GotoAsync(Content(""" +
+
+ + + + """)); + Assert.That(await LabelsAsync(Screen.GetByRole("button", "Hello")), Is.EqualTo(new[] { " Hello " })); + Assert.That(await LabelsAsync(Screen.GetByRole("button", " \n Hello ")), Is.EqualTo(new[] { " Hello " })); + Assert.That(await LabelsAsync(Screen.GetByRole("button", "Hello", exact: false)), Is.EqualTo(new[] { " Hello " })); + Assert.That(await LabelsAsync(Screen.GetByRole("button", "all", exact: false)), Is.EqualTo(new[] { "Hallo" })); + Assert.That(await LabelsAsync(Screen.GetByRole("button", new Regex("^H[ae]llo$"))), Is.EqualTo(new[] { " Hello ", "Hallo" })); + Assert.That(await LabelsAsync(Screen.GetByRole("button", new Regex("h.*o", RegexOptions.IgnoreCase))), Is.EqualTo(new[] { " Hello ", "Hallo" })); + } + + [Test] + public async Task Should_support_option_name_with_html_whitespace() + { + await Browser.GotoAsync(Content(""" + + """)); + await Expect.That(Screen.GetByRole("option", "HTML", exact: false)).ToHaveCountAsync(1); + } + + [Test] + public async Task Hidden_with_shadow_dom_slots() + { + await Browser.GotoAsync(Content(""" +
+ +
+
+ +
+
+ +
+
+ +
+ + """)); + Assert.That(await Screen.GetByRole("button").AllTextContentsAsync(), Is.EqualTo(new[] { "visible1", "visible2" })); + } + + [Test] + public async Task Should_support_output_accessible_name() + { + await Browser.GotoAsync(Content("")); + await Expect.That(Screen.GetByRole("status", "Output1", exact: false)).ToBeVisibleAsync(); + } + + [Test] + public async Task Should_not_match_scope_by_default() + { + await Browser.GotoAsync(Content(""" +
    +
  • + Parent list +
      +
    • child 1
    • +
    • child 2
    • +
    +
  • +
+ """)); + var children = Screen.GetByRole("listitem", "Parent list", exact: false).GetByRole("listitem"); + await Expect.That(children).ToHaveCountAsync(2); + await Expect.That(children).ToHaveTextAsync(new TextMatch[] { "child 1", "child 2" }); + } +} diff --git a/tests/E2E.NUnit.Tests/SelectorsText/SelectorsTextTests.cs b/tests/E2E.NUnit.Tests/SelectorsText/SelectorsTextTests.cs new file mode 100644 index 000000000..c14218e4a --- /dev/null +++ b/tests/E2E.NUnit.Tests/SelectorsText/SelectorsTextTests.cs @@ -0,0 +1,62 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Text.RegularExpressions; +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.SelectorsText; + +/// +/// test.describe('selectors text') of upstream's apps/testbed/tests/selectors-text.e2e.ts (Playwright's +/// selectors-text.spec.ts). locator(css, { hasText }) is a locator Filter(hasText). The 20 other tests are not +/// ported, as upstream: they need Playwright's text= selectors, :text pseudo-classes, >> chains, or element handles. +/// +public sealed partial class SelectorsTextTests : PlaywrightTest +{ + [Test] + public async Task Text_is_should_ignore_comments() + { + await Browser.GotoAsync(Content(""" +
hello + + world
+ """)); + Assert.That(await Browser.Locator("div").Filter(hasText: "hello world").GetAttributeAsync("id"), Is.EqualTo("me")); + Assert.That(await Screen.GetByText("hello world").GetAttributeAsync("id"), Is.EqualTo("me")); + } + + // Known difference, hardkoded/e2e-dotnet#1062: the first getByText('hello') match has no `id`. + [Test] + public async Task HasText_and_internal_text_should_match_full_node_text_in_strict_mode() + { + await ExpectedToFailAsync("Expected span1", async () => + { + await Browser.GotoAsync(Content(""" +
helloworld
+
hello
+ """)); + await Expect.That(Screen.GetByText("helloworld")).ToHaveAttributeAsync("id", "div1"); + await Expect.That(Screen.GetByText("hello")).ToHaveAttributeAsync("id", "div2"); + await Expect.That(Browser.Locator("div").Filter(hasText: HelloWorld())).ToHaveAttributeAsync("id", "div1"); + await Expect.That(Browser.Locator("div").Filter(hasText: Hello())).ToHaveAttributeAsync("id", "div2"); + + await Browser.GotoAsync(Content(""" +
helloworld
+
hello
+ """)); + await Expect.That(Screen.GetByText("helloworld")).ToHaveAttributeAsync("id", "div1"); + Equal("span1", await Screen.GetByText("hello").Nth(0).GetAttributeAsync("id")); + Equal("span2", await Screen.GetByText("hello").Nth(1).GetAttributeAsync("id")); + await Expect.That(Screen.GetByText("hello")).ToHaveCountAsync(2); + await Expect.That(Browser.Locator("div").Filter(hasText: HelloWorld())).ToHaveAttributeAsync("id", "div1"); + await Expect.That(Browser.Locator("div").Filter(hasText: Hello())).ToHaveAttributeAsync("id", "div2"); + }); + } + + [GeneratedRegex(@"^helloworld$")] + private static partial Regex HelloWorld(); + + [GeneratedRegex(@"^hello$")] + private static partial Regex Hello(); +} diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/LICENSE b/tests/E2E.NUnit.Tests/Testbed/playwright/LICENSE new file mode 100644 index 000000000..4ace03ddb --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/LICENSE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Portions Copyright (c) Microsoft Corporation. + Portions Copyright 2017 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/NOTICE b/tests/E2E.NUnit.Tests/Testbed/playwright/NOTICE new file mode 100644 index 000000000..814ec1696 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/NOTICE @@ -0,0 +1,5 @@ +Playwright +Copyright (c) Microsoft Corporation + +This software contains code derived from the Puppeteer project (https://github.com/puppeteer/puppeteer), +available under the Apache 2.0 license (https://github.com/puppeteer/puppeteer/blob/master/LICENSE). diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/dom.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/dom.html new file mode 100644 index 000000000..25572912e --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/dom.html @@ -0,0 +1,5 @@ +
Text, +more text
+ + + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/empty.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/empty.html new file mode 100644 index 000000000..e69de29bb diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/frame.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/frame.html new file mode 100644 index 000000000..3d3b94343 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/frame.html @@ -0,0 +1,19 @@ + + + + + +
Hi, I'm frame
diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/script.js b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/script.js new file mode 100644 index 000000000..be22256d1 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/script.js @@ -0,0 +1 @@ +console.log('Cheers!'); diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/style.css b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/style.css new file mode 100644 index 000000000..5b5436e87 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/style.css @@ -0,0 +1,3 @@ +div { + color: blue; +} diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/two-frames.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/two-frames.html new file mode 100644 index 000000000..00324f084 --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/frames/two-frames.html @@ -0,0 +1,16 @@ + + + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/grid.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/grid.html new file mode 100644 index 000000000..8345f429b --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/grid.html @@ -0,0 +1,66 @@ + + + diff --git a/tests/E2E.NUnit.Tests/Testbed/playwright/assets/offscreenbuttons.html b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/offscreenbuttons.html new file mode 100644 index 000000000..b7f381dec --- /dev/null +++ b/tests/E2E.NUnit.Tests/Testbed/playwright/assets/offscreenbuttons.html @@ -0,0 +1,55 @@ + +
+ + + + + + + + + + + +
+ diff --git a/tests/E2E.NUnit.Tests/WaitForUrl/PageWaitForUrlTests.cs b/tests/E2E.NUnit.Tests/WaitForUrl/PageWaitForUrlTests.cs new file mode 100644 index 000000000..29bb79f45 --- /dev/null +++ b/tests/E2E.NUnit.Tests/WaitForUrl/PageWaitForUrlTests.cs @@ -0,0 +1,127 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Text.RegularExpressions; +using static E2E.NUnit.Tests.Helpers; + +namespace E2E.NUnit.Tests.WaitForUrl; + +/// +/// test.describe('page wait for url') of upstream's apps/testbed/tests/wait-for-url.e2e.ts (Playwright's +/// page-wait-for-url.spec.ts). Not ported, as upstream: should work with both domcontentloaded and load, should work with +/// commit, should work with commit and about:blank (no waitUntil), should work on frame (no frame.waitForURL). +/// +public sealed partial class PageWaitForUrlTests : PlaywrightTest +{ + private static string EmptyPage => $"{PREFIX}/empty.html"; + + [Test] + public async Task Should_work() + { + await Browser.GotoAsync(EmptyPage); + await Browser.EvaluateAsync("(url) => { window.location.href = url; }", $"{PREFIX}/grid.html"); + await Browser.WaitForURLAsync("/grid.html"); + } + + [Test] + public async Task Should_respect_timeout() + { + // e2e opens no page before the first goto, where Playwright starts on about:blank. + await Browser.GotoAsync(EmptyPage); + var promise = Assert.CatchAsync(() => Browser.WaitForURLAsync("/frame.html", TimeSpan.FromMilliseconds(2500))); + await Browser.GotoAsync(EmptyPage); + var error = await promise; + Assert.That(error!.Message, Does.Contain("frame.html")); + } + + [Test] + public async Task Should_work_with_clicking_on_anchor_links() + { + await Browser.GotoAsync(EmptyPage); + await Browser.GotoAsync(Content("foobar")); + await Browser.Locator("a").TapAsync(); + await Browser.WaitForURLAsync(FoobarSuffix()); + } + + [Test] + public async Task Should_work_with_history_pushState() + { + await Browser.GotoAsync(EmptyPage); + await Browser.GotoAsync(Content(""" + SPA + + """)); + await Browser.Locator("a").TapAsync(); + await Browser.WaitForURLAsync("/wow.html"); + Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/wow.html")); + } + + [Test] + public async Task Should_work_with_history_replaceState() + { + await Browser.GotoAsync(EmptyPage); + await Browser.GotoAsync(Content(""" + SPA + + """)); + await Browser.Locator("a").TapAsync(); + await Browser.WaitForURLAsync("/replaced.html"); + Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/replaced.html")); + } + + [Test] + public async Task Should_work_with_DOM_history_back_history_forward() + { + await Browser.GotoAsync(EmptyPage); + await Browser.GotoAsync(Content(""" + back + forward + + """)); + Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/second.html")); + + await Browser.Locator("a#back").TapAsync(); + await Browser.WaitForURLAsync("/first.html"); + Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/first.html")); + + await Browser.Locator("a#forward").TapAsync(); + await Browser.WaitForURLAsync("/second.html"); + Assert.That(await Browser.UrlAsync(), Is.EqualTo($"{PREFIX}/second.html")); + } + + [Test] + public async Task Should_work_with_url_match_for_same_document_navigations() + { + await Browser.GotoAsync(EmptyPage); + var resolved = false; + var waitPromise = Task.Run(async () => + { + await Browser.WaitForURLAsync(ThirdHtml()); + resolved = true; + }); + Assert.That(resolved, Is.False); + await Browser.EvaluateAsync("() => { history.pushState({}, '', '/first.html'); }"); + Assert.That(resolved, Is.False); + await Browser.EvaluateAsync("() => { history.pushState({}, '', '/second.html'); }"); + Assert.That(resolved, Is.False); + await Browser.EvaluateAsync("() => { history.pushState({}, '', '/third.html'); }"); + await waitPromise; + Assert.That(resolved, Is.True); + } + + [GeneratedRegex(@"#foobar$")] + private static partial Regex FoobarSuffix(); + + [GeneratedRegex(@"third\.html")] + private static partial Regex ThirdHtml(); +} diff --git a/tests/E2E.XUnit.V3.Tests/E2E.XUnit.V3.Tests.csproj b/tests/E2E.XUnit.V3.Tests/E2E.XUnit.V3.Tests.csproj index 9d764e906..43d87e126 100644 --- a/tests/E2E.XUnit.V3.Tests/E2E.XUnit.V3.Tests.csproj +++ b/tests/E2E.XUnit.V3.Tests/E2E.XUnit.V3.Tests.csproj @@ -19,6 +19,7 @@ +