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 @@ -761,7 +761,7 @@ The snapshot leaves out a checkbox's or radio's value, which `inputValue` still

The page script reads the text selected inside the focused input, textarea, or `contenteditable` host into `SemanticNode.Selection`, as upstream does. A caret, an unfocused field, and a secure field have none. The agent snapshot lists it as `selection="..."` after the field's value, verbatim and escaped (a lone space counts, an empty selection does not), as upstream does. The runner drops it from a field whose value or text holds a registered secret, and from a field cut at its length limit when the selection is not in the part shown. Differences: the port's `press` has no `times`, so one call presses the key once; and the snapshot shows a `value` as it is, a newline included, where upstream escapes it.

Upstream leaves an `aria-hidden` subtree out of its tree walk, and its text and visibility reads go through Playwright. Here every locator read comes from the tree, so the port lists the subtree and marks each node `AriaHidden` (.NET-only), and role queries and the agent skip those nodes. They are not hidden: a painted `aria-hidden` node is visible to `isVisible`, `toBeVisible`, and `toBeHidden`, as upstream, and a `visible` query keeps it. An action on one is not refused as hidden. A box with no size is not hidden here, since role queries skip hidden nodes and an empty landmark has no size. A hidden node reads its DOM text (`textContent`) instead of `innerText`, so a text query still finds a `visibility: hidden` copy.
Upstream leaves an `aria-hidden` subtree out of its tree walk, and its text and visibility reads go through Playwright. Here every locator read comes from the tree, so the port lists the subtree and marks each node `AriaHidden` (.NET-only), and role queries and the agent skip those nodes. They are not hidden: a painted `aria-hidden` node is visible to `isVisible`, `toBeVisible`, and `toBeHidden`, as upstream, and a `visible` query keeps it. An action on one is not refused as hidden. A box with no size is not hidden here, since role queries skip hidden nodes and an empty landmark has no size; `isVisible`, `toBeVisible`, `toBeHidden`, `WaitFor`, and the `visible` option read `NodeStates.EmptyBox` too (`LocatorEngine.IsNodeVisible`, .NET-only, web only), so an element with an empty box is not visible, as upstream. A node with no layout box (`display: contents`) and an `option` keep their hidden state. `NodeStates.Mixed` and `NodeStates.ExpandedAbsent` (.NET-only, web only) stand for upstream's nullable states: a role query for `Checked` or `Pressed` skips a mixed node (an indeterminate checkbox, `aria-pressed="mixed"`), and `Expanded = false` skips a node with no `aria-expanded`. `Level` of a `role="heading"` element is its `aria-level`, else 2. A hidden node reads its DOM text (`textContent`) instead of `innerText`, so a text query still finds a `visibility: hidden` copy.

Password fields and `autocomplete=current-password` fields are marked secure. Their values, and their `value` attribute, are omitted from the snapshot. A text, value, or attribute expectation on a secure field fails with `POLICY_DENIED`, as upstream. So do the `textContent`, `inputValue`, `allTextContents`, and `getAttribute` reads (for any attribute).

Expand Down
45 changes: 45 additions & 0 deletions docs-site/src/content/docs/api/E2E.Engine/NodeStates.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public bool Disabled { get; init; }
```


**Value:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

<a id="E2E_Engine_NodeStates_EmptyBox_"></a>
<a id="E2E_Engine_NodeStates_EmptyBox"></a>

### EmptyBox


```csharp
public bool EmptyBox { get; init; }
```


The node renders a box with no width or no height. It is not visible, though role queries and the agent still read it (.NET-only; upstream puts this in its `hidden` state). Web only.

**Value:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

<a id="E2E_Engine_NodeStates_Expanded_"></a>
Expand All @@ -79,6 +94,21 @@ public bool Expanded { get; init; }
```


**Value:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

<a id="E2E_Engine_NodeStates_ExpandedAbsent_"></a>
<a id="E2E_Engine_NodeStates_ExpandedAbsent"></a>

### ExpandedAbsent


```csharp
public bool ExpandedAbsent { get; init; }
```


The node has no `aria-expanded` of true or false. A role query for `Expanded = false` skips it. Web only (.NET-only; upstream reads the state as nullable).

**Value:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

<a id="E2E_Engine_NodeStates_Focused_"></a>
Expand Down Expand Up @@ -109,6 +139,21 @@ The node does not render: `display: none`, `visibility`, skipped content, a clos

**Value:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

<a id="E2E_Engine_NodeStates_Mixed_"></a>
<a id="E2E_Engine_NodeStates_Mixed"></a>

### Mixed


```csharp
public bool Mixed { get; init; }
```


The node is a checkbox or a button in the mixed state. A role query for `Checked` or `Pressed` of either value skips it. Web only (.NET-only; upstream reads the state as nullable).

**Value:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

<a id="E2E_Engine_NodeStates_Pressed_"></a>
<a id="E2E_Engine_NodeStates_Pressed"></a>

Expand Down
9 changes: 9 additions & 0 deletions src/E2E/Engine/Observation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public sealed class NodeStates

