|
| 1 | +// Copyright (c) Microsoft Corporation |
| 2 | +// The Microsoft Corporation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Text.Json; |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | + |
| 8 | +namespace Microsoft.PowerToys.UITest.Next; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Reference to a UI element resolved via winappcli. Wraps the resolved <see cref="Selector"/> |
| 12 | +/// (slug or text query), the owning <see cref="Session"/>, and the metadata captured at lookup |
| 13 | +/// time (control type, class name, name). |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// Element instances are <i>stateless on the wire</i> — every property read and every action |
| 17 | +/// shells out to <c>winapp ui …</c>. The cached <see cref="ControlType"/>, <see cref="ClassName"/>, |
| 18 | +/// and <see cref="Name"/> are the values seen at <c>Find</c> time; for fresh values, re-find. |
| 19 | +/// </remarks> |
| 20 | +public class Element |
| 21 | +{ |
| 22 | + internal Session? Owner { get; set; } |
| 23 | + |
| 24 | + /// <summary>The selector winappcli will use to address this element (semantic slug, ID, or text query).</summary> |
| 25 | + public string Selector { get; internal set; } = string.Empty; |
| 26 | + |
| 27 | + /// <summary>Cached control type at lookup time (e.g. "Button", "ToggleSwitch").</summary> |
| 28 | + public string ControlType { get; internal set; } = string.Empty; |
| 29 | + |
| 30 | + /// <summary>Cached class name at lookup time (e.g. "ToggleSwitch", "TextBlock").</summary> |
| 31 | + public string ClassName { get; internal set; } = string.Empty; |
| 32 | + |
| 33 | + /// <summary>Cached Name property at lookup time.</summary> |
| 34 | + public string Name { get; internal set; } = string.Empty; |
| 35 | + |
| 36 | + /// <summary>Top-left X (screen pixels) reported by <c>search</c> at lookup time.</summary> |
| 37 | + public int X { get; internal set; } |
| 38 | + |
| 39 | + /// <summary>Top-left Y (screen pixels) reported by <c>search</c> at lookup time.</summary> |
| 40 | + public int Y { get; internal set; } |
| 41 | + |
| 42 | + /// <summary>Bounding-box width reported by <c>search</c> at lookup time.</summary> |
| 43 | + public int Width { get; internal set; } |
| 44 | + |
| 45 | + /// <summary>Bounding-box height reported by <c>search</c> at lookup time.</summary> |
| 46 | + public int Height { get; internal set; } |
| 47 | + |
| 48 | + /// <summary>UIA control type that this wrapper subclass expects (e.g. <c>"Button"</c>). Null = match anything.</summary> |
| 49 | + protected string? TargetControlType { get; set; } |
| 50 | + |
| 51 | + /// <summary>Optional ClassName filter applied alongside <see cref="TargetControlType"/>.</summary> |
| 52 | + protected string? TargetClassName { get; set; } |
| 53 | + |
| 54 | + internal bool MatchesFilter() |
| 55 | + { |
| 56 | + if (TargetControlType is not null && |
| 57 | + !string.Equals(ControlType, TargetControlType, StringComparison.OrdinalIgnoreCase)) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if (TargetClassName is not null && |
| 63 | + !string.Equals(ClassName, TargetClassName, StringComparison.OrdinalIgnoreCase)) |
| 64 | + { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Activate the element. winappcli's <c>invoke</c> tries InvokePattern → TogglePattern → |
| 73 | + /// SelectionItemPattern → ExpandCollapsePattern in order; <c>rightClick</c> falls back to |
| 74 | + /// <c>click --right</c> via real mouse input. |
| 75 | + /// </summary> |
| 76 | + public virtual void Click(bool rightClick = false, int msPostAction = 200) |
| 77 | + { |
| 78 | + EnsureBound(); |
| 79 | + |
| 80 | + if (rightClick) |
| 81 | + { |
| 82 | + WinappCli.InvokeAssertSuccess("ui", "click", Selector, "-w", Owner!.WindowHandleArg, "--right"); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + WinappCli.InvokeAssertSuccess("ui", "invoke", Selector, "-w", Owner!.WindowHandleArg); |
| 87 | + } |
| 88 | + |
| 89 | + if (msPostAction > 0) |
| 90 | + { |
| 91 | + Thread.Sleep(msPostAction); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Mouse-simulation left-click via <c>winapp ui click <slug></c>. Use for elements that |
| 97 | + /// don't expose an InvokePattern (e.g. TextBlocks, ListItems, column headers), where the |
| 98 | + /// click is handled by an ancestor's Click handler rather than by the element itself. |
| 99 | + /// </summary> |
| 100 | + public void MouseClick(int msPostAction = 200) |
| 101 | + { |
| 102 | + EnsureBound(); |
| 103 | + WinappCli.InvokeAssertSuccess("ui", "click", Selector, "-w", Owner!.WindowHandleArg); |
| 104 | + if (msPostAction > 0) |
| 105 | + { |
| 106 | + Thread.Sleep(msPostAction); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary>Move keyboard focus to this element.</summary> |
| 111 | + public void Focus() |
| 112 | + { |
| 113 | + EnsureBound(); |
| 114 | + WinappCli.InvokeAssertSuccess("ui", "focus", Selector, "-w", Owner!.WindowHandleArg); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Read a single UIA property via <c>winapp ui get-property … --json</c>. Returns the raw string |
| 119 | + /// value as winappcli reports it (e.g. <c>"On"</c>/<c>"Off"</c> for <c>ToggleState</c>). |
| 120 | + /// </summary> |
| 121 | + public string GetProperty(string propertyName) |
| 122 | + { |
| 123 | + EnsureBound(); |
| 124 | + var root = WinappCli.InvokeJson("ui", "get-property", Selector, "-p", propertyName, "-w", Owner!.WindowHandleArg, "--json"); |
| 125 | + if (root.TryGetProperty("properties", out var props) && |
| 126 | + props.TryGetProperty(propertyName, out var v)) |
| 127 | + { |
| 128 | + return v.GetString() ?? string.Empty; |
| 129 | + } |
| 130 | + |
| 131 | + return string.Empty; |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// UIA <c>HelpText</c> (from <c>AutomationProperties.HelpText</c>). Used by the Settings UI |
| 136 | + /// ShortcutControl to surface the current shortcut as readable text on the EditButton |
| 137 | + /// (e.g. <c>"Win + Shift + C"</c>). |
| 138 | + /// </summary> |
| 139 | + public string HelpText => GetProperty("HelpText"); |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Read the element's value via <c>winapp ui get-value … --json</c>. winappcli walks |
| 143 | + /// TextPattern → ValuePattern → SelectionPattern → Name to find a value, so this returns |
| 144 | + /// the rendered text content of TextBlocks (e.g. ColorPicker's <c>ColorTextBlock</c> |
| 145 | + /// where <c>AutomationProperties.Name</c> overrides the UIA Name with the color's friendly |
| 146 | + /// name, but the actual <c>Text</c> binding holds the HEX value we want). |
| 147 | + /// </summary> |
| 148 | + public string GetValue() |
| 149 | + { |
| 150 | + EnsureBound(); |
| 151 | + var root = WinappCli.InvokeJson("ui", "get-value", Selector, "-w", Owner!.WindowHandleArg, "--json"); |
| 152 | + if (root.TryGetProperty("text", out var t)) |
| 153 | + { |
| 154 | + return t.GetString() ?? string.Empty; |
| 155 | + } |
| 156 | + |
| 157 | + return string.Empty; |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Wait for this element to reach <paramref name="expectedValue"/> on <paramref name="propertyName"/>. |
| 162 | + /// Mirrors <c>winapp ui wait-for --property X --value Y -t T</c>; returns true on success, false on timeout. |
| 163 | + /// </summary> |
| 164 | + public bool WaitForProperty(string propertyName, string expectedValue, int timeoutMS = 5000) |
| 165 | + { |
| 166 | + EnsureBound(); |
| 167 | + var r = WinappCli.Invoke( |
| 168 | + "ui", "wait-for", Selector, |
| 169 | + "-w", Owner!.WindowHandleArg, |
| 170 | + "--property", propertyName, |
| 171 | + "--value", expectedValue, |
| 172 | + "-t", timeoutMS.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 173 | + return r.ExitCode == 0; |
| 174 | + } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Wait for any element matching the original selector to disappear from the tree |
| 178 | + /// (<c>winapp ui wait-for … --gone</c>). |
| 179 | + /// </summary> |
| 180 | + public bool WaitForGone(int timeoutMS = 5000) |
| 181 | + { |
| 182 | + EnsureBound(); |
| 183 | + var r = WinappCli.Invoke( |
| 184 | + "ui", "wait-for", Selector, |
| 185 | + "-w", Owner!.WindowHandleArg, |
| 186 | + "--gone", |
| 187 | + "-t", timeoutMS.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 188 | + return r.ExitCode == 0; |
| 189 | + } |
| 190 | + |
| 191 | + /// <summary>Find a descendant matching <paramref name="by"/>, scoped under this element via its slug.</summary> |
| 192 | + public T Find<T>(By by, int timeoutMS = 5000) |
| 193 | + where T : Element, new() |
| 194 | + { |
| 195 | + EnsureBound(); |
| 196 | + |
| 197 | + // winappcli scopes a search beneath an element by passing the parent's selector to inspect. |
| 198 | + // For most cases (within the same window) the global search is fine and faster; if you need |
| 199 | + // strict scoping under a subtree, use a slug By that prefixes with the parent's slug. |
| 200 | + return Owner!.FindUnder<T>(by, timeoutMS); |
| 201 | + } |
| 202 | + |
| 203 | + public T Find<T>(string name, int timeoutMS = 5000) |
| 204 | + where T : Element, new() => Find<T>(By.Name(name), timeoutMS); |
| 205 | + |
| 206 | + private void EnsureBound() |
| 207 | + { |
| 208 | + Assert.IsNotNull(Owner, "Element is not bound to a Session."); |
| 209 | + Assert.IsFalse(string.IsNullOrEmpty(Selector), "Element has no selector."); |
| 210 | + } |
| 211 | +} |
0 commit comments