-
Notifications
You must be signed in to change notification settings - Fork 0
test(ui): FlaUI scaffold + Connect-Stream-Disconnect (#531) #532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cptkoolbeenz
wants to merge
9
commits into
main
Choose a base branch
from
test/flaui-scaffold
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3ab2c12
test(ui): add FlaUI scaffold + main-window smoke test (#531)
cptkoolbeenz 9197d29
Code review: clean up before PR
cptkoolbeenz 5163755
Apply Qodo pass 1: address 11 findings on FlaUI UI-test scaffold
cptkoolbeenz a2f9571
Apply Qodo pass 2: address 4 new findings on FlaUI test scaffold
cptkoolbeenz 17ab383
Apply Qodo pass 3: 3 new /improve suggestions on lifecycle helpers
cptkoolbeenz 25afc80
Apply Qodo pass 4: device-missing skip + list-row counting
cptkoolbeenz 004c857
Apply Qodo pass 5: visible-update streaming check + virtualization-sa…
cptkoolbeenz 69f939e
Apply Qodo pass 6: fix baseline race + grid headers + skip flow
cptkoolbeenz acbd357
Apply Qodo pass 7: fail-fast on selector regression in device-appear …
cptkoolbeenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
273 changes: 273 additions & 0 deletions
273
Daqifi.Desktop.Test/UITests/ConnectStreamDisconnectTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,273 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
| using FlaUI.Core; | ||
| using FlaUI.Core.AutomationElements; | ||
| using FlaUI.Core.Conditions; | ||
| using FlaUI.UIA3; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Daqifi.Desktop.Test.UITests; | ||
|
|
||
| /// <summary> | ||
| /// Phase 2 of the FlaUI UI-automation scaffold (issue #531). | ||
| /// | ||
| /// Drives the full connect -> enable-channel -> stream -> disconnect happy path | ||
| /// against a real DAQiFi device. | ||
| /// | ||
| /// Skip behavior is opt-in rather than opt-out. The test self-skips unless the | ||
| /// environment variable <c>DAQIFI_BENCH_DEVICE_AVAILABLE</c> is set to a truthy | ||
| /// value ("1", "true", or "yes", case-insensitive). On a normal CI run with no | ||
| /// bench device wired up, the test reports <c>Inconclusive</c> with a pointer | ||
| /// to issue #531; on the bench machine, set the env var and the test runs. | ||
| /// | ||
| /// Before enabling on the bench: | ||
| /// 1. A DAQiFi Nyquist must be attached via USB (or reachable via Wi-Fi). | ||
| /// 2. The required XAML controls must be annotated with the AutomationIds | ||
| /// referenced below. Each Id has a comment in the XAML pointing back to | ||
| /// this test + issue #531 for traceability. | ||
| /// 3. The desktop app must be built (Phase 1 verifies the launch path). | ||
| /// | ||
| /// Naming convention used for AutomationIds: "Daqifi.<Pane>.<Control>", | ||
| /// e.g. "Daqifi.Connection.AddDeviceButton". Keeping a stable, dotted namespace | ||
| /// makes future selectors greppable in the XAML. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class ConnectStreamDisconnectTests | ||
| { | ||
| private const string DESKTOP_EXE_NAME = "DAQiFi.exe"; | ||
| private const string DESKTOP_PROJECT_NAME = "Daqifi.Desktop"; | ||
|
|
||
| // Env-var gate: the test only runs when this is set to a truthy value on the | ||
| // host. This replaces the previous unconditional [Ignore] attribute so the | ||
| // bench machine can opt in without a code change (PR #531 / Qodo finding #3). | ||
| private const string BENCH_AVAILABLE_ENV_VAR = "DAQIFI_BENCH_DEVICE_AVAILABLE"; | ||
|
|
||
| // AutomationIds that Phase 2 expects to exist in MainWindow / dialogs. | ||
| // None of these are wired up yet; add them in the corresponding XAML with | ||
| // a comment referencing this test + #531 when enabling the test. | ||
| private const string ADD_DEVICE_BUTTON_ID = "Daqifi.Connection.AddDeviceButton"; | ||
| private const string CONNECT_BUTTON_ID = "Daqifi.Connection.ConnectButton"; | ||
| private const string DEVICE_LIST_ID = "Daqifi.Devices.ConnectedList"; | ||
| private const string FIRST_CHANNEL_TOGGLE_ID = "Daqifi.Channels.FirstChannelEnable"; | ||
| private const string START_STREAMING_ID = "Daqifi.Streaming.StartButton"; | ||
| private const string STOP_STREAMING_ID = "Daqifi.Streaming.StopButton"; | ||
| private const string DISCONNECT_BUTTON_ID = "Daqifi.Connection.DisconnectButton"; | ||
| private const string LIVE_GRAPH_ID = "Daqifi.Graph.Live"; | ||
|
|
||
| // ConnectionDialog (a separate top-level MetroWindow) is identified by its | ||
| // window title; see Daqifi.Desktop/View/ConnectionDialog.xaml. | ||
| private const string CONNECTION_DIALOG_TITLE = "CONNECT DEVICE"; | ||
|
|
||
| private static readonly TimeSpan MAIN_WINDOW_TIMEOUT = TimeSpan.FromSeconds(60); | ||
| private static readonly TimeSpan DEVICE_APPEAR_TIMEOUT = TimeSpan.FromSeconds(15); | ||
| private static readonly TimeSpan CONNECTION_DIALOG_TIMEOUT = TimeSpan.FromSeconds(10); | ||
| private static readonly TimeSpan STREAMING_DWELL_TIME = TimeSpan.FromSeconds(3); | ||
| private static readonly TimeSpan SHUTDOWN_GRACE = TimeSpan.FromSeconds(5); | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("UI-Bench")] | ||
| public void ConnectStreamDisconnect_HappyPath() | ||
| { | ||
| if (!IsBenchDeviceAvailable()) | ||
| { | ||
| Assert.Inconclusive( | ||
| "Skipped: no bench device available. Set the environment variable " + | ||
| $"{BENCH_AVAILABLE_ENV_VAR}=1 on the bench machine (with a DAQiFi Nyquist " + | ||
| "attached and XAML AutomationIds wired up) to run this happy-path test. " + | ||
| "See issue #531."); | ||
| return; // unreachable | ||
| } | ||
|
|
||
| var exePath = MainWindowSmokeTests.TryLocateDesktopExe(); | ||
| if (exePath is null) | ||
| { | ||
| Assert.Inconclusive( | ||
| $"Skipped: {DESKTOP_EXE_NAME} was not found. Build the {DESKTOP_PROJECT_NAME} " + | ||
| "project first."); | ||
| return; // unreachable | ||
| } | ||
|
|
||
| Application? app = null; | ||
| try | ||
| { | ||
| app = UIAppLifecycle.LaunchOrInconclusive(exePath!); | ||
|
|
||
| using var automation = new UIA3Automation(); | ||
| var mainWindow = app.GetMainWindow(automation, MAIN_WINDOW_TIMEOUT); | ||
| Assert.IsNotNull(mainWindow, "Main window did not appear."); | ||
| var cf = automation.ConditionFactory; | ||
|
|
||
| // ----- Connect ----- | ||
| // The Add Device button lives on the main window; clicking it opens the | ||
| // separate ConnectionDialog (a MetroWindow) where the Connect button | ||
| // actually lives. | ||
| var addDevice = FindByAutomationId(mainWindow, cf, ADD_DEVICE_BUTTON_ID, | ||
| "Add-device entry point (USB/Serial picker)."); | ||
| addDevice.AsButton().Invoke(); | ||
|
|
||
| var connectionDialog = WaitForTopLevelWindow(app, automation, | ||
| CONNECTION_DIALOG_TITLE, CONNECTION_DIALOG_TIMEOUT, | ||
| "Connection dialog did not appear after invoking Add Device."); | ||
|
|
||
| var connect = FindByAutomationId(connectionDialog, cf, CONNECT_BUTTON_ID, | ||
| "Confirm button on the connection dialog."); | ||
| connect.AsButton().Invoke(); | ||
|
|
||
| // Wait for the connected-devices list to show at least one row. | ||
| var deviceList = FindByAutomationId(mainWindow, cf, DEVICE_LIST_ID, | ||
| "Connected-devices list container."); | ||
| WaitFor(() => deviceList.FindAllChildren().Length > 0, DEVICE_APPEAR_TIMEOUT, | ||
| "Device did not appear in the connected list."); | ||
|
|
||
| // ----- Enable first channel ----- | ||
| var firstChannel = FindByAutomationId(mainWindow, cf, FIRST_CHANNEL_TOGGLE_ID, | ||
| "Enable-toggle on the first analog channel."); | ||
| // Toggle controls in MahApps are typically ToggleButtons; click via Invoke. | ||
| firstChannel.AsToggleButton().Toggle(); | ||
|
|
||
| // ----- Start streaming, dwell, check graph has data ----- | ||
| var start = FindByAutomationId(mainWindow, cf, START_STREAMING_ID, | ||
| "Start-streaming command button."); | ||
| start.AsButton().Invoke(); | ||
|
|
||
| // Capture graph geometry BEFORE the dwell so we can detect that data | ||
| // actually arrived during streaming (the rectangle grows / changes as | ||
| // points are plotted). UI-Automation can't see OxyPlot/LiveCharts data | ||
| // directly, so this is the best UIA-only proxy for "non-zero data" we | ||
| // can do until the XAML grows an automation-visible point-count probe | ||
| // (tracked under #531). | ||
| var liveGraph = FindByAutomationId(mainWindow, cf, LIVE_GRAPH_ID, | ||
| "Live graph control. Should contain non-zero point count after dwell."); | ||
| var preStreamRect = liveGraph.BoundingRectangle; | ||
|
|
||
| Thread.Sleep(STREAMING_DWELL_TIME); | ||
|
|
||
| // Re-fetch in case the surface was lazy-bound on Start. | ||
| liveGraph = FindByAutomationId(mainWindow, cf, LIVE_GRAPH_ID, | ||
| "Live graph control (post-dwell)."); | ||
|
|
||
| Assert.IsFalse(liveGraph.IsOffscreen, "Live graph was offscreen after Start."); | ||
| Assert.IsTrue(liveGraph.BoundingRectangle.Width > 0 | ||
| && liveGraph.BoundingRectangle.Height > 0, | ||
| "Live graph had zero-sized bounding box; streaming likely did not start."); | ||
|
|
||
| // Proxy data-arrival check: enabled-state should remain visible and the | ||
| // graph's rectangle stayed non-empty across the dwell. A stronger | ||
| // assertion needs an AutomationProperties.HelpText (or similar) bound | ||
| // to the live point count - tracked under #531 follow-up. | ||
| Assert.IsTrue(liveGraph.IsEnabled, | ||
| "Live graph was disabled after streaming dwell; streaming likely did not start."); | ||
| Assert.IsTrue(preStreamRect.Width > 0, | ||
| "Pre-stream graph rectangle was empty; the control wasn't laid out before Start."); | ||
|
|
||
| // ----- Stop streaming ----- | ||
| var stop = FindByAutomationId(mainWindow, cf, STOP_STREAMING_ID, | ||
| "Stop-streaming command button."); | ||
| stop.AsButton().Invoke(); | ||
|
|
||
| // ----- Disconnect ----- | ||
| var disconnect = FindByAutomationId(mainWindow, cf, DISCONNECT_BUTTON_ID, | ||
| "Disconnect command button."); | ||
| disconnect.AsButton().Invoke(); | ||
|
|
||
| WaitFor(() => deviceList.FindAllChildren().Length == 0, DEVICE_APPEAR_TIMEOUT, | ||
| "Device was not removed from the connected list after disconnect."); | ||
| } | ||
| finally | ||
| { | ||
| UIAppLifecycle.CloseAppGracefully(app, SHUTDOWN_GRACE); | ||
| } | ||
| } | ||
|
|
||
| private static bool IsBenchDeviceAvailable() | ||
| { | ||
| var raw = Environment.GetEnvironmentVariable(BENCH_AVAILABLE_ENV_VAR); | ||
| if (string.IsNullOrWhiteSpace(raw)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var trimmed = raw.Trim(); | ||
| return trimmed.Equals("1", StringComparison.OrdinalIgnoreCase) | ||
| || trimmed.Equals("true", StringComparison.OrdinalIgnoreCase) | ||
| || trimmed.Equals("yes", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| private static AutomationElement FindByAutomationId( | ||
| AutomationElement scope, ConditionFactory cf, string automationId, string description) | ||
| { | ||
| var element = scope.FindFirstDescendant(cf.ByAutomationId(automationId)); | ||
| Assert.IsNotNull(element, | ||
| $"Could not find AutomationId '{automationId}' ({description}). " + | ||
| "Add AutomationProperties.AutomationId to the XAML and reference issue #531."); | ||
| return element!; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Polls the app's top-level windows for one whose title contains | ||
| /// <paramref name="titleFragment"/> (case-insensitive). The ConnectionDialog | ||
| /// is a separate <c>MetroWindow</c>, so descendants of the main window can't | ||
| /// see it. | ||
| /// </summary> | ||
| private static Window WaitForTopLevelWindow( | ||
| Application app, UIA3Automation automation, string titleFragment, | ||
| TimeSpan timeout, string failureMessage) | ||
| { | ||
| var deadline = DateTime.UtcNow + timeout; | ||
| Exception? lastException = null; | ||
| while (DateTime.UtcNow < deadline) | ||
| { | ||
| try | ||
| { | ||
| var match = app.GetAllTopLevelWindows(automation) | ||
| .FirstOrDefault(w => (w.Title ?? string.Empty) | ||
| .Contains(titleFragment, StringComparison.OrdinalIgnoreCase)); | ||
| if (match is not null) | ||
| { | ||
| return match; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| lastException = ex; | ||
| } | ||
|
|
||
| Thread.Sleep(200); | ||
| } | ||
|
|
||
| var detail = lastException is null | ||
| ? string.Empty | ||
| : $" Last exception while enumerating top-level windows: {lastException.GetType().Name}: {lastException.Message}"; | ||
| Assert.Fail($"{failureMessage} (waited {timeout.TotalSeconds:F0}s).{detail}"); | ||
| throw new InvalidOperationException("unreachable; Assert.Fail throws."); | ||
| } | ||
|
|
||
| private static void WaitFor(Func<bool> condition, TimeSpan timeout, string failureMessage) | ||
| { | ||
| var deadline = DateTime.UtcNow + timeout; | ||
| Exception? lastException = null; | ||
| while (DateTime.UtcNow < deadline) | ||
| { | ||
| try | ||
| { | ||
| if (condition()) return; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Swallow but remember transient UIA errors while controls are still | ||
| // spinning up; if we eventually time out, surface the last error so | ||
| // the failure message points at the real root cause. | ||
| lastException = ex; | ||
| } | ||
| Thread.Sleep(200); | ||
| } | ||
|
|
||
| var detail = lastException is null | ||
| ? string.Empty | ||
| : $" Last exception during polling: {lastException.GetType().Name}: {lastException.Message}"; | ||
| Assert.Fail($"{failureMessage} (waited {timeout.TotalSeconds:F0}s).{detail}"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Reflection; | ||
| using FlaUI.Core; | ||
| using FlaUI.UIA3; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Daqifi.Desktop.Test.UITests; | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Phase 1 of the FlaUI UI-automation scaffold (issue #531). | ||
| /// | ||
| /// Launches the DAQiFi Desktop executable, asserts the main window appears and | ||
| /// has a sensible title, then closes the app cleanly. Requires the WPF app to | ||
| /// already be built; the test is marked Inconclusive (skipped) if the exe is | ||
| /// not on disk so a clean CI run without a built desktop is informative rather | ||
| /// than red. | ||
| /// | ||
| /// This is intentionally minimal: it proves FlaUI can drive the app under | ||
| /// MSTest. Phase 2 builds the connect/stream/disconnect happy path on top of | ||
| /// the helpers established here. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class MainWindowSmokeTests | ||
| { | ||
| // Assembly name is "DAQiFi" per Daqifi.Desktop.csproj <AssemblyName>; the | ||
| // produced exe is therefore DAQiFi.exe under the desktop project's bin dir. | ||
| private const string DESKTOP_EXE_NAME = "DAQiFi.exe"; | ||
| private const string DESKTOP_PROJECT_NAME = "Daqifi.Desktop"; | ||
| private const string EXPECTED_TITLE_FRAGMENT = "DAQiFi"; | ||
|
|
||
| // Allow up to 60s for the main window to appear; WPF cold-start can be slow | ||
| // on first launch (JIT, MEF composition, MahApps theme load). | ||
| private static readonly TimeSpan MAIN_WINDOW_TIMEOUT = TimeSpan.FromSeconds(60); | ||
|
|
||
| // Grace period for graceful shutdown after Close(); matches Phase 2 teardown. | ||
| private static readonly TimeSpan SHUTDOWN_GRACE = TimeSpan.FromSeconds(5); | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("UI")] | ||
| public void MainWindow_Launches_And_HasExpectedTitle() | ||
| { | ||
| var exePath = TryLocateDesktopExe(); | ||
| if (exePath is null) | ||
| { | ||
| Assert.Inconclusive( | ||
| $"Skipped: {DESKTOP_EXE_NAME} was not found. Build the {DESKTOP_PROJECT_NAME} " + | ||
| "project (Debug or Release, net10.0-windows) before running this UI test. " + | ||
| "See issue #531 for the full FlaUI scaffold rollout plan."); | ||
| } | ||
|
|
||
| Application? app = null; | ||
| try | ||
| { | ||
| app = UIAppLifecycle.LaunchOrInconclusive(exePath!); | ||
|
|
||
| using var automation = new UIA3Automation(); | ||
| var mainWindow = app.GetMainWindow(automation, MAIN_WINDOW_TIMEOUT); | ||
|
|
||
| Assert.IsNotNull(mainWindow, "Main window was not found within the timeout."); | ||
|
|
||
| // The MainWindow code-behind sets Title = $"DAQiFi v{Major}.{Minor}.{Build}". | ||
| // Case-insensitive substring match keeps the assertion resilient to | ||
| // version-string changes. | ||
| var title = mainWindow.Title ?? string.Empty; | ||
| Assert.IsTrue( | ||
| title.Contains(EXPECTED_TITLE_FRAGMENT, StringComparison.OrdinalIgnoreCase), | ||
| $"Expected window title to contain '{EXPECTED_TITLE_FRAGMENT}', but was '{title}'."); | ||
| } | ||
| finally | ||
| { | ||
| UIAppLifecycle.CloseAppGracefully(app, SHUTDOWN_GRACE); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tries common build-output locations for DAQiFi.exe. Returns null if none | ||
| /// of them exist; the caller turns that into Assert.Inconclusive. | ||
| /// </summary> | ||
| internal static string? TryLocateDesktopExe() | ||
| { | ||
| // The test binary lands under | ||
| // <repo>/Daqifi.Desktop.Test/bin/<config>/<tfm>/Daqifi.Desktop.Test.dll | ||
| // so the repo root is four levels up. | ||
| var testAssemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) | ||
| ?? Directory.GetCurrentDirectory(); | ||
| var repoRoot = Path.GetFullPath(Path.Combine(testAssemblyDir, "..", "..", "..", "..")); | ||
|
|
||
| // Match the same configuration we were built with, then fall back to | ||
| // any sibling config so a Release test run can still drive a Debug app | ||
| // build (or vice versa). | ||
| #if DEBUG | ||
| var preferredConfigs = new[] { "Debug", "Release" }; | ||
| #else | ||
| var preferredConfigs = new[] { "Release", "Debug" }; | ||
| #endif | ||
|
|
||
| var tfms = new[] { "net10.0-windows", "net9.0-windows" }; | ||
|
|
||
| foreach (var config in preferredConfigs) | ||
| { | ||
| foreach (var tfm in tfms) | ||
| { | ||
| var candidate = Path.Combine( | ||
| repoRoot, DESKTOP_PROJECT_NAME, "bin", config, tfm, DESKTOP_EXE_NAME); | ||
| if (File.Exists(candidate)) | ||
| { | ||
| return candidate; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.