Skip to content

Commit b474a39

Browse files
authored
fix(windows): stop two UI tests reading a rebuilt row and a clickable point once (#4297)
1 parent e3ca883 commit b474a39

8 files changed

Lines changed: 74 additions & 31 deletions

File tree

‎.claude/skills/fix-issue/references/verification-windows.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Read this when a `verify.sh` verdict needs interpreting for the Windows app, bef
3434
- **They drive the real app through UI Automation (FlaUI)** and take the mouse and keyboard. `TABLEPRO_APP` names the published `TablePro.exe`, which the `ui` step finds under `windows/src/TablePro.App/bin/` in the `publish` folder for the SDK's runtime identifier; run `app` first.
3535
- **Each launch gets a new data folder** through `TABLEPRO_DATA_DIR`, with its theme and row limit written into `settings.json`, so the user's connections never show and nothing carries over.
3636
- **`windows/ci/desktop.ps1` is for hosted runners.** It writes a machine policy, stops system processes and changes the display mode. Never run it on a developer's machine.
37-
- **Keys and clicks go through `AppSession`.** It brings the app to the front first, and `Chord`, `Click(element, landed)`, `Drag(..., landed)` and `TypeInto` try up to three times, waiting 5 seconds each for the app to show the input landed. A new test sends a shortcut with `Chord` and a `landed` check, never a bare `Keyboard.TypeSimultaneously`.
37+
- **Keys and clicks go through `AppSession`.** It brings the app to the front first, and `Chord`, `Click(element, landed)`, `Drag(..., landed)` and `TypeInto` try up to three times, waiting 5 seconds each for the app to show the input landed. A new test sends a shortcut with `Chord` and a `landed` check, never a bare `Keyboard.TypeSimultaneously`. Every click reads the element's clickable point again until it has one, since a live element's point can fail to read for a moment; a test never calls FlaUI's own `Click`, `RightClick` or `DoubleClick`.
38+
- **A list the app builds again after a change drops the row a test just found.** Read a row of such a list inside `AppSession.Holds`, which counts a gone row or its unsupported state as not yet (`windows/docs/decisions/0004-app-shell.md`, What the runner showed).
3839
- **A drag goes through `SendInput`** (`PointerInput`). FlaUI moves the pointer with `SetCursorPos`, which WinUI does not take as a pointer moving with the button down.
3940
- **UIA reads an empty Name as none.** WinUI reports an empty name as not supported, so FlaUI's `Name` throws `PropertyNotSupportedException`: it did once on x64, in run 41566360608. Read a name that can be empty through `Properties.Name.ValueOrDefault`.
4041
- **`TablePattern.ColumnHeaders` is empty on Windows App SDK 2.6.4.** `CUIATableProviderWrapper::GetColumnHeaders` releases its SAFEARRAY before filling it; microsoft/microsoft-ui-xaml#11820 fixed it after 2.6.4. Read the headers from the header peers, or a cell's `TableItem` while its header is realized (`windows/docs/decisions/0003-data-grid-control.md`, Consequences).

‎windows/docs/decisions/0004-app-shell.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ Facts the CI runs on `windows-2025` and `windows-11-arm` established, which the
209209
- `TreeViewItem`'s multiple-selection check box is raw to UI Automation, so the tests check a row through its `SelectionItem` pattern, which reads that box.
210210
- A click on a `MenuFlyoutSubItem` did not always open its submenu, and FlaUI's pointer glides to the next item across its neighbours, whose own submenus then open. The tests open a submenu through `ExpandCollapse` and invoke its item.
211211
- `windows-2025` drags a window as an outline: "Show window contents while dragging" is off there (`SPI_GETDRAGFULLWINDOWS` 0), while `windows-11-arm` has it on. TabView merges a dragged tab into another strip from `InputNonClientPointerSource.WindowRectChanging`, which the outline drag raised once, on release; with window contents on, the same drag raised it 82 times and the tab merged and tore out again before the release (run 41837948701). CI turns the setting on with `Set-DragFullWindows` in `windows/ci/desktop.ps1`.
212+
- A list the app builds again after each change (saved queries, the Connections page, history) drops every row, and a row a client found a moment before reads as gone: `UIA_E_ELEMENTNOTAVAILABLE` for its properties, and not supported for a tree row's `ExpandCollapseState`, since WinUI's `TreeViewItemDataAutomationPeer` throws once its item has no container. `AQueryIsSavedIntoAFolderAndARunIsPinned` found the folder row just before the move rebuilt the tree and read it just after, and failed in 7 of 12 rounds on `windows-11-arm` (run 43737243526). Read from the row found before the move, the old check threw `PropertyNotSupportedException` 12 times in 12, and the same check inside `AppSession.Holds`, which reads the new row on the next try, passed 12 times in 12; with it, both themes of the test passed 30 rounds in five `windows-11-arm` jobs and 6 on `windows-2025` (run 43742489353).
213+
- A live element's clickable point can fail to read for a moment: on the Go to page button, with the same runtime id and the hit test at its center finding it, 22 of 22,231 reads over 216 page changes failed on `windows-11-arm` and 4 of 5,762 over 48 on `windows-2025`, after Go to page and after Next page alike, each between two reads that passed (runs 43737243526 and 43742489353). FlaUI's `Click` throws `NoClickablePointException` on the first failed read, which failed `ATableKeepsItsPageSizeAndGoesToAPageInsideItsCount` in runs 43690747382 and 43707549355; no flyout covered the button. `AppSession` reads the point again for up to 5 seconds before each click.
212214
## Consequences
213215

214216
- The core gained the library and connecting through it: `Core::library`, `connect_saved`, `test_connection`, `parse_connection_url`, `DatabaseSession::table_query` and `sorted_query` with `sql_syntax::with_order_by` and `sort_reference`, `ResultSet::sorted` over `value::order`, and `Core::with_system_secrets`, which keeps secrets in Credential Manager on Windows (`core/ARCHITECTURE.md`, Platforms).

‎windows/tests/TablePro.App.UITests/AppSession.cs‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System.Diagnostics;
2+
using System.Runtime.InteropServices;
23
using FlaUI.Core;
34
using FlaUI.Core.AutomationElements;
45
using FlaUI.Core.Capturing;
56
using FlaUI.Core.Definitions;
7+
using FlaUI.Core.Exceptions;
68
using FlaUI.Core.Input;
79
using FlaUI.Core.Tools;
810
using FlaUI.Core.WindowsAPI;
@@ -14,6 +16,7 @@ namespace TablePro.App.UITests;
1416
internal sealed class AppSession : IDisposable
1517
{
1618
public static readonly TimeSpan Wait = TimeSpan.FromSeconds(30);
19+
private const int ElementNotAvailable = unchecked((int)0x80040201);
1720
private static readonly PackageTestState _packageState = new();
1821

1922
private readonly UIA3Automation _automation = new();
@@ -95,7 +98,7 @@ public static string LibraryFile(string folder) =>
9598
public void Drag(AutomationElement from, AutomationElement to)
9699
{
97100
Front();
98-
Drag(from.GetClickablePoint(), to.GetClickablePoint());
101+
Drag(PointOf(from), PointOf(to));
99102
}
100103

101104
// The Arm64 runner's own windows take keys and drags now and then, so each goes again until it lands.
@@ -142,7 +145,23 @@ public void DragThrough(Window window, Func<System.Drawing.Point> start, Func<Sy
142145
}
143146

144147
public void RightClick(AutomationElement element, Func<bool> landed) =>
145-
Again(() => element.RightClick(), landed, "The right-click");
148+
Again(() => RightClick(element), landed, "The right-click");
149+
150+
public void RightClick(AutomationElement element)
151+
{
152+
var point = PointOf(element);
153+
Front();
154+
Mouse.RightClick(point);
155+
FlaUI.Core.Input.Wait.UntilInputIsProcessed();
156+
}
157+
158+
public void DoubleClick(AutomationElement element)
159+
{
160+
var point = PointOf(element);
161+
Front();
162+
Mouse.DoubleClick(point);
163+
FlaUI.Core.Input.Wait.UntilInputIsProcessed();
164+
}
146165

147166
public void Chord(Func<bool> landed, params VirtualKeyShort[] keys) =>
148167
Again(() => Keyboard.TypeSimultaneously(keys), landed, string.Join("+", keys));
@@ -245,7 +264,7 @@ public void Check(AutomationElement row, bool isChecked = true)
245264
{
246265
if (box.AsCheckBox().IsChecked != isChecked)
247266
{
248-
box.Click();
267+
Click(box);
249268
}
250269

251270
return;
@@ -265,7 +284,7 @@ public void Check(AutomationElement row, bool isChecked = true)
265284
return;
266285
}
267286

268-
row.Click();
287+
Click(row);
269288
}
270289

271290
// The ContentDialog template names its primary button PrimaryButton; a name tells it from a dialog still closing.
@@ -353,19 +372,47 @@ public AutomationElement FindByName(string name, AutomationElement? scope = null
353372

354373
public AutomationElement FindByNamePrefix(string prefix, AutomationElement scope) =>
355374
Retry.WhileNull(
356-
() => scope.FindAllDescendants().FirstOrDefault(element =>
357-
element.Properties.Name.TryGetValue(out var name) && name.StartsWith(prefix, StringComparison.Ordinal)),
375+
() => scope.FindAllDescendants().FirstOrDefault(element => Holds(() =>
376+
element.Properties.Name.TryGetValue(out var name) && name.StartsWith(prefix, StringComparison.Ordinal))),
358377
Wait,
359378
throwOnTimeout: false).Result
360379
?? throw Failure($"No element named {prefix}... appeared.");
361380

381+
// A list built again after a change drops the row just found, which WinUI then reports gone or, for a tree row's state, not supported.
382+
public static bool Holds(Func<bool> check)
383+
{
384+
try
385+
{
386+
return check();
387+
}
388+
catch (Exception exception) when (exception is PropertyNotSupportedException or ElementNotAvailableException
389+
|| exception is COMException { HResult: ElementNotAvailable })
390+
{
391+
return false;
392+
}
393+
}
394+
362395
// Mouse and keys reach the foreground window, and the Arm64 runner opens windows of its own over the app.
363396
public void Front(Window? window = null) => (window ?? Window).SetForeground();
364397

365398
public void Click(AutomationElement element, Window? window = null)
366399
{
400+
var point = PointOf(element);
367401
Front(window);
368-
element.Click();
402+
Mouse.Click(point);
403+
FlaUI.Core.Input.Wait.UntilInputIsProcessed();
404+
}
405+
406+
// FlaUI's own click gives up on the first failed read, and a live element's point can fail to read for a moment.
407+
public System.Drawing.Point PointOf(AutomationElement element)
408+
{
409+
var point = System.Drawing.Point.Empty;
410+
if (!Retry.WhileFalse(() => element.TryGetClickablePoint(out point), TimeSpan.FromSeconds(5), throwOnTimeout: false).Success)
411+
{
412+
throw Failure("The element to click has no clickable point.", screen: true);
413+
}
414+
415+
return point;
369416
}
370417

371418
// A window the runner opens for a moment can take a click, so it goes again until the app shows it landed.

‎windows/tests/TablePro.App.UITests/BrowseObjectsTests.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ private static void OpenAgain(AppSession app, AutomationElement run, string tab)
9292
{
9393
for (var attempt = 0; attempt < 3; attempt++)
9494
{
95-
app.Front();
96-
run.DoubleClick();
95+
app.DoubleClick(run);
9796
if (Retry.WhileNull(
9897
() => app.Window.FindFirstDescendant(condition =>
9998
condition.ByAutomationId("SqlEditor").And(condition.ByName($"SQL editor, {tab}"))),

‎windows/tests/TablePro.App.UITests/ConnectionSecurityTests.cs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ public void SqlServerStrictAndWindowsAuthenticationAreSavedAndRestored(string th
3636
app.Find("SaveButton", form).AsButton().Invoke();
3737
app.WaitForClose("New connection");
3838

39-
var row = app.FindByNamePrefix("Strict SQL Server,", app.Find("ConnectionTree"));
40-
app.Front();
41-
row.RightClick();
39+
app.RightClick(app.FindByNamePrefix("Strict SQL Server,", app.Find("ConnectionTree")));
4240
app.FindMenuItem("Edit...").Patterns.Invoke.Pattern.Invoke();
4341
var edit = app.WaitForWindow("Edit connection");
4442
Assert.Equal("Windows authentication", app.Find("AuthBox", edit).AsComboBox().SelectedItem?.Text);

‎windows/tests/TablePro.App.UITests/SavedQueriesTests.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public void AQueryIsSavedIntoAFolderAndARunIsPinned(string theme)
6161
bool Shown() => tree.FindFirstDescendant(condition =>
6262
condition.ByName($"Five tracks, {FiveTracks}")) is { IsOffscreen: false };
6363
Assert.True(Retry.WhileFalse(
64-
() => Folder().Patterns.ExpandCollapse.Pattern.ExpandCollapseState.Value == ExpandCollapseState.Expanded && Shown(),
64+
() => AppSession.Holds(() =>
65+
Folder().Patterns.ExpandCollapse.Pattern.ExpandCollapseState.Value == ExpandCollapseState.Expanded && Shown()),
6566
AppSession.Wait,
6667
throwOnTimeout: false).Success);
6768
app.Screenshot("72-saved-query-folders");
@@ -81,22 +82,22 @@ bool Shown() => tree.FindFirstDescendant(condition =>
8182
app.RightClick(app.FindByNamePrefix(FiveTracks, runs), () => app.TryFindMenuItem("Pin") is not null);
8283
app.FindMenuItem("Pin").Patterns.Invoke.Pattern.Invoke();
8384
var pinned = Retry.WhileFalse(
84-
() => app.FindByNamePrefix(FiveTracks, runs).Name.EndsWith(", Pinned", StringComparison.Ordinal),
85+
() => AppSession.Holds(() => app.FindByNamePrefix(FiveTracks, runs).Name.EndsWith(", Pinned", StringComparison.Ordinal)),
8586
AppSession.Wait,
8687
throwOnTimeout: false).Success;
8788
Assert.True(pinned, app.FindByNamePrefix(FiveTracks, runs).Name);
8889
// The list is built again with the pinned run first, and UI Automation lists a row before it is drawn.
8990
Assert.True(Retry.WhileFalse(
90-
() => app.FindByNamePrefix(FiveTracks, runs) is { IsOffscreen: false } row && row.BoundingRectangle.Height > 0,
91+
() => AppSession.Holds(() => app.FindByNamePrefix(FiveTracks, runs) is { IsOffscreen: false } row && row.BoundingRectangle.Height > 0),
9192
AppSession.Wait,
9293
throwOnTimeout: false).Success);
9394
Thread.Sleep(1_000);
9495
app.Screenshot("73-history-pinned");
9596

9697
app.RightClick(app.FindByNamePrefix(FiveTracks, runs), () => app.TryFindMenuItem("Unpin") is not null);
9798
app.FindMenuItem("Unpin").Patterns.Invoke.Pattern.Invoke();
98-
Assert.True(Retry.WhileTrue(
99-
() => app.FindByNamePrefix(FiveTracks, runs).Name.EndsWith(", Pinned", StringComparison.Ordinal),
99+
Assert.True(Retry.WhileFalse(
100+
() => AppSession.Holds(() => !app.FindByNamePrefix(FiveTracks, runs).Name.EndsWith(", Pinned", StringComparison.Ordinal)),
100101
AppSession.Wait,
101102
throwOnTimeout: false).Success);
102103
}
@@ -106,8 +107,7 @@ private static void InsertSaved(AppSession app, Func<AutomationElement> row, str
106107
{
107108
for (var attempt = 0; attempt < 3; attempt++)
108109
{
109-
app.Front();
110-
row().DoubleClick();
110+
app.DoubleClick(row());
111111
if (Retry.WhileFalse(
112112
() => AppSession.EditorText(app.FindEditor(tab)).TrimEnd('\r', '\n').Length > 0,
113113
TimeSpan.FromSeconds(5),

‎windows/tests/TablePro.App.UITests/ShellPolishTests.cs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ public void APasswordIsAskedForWhenNoneIsSaved(string theme)
178178
// The form saved the empty password it was given; taking it away leaves the connection with none saved.
179179
var target = $"TablePro/connection/{ConnectionId(app.DataFolder)}/database-password";
180180
Credentials.Delete(target);
181-
var row = app.FindByNamePrefix("Asks,", app.Find("ConnectionTree"));
182-
app.Front();
183-
row.DoubleClick();
181+
app.DoubleClick(app.FindByNamePrefix("Asks,", app.Find("ConnectionTree")));
184182

185183
var box = app.Find("SecretBox");
186184
app.Screenshot("18-password-asked");
@@ -367,13 +365,12 @@ public void TheConnectionsPageRenamesTagsDropsAndSelects(string theme)
367365
app.Chord(() => app.TryFind("RenameBox") is null, VirtualKeyShort.RETURN);
368366
var music = app.FindByNamePrefix("Music,", tree);
369367

370-
app.Front();
371-
music.RightClick();
368+
app.RightClick(music);
372369
app.OpenSubmenu("Tags", "New tag...").Patterns.Invoke.Pattern.Invoke();
373370
app.Find("NameDialogBox").AsTextBox().Text = "demo";
374371
app.PressDialogButton();
375372
var tagged = Retry.WhileFalse(
376-
() => app.FindByNamePrefix("Music,", tree).Name.EndsWith("demo", StringComparison.Ordinal),
373+
() => AppSession.Holds(() => app.FindByNamePrefix("Music,", tree).Name.EndsWith("demo", StringComparison.Ordinal)),
377374
AppSession.Wait,
378375
throwOnTimeout: false).Success;
379376
Assert.True(tagged, app.FindByNamePrefix("Music,", tree).Name);

‎windows/tests/TablePro.App.UITests/WorkspaceDailyUseTests.cs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ public void AConnectionsColourAndTagsMarkItsWindow(string theme)
5757

5858
app.Find("HomeItem").AsListBoxItem().Select();
5959
var tree = app.Find("ConnectionTree");
60-
app.Front();
61-
app.FindByNamePrefix("Orders,", tree).RightClick();
60+
app.RightClick(app.FindByNamePrefix("Orders,", tree));
6261
app.OpenSubmenu("Tags", "New tag...").Patterns.Invoke.Pattern.Invoke();
6362
app.Find("NameDialogBox").AsTextBox().Text = "production";
6463
app.PressDialogButton();
65-
Retry.WhileFalse(
66-
() => app.FindByNamePrefix("Orders,", tree).Name.EndsWith("production", StringComparison.Ordinal),
64+
Assert.True(Retry.WhileFalse(
65+
() => AppSession.Holds(() => app.FindByNamePrefix("Orders,", tree).Name.EndsWith("production", StringComparison.Ordinal)),
6766
AppSession.Wait,
68-
throwOnTimeout: false);
67+
throwOnTimeout: false).Success);
6968

7069
app.Find("Rail_Orders").AsListBoxItem().Select();
7170
app.FindByName("Album", app.Find("ObjectTree"));

0 commit comments

Comments
 (0)