public bool Expanded { get; init; }

/// <summary>The node has no <c>aria-expanded</c> of true or false. A role query for <c>Expanded = false</c> skips it. Web only (.NET-only; upstream reads the state as nullable).</summary>
public bool ExpandedAbsent { get; init; }

/// <summary>The node is a checkbox or a button in the mixed state. A role query for <c>Checked</c> or <c>Pressed</c> of either value skips it. Web only (.NET-only; upstream reads the state as nullable).</summary>
public bool Mixed { get; init; }

/// <summary>The node renders a box with no width or no height. It is not visible, though role queries and the agent still read it (.NET-only; upstream puts this in its <c>hidden</c> state). Web only.</summary>
public bool EmptyBox { get; init; }

public bool Focused { get; init; }

/// <summary>The node does not render: <c>display: none</c>, <c>visibility</c>, skipped content, a closed <c>details</c>. This is what <c>isVisible</c> reads.</summary>
Expand Down
18 changes: 17 additions & 1 deletion src/E2E/Engine/PageScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,18 @@ internal static class PageScript
if (el instanceof HTMLInputElement && (el.type === "checkbox" || el.type === "radio")) return el.checked;
return el.getAttribute("aria-checked") === "true";
};
// The mixed state of a checkbox (indeterminate, or aria-checked) or a
// button (aria-pressed): a role query for either value skips it.
const mixedOf = (el) => el instanceof HTMLInputElement && el.type === "checkbox"
? el.indeterminate
: el.getAttribute("aria-checked") === "mixed" || el.getAttribute("aria-pressed") === "mixed";
// aria-level wins; an h1 to h6 reads its tag; any other heading is level 2 (HTML-AAM).
const levelOf = (el, role) => {
const tagLevel = /^H[1-6]$/.test(el.tagName) ? Number(el.tagName.slice(1)) : null;
if (role !== "heading") return tagLevel;
const ariaLevel = Number.parseInt(el.getAttribute("aria-level") || "", 10);
return Number.isInteger(ariaLevel) && ariaLevel > 0 ? ariaLevel : tagLevel ?? 2;
};
// A link target as the model reads it: origin and path, without
// userinfo, query, or fragment, which routinely carry tokens. A
// dropped query or fragment leaves "?…" or "#…" behind, so the value
Expand Down Expand Up @@ -999,9 +1011,13 @@ internal static class PageScript
testId,
placeholder: el.getAttribute("placeholder"),
inputPurpose: secure ? "password" : null,
level: /^H[1-6]$/.test(el.tagName) ? Number(el.tagName.slice(1)) : null,
level: levelOf(el, role),
disabled: disabledOf(el, role),
checked: checkedOf(el),
mixed: mixedOf(el),
// A box with no width or height; an option has none while its select is closed.
emptyBox: !isHidden && el.tagName !== "OPTION" && (() => { const box = rectOf(el); return box !== null && (box.width <= 0 || box.height <= 0); })(),
expandedAbsent: !["true", "false"].includes(el.getAttribute("aria-expanded") || "") && el.tagName !== "DETAILS",
expanded: el.getAttribute("aria-expanded") === "true" || (el.tagName === "DETAILS" && el.open),
selected: el.tagName === "OPTION" ? !!el.selected : (el.getAttribute("aria-selected") || "").toLowerCase() === "true",
pressed: el.getAttribute("aria-pressed") === "true",
Expand Down
9 changes: 9 additions & 0 deletions src/E2E/Engine/WebEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,9 @@ private static SemanticNode ToNode(WebNode dto, string testIdAttribute)
Checked = dto.Checked,
Disabled = dto.Disabled,
Expanded = dto.Expanded,
ExpandedAbsent = dto.ExpandedAbsent,
Mixed = dto.Mixed,
EmptyBox = dto.EmptyBox,
Focused = dto.Focused,
Hidden = dto.Hidden,
AriaHidden = dto.AriaHidden,
Expand Down Expand Up @@ -3853,6 +3856,12 @@ private sealed class WebNode

public bool Expanded { get; set; }

public bool ExpandedAbsent { get; set; }

public bool Mixed { get; set; }

public bool EmptyBox { get; set; }

public bool Selected { get; set; }

