Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ Differences from upstream:
- `Browser.EvaluateAsync<T>` takes the script as a string (an expression, or a function source that is called with the optional argument) and deserializes the JSON result to `T`. A throwing script fails with `EVALUATE_FAILED` and the message it threw, a result that is not JSON (such as `Infinity`) with `INVALID_ARGUMENT`, and a browser that closes under it with `ENGINE_FAILURE`.
- `Browser.RouteAsync` takes the same glob string or `Regex` as `WaitForResponseAsync`, and a handler that returns a `Task`. `WebRoute` has `Request`, `FetchAsync` (`WebResponse`: `Url`, `Status`, `Headers`, `TextAsync`, `JsonAsync`; takes no options, adds the configured site headers, runs under the action timeout, never follows a redirect, and does not decide the route; a body the browser could not read fails `TextAsync` and `JsonAsync` with `ACTION_FAILED` naming `route.fetch`), `FulfillAsync` (`RouteFulfillResponse`: `Status`, `Headers`, `ContentType`, `Json`, `Body`, `Path`), `ContinueAsync` (`RouteContinueOverrides`: `Url`, `Method`, `Headers`, `PostData`), `FallbackAsync`, and `AbortAsync`, with upstream's checks, codes, and messages. `UnrouteAsync` removes the routes added with an equal glob, or a `Regex` with the same source and options. `UnrouteAllAsync` removes every route added through the browser and keeps the configured site headers. `Path` resolves against the project root (`E2ESessionOptions.ProjectRoot`, which `E2ETest` sets to the config's directory, else the working directory). Differences: options are typed, so an unknown key, a non-string header value, and an `abort` error code do not compile instead of failing with `INVALID_ARGUMENT`; `Json` is serialized with `System.Text.Json` web defaults, and `null` means no JSON body; a handler error raised after the test's last browser call is collected by `E2ESession.SettleAttemptAsync` (as upstream's `settleAttempt`) and fails the attempt at its end, as for a dialog handler (see the `OnDialogAsync` entry): the settle waits within the cleanup timeout for a handler still running, and one that outlives it fails the attempt with `CLEANUP_TIMEOUT` naming it, after which what it throws is dropped.
- `Browser.AddInitScriptAsync` takes source or a `WebInitScript`, and a JSON argument only with `WebInitScript.FromFunction`, the function's JavaScript source.
- Each async `Browser` call is a harness step of kind `resource`, as upstream's `FixtureRecorder` records it, with the api name `browser.goto`, `reload`, `back`, `forward`, `url`, `title`, `waitForURL`, `evaluate`, `addInitScript`, `route`, `unroute`, `unrouteAll`, `waitForResponse`, `cookies`, `setCookies`, `clearCookies`, `setViewport`, `emulateMedia`, `onDialog`, `waitForDownload`, `waitForDownloads`, and `keyboard.*` and `mouse.*`, and upstream's labels (a URL or pattern, the key, `<n> chars` for typed text, the init script's path or kind, the media features). `browser.waitForURL` verifies, and `expect(browser)` matchers are `assertion` steps nested in it. A pattern label writes a regex as `/source/flags`, since .NET has no `String(RegExp)`. `Browser.Locator` and `FrameLocator` are not async and record nothing, as upstream. Argument checks the type system makes upstream's (a predicate pattern, an unknown option key) do not exist, so upstream's `fails with INVALID_ARGUMENT at %s` and `an option waitForURL does not take is refused` tests have no port. The port does not wrap each call in the action timeout, as `FixtureRecorder.run` does: a call keeps the bound it had. `Browser.ClearCookiesAsync(CookieFilter?)` is upstream's `clearCookies(options)`: a filter field is a `TextMatch` (a string matches exactly, a `Regex` matches the text, run by Playwright as JavaScript), and `Context.ClearCookiesAsync` does the removal. Upstream's `refuses an unknown filter and a value that is not a string or RegExp, clearing nothing` test and `tests/types/clear-cookies.ts` have no port: `CookieFilter` has no `url` member and the compiler refuses a field that is neither, so there is no `INVALID_ARGUMENT` path.
- Each async `Browser` call is a harness step of kind `resource`, as upstream's `FixtureRecorder` records it, with the api name `browser.goto`, `reload`, `back`, `forward`, `url`, `title`, `waitForURL`, `evaluate`, `addInitScript`, `route`, `unroute`, `unrouteAll`, `waitForResponse`, `cookies`, `setCookies`, `clearCookies`, `setViewport`, `emulateMedia`, `onDialog`, `waitForDownload`, `waitForDownloads`, and `keyboard.*` and `mouse.*`, and upstream's labels (a URL or pattern, the key, `<n> chars` for typed text, the init script's path or kind, the media features). `browser.waitForURL` verifies, and `expect(browser)` matchers are `assertion` steps nested in it. A pattern label (upstream's `patternLabel`) writes a glob string quoted, as `JSON.stringify` does, and a regex as `/source/flags`, since .NET has no `String(RegExp)`. `Browser.Locator` and `FrameLocator` are not async and record nothing, as upstream. Argument checks the type system makes upstream's (a predicate pattern, an unknown option key) do not exist, so upstream's `fails with INVALID_ARGUMENT at %s` and `an option waitForURL does not take is refused` tests have no port. The port does not wrap each call in the action timeout, as `FixtureRecorder.run` does: a call keeps the bound it had. `Browser.ClearCookiesAsync(CookieFilter?)` is upstream's `clearCookies(options)`: a filter field is a `TextMatch` (a string matches exactly, a `Regex` matches the text, run by Playwright as JavaScript), and `Context.ClearCookiesAsync` does the removal. Upstream's `refuses an unknown filter and a value that is not a string or RegExp, clearing nothing` test and `tests/types/clear-cookies.ts` have no port: `CookieFilter` has no `url` member and the compiler refuses a field that is neither, so there is no `INVALID_ARGUMENT` path.
- Navigation URLs resolve with `System.Uri`, not WHATWG. As upstream, only `http:`, `https:`, and the exact `about:blank` are admitted. But a tab or newline inside a URL is escaped, not stripped, so `view-\tsource:file:` opens as an http path under the base URL (or fails with `APP_URL_REQUIRED` without one) instead of `POLICY_DENIED`. A percent-encoded unreserved character in the path is decoded (`%66ile:` becomes `file:`). Neither opens a scheme other than http(s).

`DocumentEngine` supports `app.back` (it rebuilds the previous route), `app.restart`, and `app.clearState` (both drop the history and reopen the base URL). It has no viewport: `DocumentPage.OnScroll` and `DocumentElement.OnScroll` let a page load more rows when it is scrolled. It has no browser, so every `Browser` member fails with `UNSUPPORTED_CAPABILITY`. A custom engine opts in by implementing `IBrowserSession`, and by overriding the default `BackAsync`, `RestartAsync`, and `ClearStateAsync` on `IEngineSession`, which otherwise fail with `UNSUPPORTED_CAPABILITY`.
Expand Down
14 changes: 11 additions & 3 deletions src/E2E/Internal/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-License-Identifier: Apache-2.0

using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace E2E.Internal;
Expand Down Expand Up @@ -147,13 +149,19 @@ public static Func<string, bool> Matcher(object pattern) =>
_ => false,
};

