Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,44 @@ device-runners wasm test \
| `--timeout` | `300` | Test execution timeout in seconds |
| `--headed` | `false` | Run browser in visible mode (useful for debugging) |
| `--server-port` | `0` (auto) | HTTP port for the local web server |
| `--browser-args` | *(none)* | Extra switches for the browser, quoted as in a shell. Repeatable. Use the `=` form: `--browser-args=--enable-unsafe-webgpu` |
| `--output` | `default` | CLI output format: `default`, `json`, `xml`, or `text` |

The test command also generates a `browser-console.log` file in the results directory containing all browser console output, similar to `logcat.txt` on Android or `ios-device-log.txt` on iOS.

#### Passing switches to the browser

`--browser-args` takes a command-line fragment and splits it using shell quoting rules, so a
switch whose value contains a space just needs quotes:

```bash
device-runners wasm test --app "$wwwroot" \
--browser-args='--enable-unsafe-webgpu "--host-resolver-rules=MAP a.test 127.0.0.1"'
```

It is repeatable, so this is equivalent:

```bash
device-runners wasm test --app "$wwwroot" \
--browser-args=--enable-unsafe-swiftshader \
--browser-args=--enable-unsafe-webgpu
```

Use the `=` form. Spectre.Console treats a following token that starts with `-` as another
option, so `--browser-args --enable-unsafe-webgpu` is rejected.

The switches are appended after everything the CLI sets itself, so they win for the switches
where Chrome keeps only the last occurrence. The full command line is printed as
`Browser args:` at the start of every run.

> [!NOTE]
> This configures the **browser**, not the app under test. The app is configured through the
> runner's own query-string parameters (`?device-runners-autorun=1`), the WASM equivalent of
> the `DEVICE_RUNNERS_*` environment variables used on the other platforms.

