Add retry in attach (#39751)

Co-authored-by: Xiaofeng Wang (from Dev Box) <xiaofengwang@microsoft.com>
This commit is contained in:
XiaofengWang
2025-05-27 17:08:58 +08:00
committed by GitHub
parent f65a4495b0
commit 52d9822fac
2 changed files with 21 additions and 6 deletions

View File

@@ -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

View File

@@ -58,9 +58,6 @@ namespace Hosts.UITests
this.Find<Button>("Launch Hosts File Editor").Click();
// wait for 500 ms to make sure Hosts File Editor is launched
Task.Delay(500).Wait();
this.Session.Attach(PowerToysModule.Hosts, WindowSize.Small_Vertical);
// Should show warning dialog