/// <summary>The step label of a URL pattern, as upstream's <c>String(pattern)</c> writes it: a glob as it is, a regex as <c>/source/flags</c>.</summary>
public static string Label(object pattern) => pattern switch
/// <summary>
/// A URL pattern as steps and errors name it, upstream's <c>patternLabel</c>: a glob string quoted (<c>JSON.stringify</c>), a regex as
/// <c>/source/flags</c> (<c>String(RegExp)</c>).
/// </summary>
public static string PatternLabel(object pattern) => pattern switch
{
string glob => JsonSerializer.Serialize(glob, QuoteOptions),
Regex regex => "/" + regex + "/" + (regex.Options.HasFlag(RegexOptions.IgnoreCase) ? "i" : "") + (regex.Options.HasFlag(RegexOptions.Multiline) ? "m" : "") + (regex.Options.HasFlag(RegexOptions.Singleline) ? "s" : ""),
_ => (string)pattern,
_ => throw new ArgumentException("A URL pattern is a string or a Regex.", nameof(pattern)),
};

private static readonly JsonSerializerOptions QuoteOptions = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };

[GeneratedRegex("^[A-Za-z][A-Za-z0-9+.-]*:")]
private static partial Regex SchemePattern();
}
10 changes: 5 additions & 5 deletions src/E2E/Run/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Task<string> TitleAsync(CancellationToken cancellationToken = default) =>
public Task WaitForURLAsync(string url, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(url);
return RunStepAsync(StepKind.Resource, "browser.waitForURL", url, () =>
return RunStepAsync(StepKind.Resource, "browser.waitForURL", Routes.PatternLabel(url), () =>
{
Require("waitForURL");
return new BrowserExpect(this, negated: false).ToHaveURLAsync(url, timeout: timeout, cancellationToken: cancellationToken);
Expand All @@ -151,7 +151,7 @@ public Task WaitForURLAsync(string url, TimeSpan? timeout = null, CancellationTo
public Task WaitForURLAsync(Regex pattern, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(pattern);
return RunStepAsync(StepKind.Resource, "browser.waitForURL", Routes.Label(pattern), () =>
return RunStepAsync(StepKind.Resource, "browser.waitForURL", Routes.PatternLabel(pattern), () =>
{
Require("waitForURL");
return new BrowserExpect(this, negated: false).ToHaveURLAsync(pattern, timeout: timeout, cancellationToken: cancellationToken);
Expand Down Expand Up @@ -513,7 +513,7 @@ private async Task AddInitScriptStepAsync(WebInitScript script, object? arg, boo
}

private Task<WebResponse> WaitForResponseCoreAsync(object pattern, Func<Func<string, bool>> matcher, TimeSpan? timeout, CancellationToken cancellationToken) =>
RunStepAsync(StepKind.Resource, "browser.waitForResponse", Routes.Label(pattern), async () =>
RunStepAsync(StepKind.Resource, "browser.waitForResponse", Routes.PatternLabel(pattern), async () =>
{
var session = Require("waitForResponse");
var response = await session.WaitForResponseAsync(matcher(), timeout ?? _actionTimeout, Token(cancellationToken)).ConfigureAwait(false);
Expand All @@ -523,7 +523,7 @@ private Task<WebResponse> WaitForResponseCoreAsync(object pattern, Func<Func<str
private Task RouteCoreAsync(object pattern, Func<WebRoute, Task> handler, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(handler);
return RunStepAsync(StepKind.Resource, "browser.route", Routes.Label(pattern), () => RouteStepAsync(pattern, handler, cancellationToken));
return RunStepAsync(StepKind.Resource, "browser.route", Routes.PatternLabel(pattern), () => RouteStepAsync(pattern, handler, cancellationToken));
}

private async Task RouteStepAsync(object pattern, Func<WebRoute, Task> handler, CancellationToken cancellationToken)
Expand All @@ -544,7 +544,7 @@ async Task Decide(IBrowserRoute engineRoute)
}

private Task UnrouteCoreAsync(object pattern, CancellationToken cancellationToken) =>
RunStepAsync(StepKind.Resource, "browser.unroute", Routes.Label(pattern), () =>
RunStepAsync(StepKind.Resource, "browser.unroute", Routes.PatternLabel(pattern), () =>
RemoveRoutesAsync("unroute", stored => Routes.PatternsEqual(stored.Pattern, pattern), cancellationToken));

/// <summary>Unregisters the stored routes <paramref name="match"/> selects, newest first.</summary>
Expand Down
23 changes: 23 additions & 0 deletions tests/E2E.Tests/RoutePattern/PatternLabelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2026 TesterArmy.
// Modified by Dario Kondratiuk.
// SPDX-License-Identifier: Apache-2.0

using System.Text.RegularExpressions;

namespace E2E.Tests.RoutePattern;

/// <summary>
/// .NET-only: upstream's <c>patternLabel</c> is covered by the <c>wait</c> case of <c>fixture-runtime.test.ts</c>'s "maps an EngineError a
/// contributed method throws onto the runner taxonomy" (the label in a timeout message). The port has no <c>FixtureRecorder</c>
/// or <c>translated</c>, so that <c>it</c> cannot be ported and only the label itself is checked here.
/// </summary>
public sealed class PatternLabelTests
{
[Fact]
public void Quotes_a_glob_and_writes_a_regex_as_its_literal()
{
Assert.Equal("\"foo.css\"", E2E.Internal.Routes.PatternLabel("foo.css"));
Assert.Equal("\"**/a \\\"b\\\"\"", E2E.Internal.Routes.PatternLabel("**/a \"b\""));
Assert.Equal("/api\\/\\w+$/i", E2E.Internal.Routes.PatternLabel(new Regex(@"api\/\w+$", RegexOptions.IgnoreCase)));
}
}
Loading