public bool Pressed { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/E2E/Expect/Expect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private Task VisibilityAsync(string matcher, bool visible, TimeSpan? timeout, Ca
return Verdict.Strict(matches.Count);
}

var shown = matches.Count == 1 && !matches[0].States.Hidden;
var shown = matches.Count == 1 && LocatorEngine.IsNodeVisible(matches[0]);
var observed = matches.Count switch
{
0 => "absent",
Expand Down
5 changes: 3 additions & 2 deletions src/E2E/Locator/LocatorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ internal sealed class LocatorEngine(LocatorEngineOptions options)
internal bool LocatesInEngine => options.Locate is not null;

/// <summary>Canonical visibility predicate over a semantic node snapshot (upstream's <c>isNodeVisible</c>).</summary>
internal static bool IsNodeVisible(SemanticNode? node) => node is { States.Hidden: false };
internal static bool IsNodeVisible(SemanticNode? node) =>
node is { States: { Hidden: false, EmptyBox: false } };

/// <summary>
/// Denies reading a value, text, or attribute when any node is a secure field: the engine withholds them there, so what reads as
Expand Down Expand Up @@ -294,7 +295,7 @@ internal async Task<IReadOnlyList<SemanticNode>> ResolveOnceAsync(LocatorExpress

// An engine answers `visible` inside scopes, filters, and indices, where only it can. A direct query is held to the same
// predicate here, so it never resolves to a node its own state calls hidden.
return expression.Visible && expression.Kind is not ("filter" or "index" or "selector") ? nodes.Where(node => !node.States.Hidden).ToList() : nodes;
return expression.Visible && expression.Kind is not ("filter" or "index" or "selector") ? nodes.Where(node => IsNodeVisible(node)).ToList() : nodes;
}
catch (EngineException ex) when (retried is not null && !token.IsCancellationRequested && reads.CutOff(ex))
{
Expand Down
19 changes: 10 additions & 9 deletions src/E2E/Locator/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ TestException NotVisible(Exception? cause)
}

sampled = true;
if (node is { States.Hidden: false })
if (LocatorEngine.IsNodeVisible(node))
{
return;
}
Expand Down Expand Up @@ -937,12 +937,12 @@ await samples.RecordAsync(_screen.Steps, token, () => Time.PollReadsAsync(new Po
throw await _screen.Engine.AmbiguousAsync(Query, matches.Count, token).ConfigureAwait(false);
}

samples.Add(matches.Count == 0 ? "absent" : matches[0].States.Hidden ? "hidden" : "visible");
samples.Add(matches.Count == 0 ? "absent" : LocatorEngine.IsNodeVisible(matches[0]) ? "visible" : "hidden");
return state switch
{
WaitForState.Attached => matches.Count == 1,
WaitForState.Visible => matches.Count == 1 && !matches[0].States.Hidden,
WaitForState.Hidden => matches.Count == 0 || matches[0].States.Hidden,
WaitForState.Visible => matches.Count == 1 && LocatorEngine.IsNodeVisible(matches[0]),
WaitForState.Hidden => matches.Count == 0 || !LocatorEngine.IsNodeVisible(matches[0]),
_ => matches.Count == 0,
};
},
Expand Down Expand Up @@ -1593,7 +1593,7 @@ private static EngineException UnsupportedDisplayValue() =>

private static bool Matches(SemanticNode node, LocatorExpression query, bool includeHidden)
{
if (query.Visible && node.States.Hidden)
if (query.Visible && !LocatorEngine.IsNodeVisible(node))
{
return false;
}
Expand Down Expand Up @@ -1630,15 +1630,16 @@ private static bool RoleMatches(SemanticNode node, LocatorExpression query, bool
}

var states = node.States;
return StateIs(query.Checked, states.Checked)
return StateIs(query.Checked, states.Checked, states.Mixed)
&& StateIs(query.Disabled, states.Disabled)
&& StateIs(query.Selected, states.Selected)
&& StateIs(query.Expanded, states.Expanded)
&& StateIs(query.Pressed, states.Pressed)
&& StateIs(query.Expanded, states.Expanded, states.ExpandedAbsent)
&& StateIs(query.Pressed, states.Pressed, states.Mixed)
&& (query.Level is null || node.Level == query.Level);
}

private static bool StateIs(bool? wanted, bool actual) => wanted is null || wanted.Value == actual;
/// <summary>A state with no value to match (mixed, or an attribute that is absent) matches neither true nor false, as Playwright's role selector reads it.</summary>
private static bool StateIs(bool? wanted, bool actual, bool unset = false) => wanted is null || (!unset && wanted.Value == actual);

/// <summary>
/// Whether the node's own name, text, or value, or a descendant's, contains the text, ignoring case.
Expand Down
62 changes: 25 additions & 37 deletions tests/E2E.NUnit.Tests/LocatorIsVisible/LocatorIsVisibleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,21 @@ namespace E2E.NUnit.Tests.LocatorIsVisible;
/// </summary>
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("<div>Hi</div><span></span>"));
await Browser.GotoAsync(Content("<div>Hi</div><span></span>"));

var div = Browser.Locator("div");
Equal(true, await div.IsVisibleAsync());
Equal(false, await div.IsHiddenAsync());
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());
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());
});
Equal(false, await Browser.Locator("no-such-element").IsVisibleAsync());
Equal(true, await Browser.Locator("no-such-element").IsHiddenAsync());
}

[Test]
Expand Down Expand Up @@ -127,38 +123,30 @@ await Browser.GotoAsync(Content("""
}
}

// 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("<button><span></span>a button</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("button").WaitForAsync(new LocatorWaitForOptions { State = WaitForState.Visible });
});
await Browser.GotoAsync(Content("<button><span></span>a button</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("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("<div role=button><span></span>a button</div>"));
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 });
});
await Browser.GotoAsync(Content("<div role=button><span></span>a button</div>"));
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]
Expand Down
Loading
Loading