This is a passthrough — the CLI adds no graphics-related switches of its own. See
[WebGL and WebGPU](#webgl-and-webgpu) below for what those APIs need on a CI agent.

### `wasm serve`

| Option | Default | Description |
Expand Down Expand Up @@ -103,6 +137,37 @@ If the app fails to boot, check the Blazor WebAssembly publish output to ensure

Ensure the test app calls `AddConsoleResultChannel()` in its `Program.cs`. When using `UseVisualTestRunner` on `WebAssemblyHostBuilder`, the CLI configuration is automatically injected — when the browser navigates to `?device-runners-autorun=1`, the `EventStreamFormatter` console output is enabled.

### WebGL and WebGPU

A browser on a machine with no usable GPU — most CI agents — reports WebGL as unavailable
(`canvas.getContext("webgl")` returns `null`) and WebGPU as adapter-less
(`navigator.gpu.requestAdapter()` resolves to `null`). Headless mode is not the cause: a
visible browser behaves the same. The cause is the missing GPU.

Chrome can fall back to the SwiftShader software renderer, but only when asked. Measured with
Chrome 150 on GPU-less GitHub-hosted runners:

| Host | For WebGL | For WebGPU |
| --- | --- | --- |
| Linux | *(works already)* | `--enable-unsafe-webgpu` |
| macOS | `--enable-unsafe-swiftshader` | `--enable-unsafe-webgpu` |
| Windows | *(works already, via D3D11 WARP)* | `--enable-unsafe-webgpu --use-webgpu-adapter=swiftshader` |

Notes:

- On **Windows**, `--enable-unsafe-swiftshader` moves ANGLE onto the SwiftShader Vulkan
device, after which Dawn cannot enumerate any WebGPU adapter. Don't combine it with the
WebGPU switches there.
- `--use-webgpu-adapter=swiftshader` forces software rendering even when a real GPU is
present, so apply it on CI rather than everywhere.
- Avoid passing `--disable-gpu`. On Windows it removes the fallback WebGPU needs, and
`requestAdapter()` returns `null` even with `--use-webgpu-adapter=swiftshader`.
- `navigator.gpu` only exists in a [secure context]. The built-in server uses
`http://127.0.0.1`, which qualifies; a plain `http://` LAN address does not, and WebGPU
silently disappears there.

[secure context]: https://developer.mozilla.org/docs/Web/Security/Secure_Contexts

### Debugging with headed mode

Use `--headed` to launch a visible browser window. This lets you see the Blazor visual runner UI and inspect the browser console directly. Combined with `--timeout 0` (infinite), this is useful for interactive debugging.
Expand Down
73 changes: 73 additions & 0 deletions docs/articles/dotnet-test-wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ To change the test execution timeout, set `DeviceRunnersWasmTimeout` (default: 3
dotnet test MyApp.BrowserTests.csproj -p:DeviceRunnersWasmTimeout=600
```

### Passing switches to the browser

`DeviceRunnersWasmBrowserArgs` sets extra switches on the browser the CLI launches:

```bash
dotnet test MyApp.BrowserTests.csproj -p:DeviceRunnersWasmBrowserArgs=--enable-unsafe-webgpu
```

```xml
<PropertyGroup>
<DeviceRunnersWasmBrowserArgs>--enable-unsafe-swiftshader --enable-unsafe-webgpu</DeviceRunnersWasmBrowserArgs>
</PropertyGroup>
```

The value is a command-line fragment, not an MSBuild list. Separate switches with spaces and
quote any value that contains one, exactly as you would in a shell:

```xml
<PropertyGroup>
<DeviceRunnersWasmBrowserArgs>--enable-unsafe-webgpu "--host-resolver-rules=MAP a.test 127.0.0.1"</DeviceRunnersWasmBrowserArgs>
</PropertyGroup>
```

The switches are appended after everything the CLI sets itself, so they win for the switches
where Chrome keeps only the last occurrence. The full command line is printed as `Browser
args:` at the start of every run.

> [!NOTE]
> This configures the **browser**, not the app under test. The app is configured through the
> runner's own query-string parameters (`?device-runners-autorun=1`), which is the WASM
> equivalent of the `DEVICE_RUNNERS_*` environment variables used on the other platforms.

This is a passthrough — the CLI adds no graphics-related switches of its own. See
[WebGL and WebGPU](#webgl-and-webgpu) for the switches those APIs need on a CI agent.

## Troubleshooting

### Chrome not found
Expand All @@ -77,6 +112,44 @@ The CLI searches for Chrome/Chromium in standard installation paths. If your Chr

If the app fails to boot, check the Blazor WebAssembly publish output to ensure the `wwwroot` directory contains `_framework/blazor.webassembly.js` and the app's DLLs. The default timeout is 300 seconds — use `--timeout` to increase it for large test suites.

### WebGL and WebGPU

A browser on a machine with no usable GPU — which describes most CI agents — reports WebGL as
unavailable (`canvas.getContext("webgl")` returns `null`) and WebGPU as adapter-less
(`navigator.gpu.requestAdapter()` resolves to `null`). This is not caused by headless mode: a
visible browser behaves the same way. It is caused by there being no GPU.

Chrome can fall back to the SwiftShader software renderer, but only when asked. Measured with
Chrome 150 on GPU-less GitHub-hosted runners:

| Host | For WebGL | For WebGPU |
| --- | --- | --- |
| Linux | *(works already)* | `--enable-unsafe-webgpu` |
| macOS | `--enable-unsafe-swiftshader` | `--enable-unsafe-webgpu` |
| Windows | *(works already, via D3D11 WARP)* | `--enable-unsafe-webgpu --use-webgpu-adapter=swiftshader` |

For example, on a GPU-less Linux or macOS agent:

```bash
dotnet test MyApp.BrowserTests.csproj \
-p:DeviceRunnersWasmBrowserArgs="--enable-unsafe-swiftshader --enable-unsafe-webgpu"
```

Notes:

- On **Windows**, `--enable-unsafe-swiftshader` moves ANGLE onto the SwiftShader Vulkan
device, after which Dawn cannot enumerate any WebGPU adapter. Don't combine it with the
WebGPU switches there.
- `--use-webgpu-adapter=swiftshader` forces software rendering even when a real GPU is
present, so apply it on CI rather than in a checked-in property.
- Avoid `--disable-gpu`. On Windows it removes the fallback WebGPU needs, and
`requestAdapter()` returns `null` even with `--use-webgpu-adapter=swiftshader`.
- `navigator.gpu` only exists in a [secure context]. The built-in server uses
`http://127.0.0.1`, which qualifies; a plain `http://` LAN address does not, and WebGPU
silently disappears there.

[secure context]: https://developer.mozilla.org/docs/Web/Security/Secure_Contexts

### Console output is empty

When using `UseVisualTestRunner` on `WebAssemblyHostBuilder`, CLI configuration is automatically injected — the `EventStreamFormatter` console output is enabled when `?device-runners-autorun=1` is in the URL. If you're building a custom setup, ensure `AddConsoleResultChannel()` is called.
Expand Down
16 changes: 15 additions & 1 deletion src/DeviceRunners.Cli/Commands/Wasm/WasmTestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class Settings : BaseTestCommandSettings
[CommandOption("--timeout")]
[DefaultValue(300)]
public int Timeout { get; set; } = 300;

[Description("Extra switches for the browser, quoted as they would be in a shell. Repeatable. Example: --browser-args \"--enable-unsafe-webgpu\"")]
[CommandOption("--browser-args")]
public string[] BrowserArgs { get; set; } = [];
}

protected override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
Expand Down Expand Up @@ -136,7 +140,17 @@ protected override async Task<int> ExecuteAsync(CommandContext context, Settings
var testUrl = $"{url}?device-runners-autorun=1";
if (!string.IsNullOrWhiteSpace(settings.Filter))
testUrl += $"&device-runners-filter={Uri.EscapeDataString(settings.Filter)}";
await browser.LaunchAsync(testUrl, headless: !settings.Headed);
// Each --browser-args value is a command-line fragment: the shell that
// launched us only stripped the outer quotes, so the inner grouping still
// has to be resolved. "--" is deliberately left alone — it is reserved for
// arguments aimed at the app under test rather than at the browser.
var browserArgs = settings.BrowserArgs.SelectMany(ArgumentTokenizer.Tokenize).ToList();

await browser.LaunchAsync(
testUrl,
headless: !settings.Headed,
extraArguments: browserArgs);
WriteConsoleOutput($" Browser args: [grey]{Markup.Escape(string.Join(' ', browser.LaunchArguments))}[/]", settings);
WriteConsoleOutput($" Browser launched, running tests...", settings);

// Wait for test completion or timeout
Expand Down
88 changes: 88 additions & 0 deletions src/DeviceRunners.Cli/Services/ArgumentTokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace DeviceRunners.Cli.Services;

/// <summary>
/// Splits a command-line fragment into individual arguments using the quoting rules a
/// shell would apply.
/// </summary>
/// <remarks>
/// <para>
/// Needed because options such as <c>--browser-args</c> receive a whole fragment as a
/// single value — the shell that launched the CLI only stripped the outer quotes, so the
/// inner grouping still has to be resolved.
/// </para>
/// <para>
/// A backslash is only an escape character inside double quotes, so Windows paths can be
/// written without doubling: <c>--user-data-dir=C:\temp\profile</c> means what it looks
/// like. Inside double quotes, <c>\"</c> produces a literal quote.
/// </para>
/// </remarks>
public static class ArgumentTokenizer
{
public static IReadOnlyList<string> Tokenize(string? value)
{
var results = new List<string>();

if (string.IsNullOrWhiteSpace(value))
return results;

var current = new System.Text.StringBuilder();
var hasToken = false;
var quote = '\0';

for (var i = 0; i < value.Length; i++)
{
var c = value[i];

if (quote != '\0')
{
// Only double quotes support escaping, matching POSIX shells.
if (quote == '"' && c == '\\' && i + 1 < value.Length && value[i + 1] == '"')
{
current.Append('"');
i++;
continue;
}

if (c == quote)
{
quote = '\0';
continue;
}

current.Append(c);
continue;
}

if (c is '"' or '\'')
{
// An empty quoted section still produces an argument.
quote = c;
hasToken = true;
continue;
}

if (char.IsWhiteSpace(c))
{
if (hasToken)
{
results.Add(current.ToString());
current.Clear();
hasToken = false;
}

continue;
}

current.Append(c);
hasToken = true;
}

if (quote != '\0')
throw new FormatException($"Unbalanced {quote} quote in arguments: {value}");

if (hasToken)
results.Add(current.ToString());

return results;
}
}
Loading
Loading