diff --git a/src/common/UITestAutomation/Session.cs b/src/common/UITestAutomation/Session.cs index 8189941ff1..9ba6ac6363 100644 --- a/src/common/UITestAutomation/Session.cs +++ b/src/common/UITestAutomation/Session.cs @@ -480,10 +480,28 @@ namespace Microsoft.PowerToys.UITest if (this.Root != null) { // search window handler by window title (admin and non-admin titles) - var matchingWindows = WindowHelper.ApiHelper.FindDesktopWindowHandler([windowName, WindowHelper.AdministratorPrefix + windowName]); - if (matchingWindows.Count == 0 || matchingWindows[0].HWnd == IntPtr.Zero) + var timeout = TimeSpan.FromMinutes(2); + var retryInterval = TimeSpan.FromSeconds(5); + DateTime startTime = DateTime.Now; + + List<(IntPtr HWnd, string Title)>? matchingWindows = null; + + while (DateTime.Now - startTime < timeout) { - Assert.Fail($"Failed to attach. Window '{windowName}' not found"); +    matchingWindows = WindowHelper.ApiHelper.FindDesktopWindowHandler( +        new[] { windowName, WindowHelper.AdministratorPrefix + windowName }); + +    if (matchingWindows.Count > 0 && matchingWindows[0].HWnd != IntPtr.Zero) +    { +        break; +    } + +    Thread.Sleep(retryInterval); + } + + if (matchingWindows == null || matchingWindows.Count == 0 || matchingWindows[0].HWnd == IntPtr.Zero) + { +    Assert.Fail($"Failed to attach. Window '{windowName}' not found after {timeout.TotalSeconds} seconds."); } // pick one from matching windows diff --git a/src/modules/Hosts/Hosts.UITests/HostsSettingTests.cs b/src/modules/Hosts/Hosts.UITests/HostsSettingTests.cs index e887143c26..d6e561f8a2 100644 --- a/src/modules/Hosts/Hosts.UITests/HostsSettingTests.cs +++ b/src/modules/Hosts/Hosts.UITests/HostsSettingTests.cs @@ -58,9 +58,6 @@ namespace Hosts.UITests this.Find