Fix startup config

This commit is contained in:
Gleb Khmyznikov
2025-12-08 18:06:06 -08:00
parent d1fa38b1fe
commit 38173f3ec8
7 changed files with 22 additions and 45 deletions

View File

@@ -130,9 +130,13 @@ namespace Microsoft.PowerToys.UITest
/// </summary>
/// <param name="appPath">The path to the application executable.</param>
/// <param name="args">Optional command line arguments to pass to the application.</param>
public void StartExe(string appPath, string[]? args = null)
public void StartExe(string appPath, string[]? args = null, string? enableModules = null)
{
var opts = new AppiumOptions();
if (!string.IsNullOrEmpty(enableModules))
{
opts.AddAdditionalCapability("enableModules", enableModules);
}
if (scope == PowerToysModule.PowerToysSettings)
{
@@ -169,7 +173,12 @@ namespace Microsoft.PowerToys.UITest
private void TryLaunchPowerToysSettings(AppiumOptions opts)
{
SettingsConfigHelper.ConfigureGlobalModuleSettings("Hosts");
if (opts.ToCapabilities().HasCapability("enableModules"))
{
var modulesString = (string)opts.ToCapabilities().GetCapability("enableModules");
var modulesArray = modulesString.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
SettingsConfigHelper.ConfigureGlobalModuleSettings(modulesArray);
}
const int maxTries = 3;
const int delayMs = 5000;
@@ -179,8 +188,6 @@ namespace Microsoft.PowerToys.UITest
{
try
{
Console.WriteLine($"[TryLaunchPowerToysSettings] Attempt {tryCount}/{maxTries}");
var runnerProcessInfo = new ProcessStartInfo
{
FileName = locationPath + runnerPath,
@@ -188,53 +195,39 @@ namespace Microsoft.PowerToys.UITest
Arguments = "--open-settings",
};
Console.WriteLine($"[TryLaunchPowerToysSettings] Killing existing runner process: {runnerProcessInfo.FileName}");
ExitExe(runnerProcessInfo.FileName);
// Verify process was killed
string exeName = Path.GetFileNameWithoutExtension(runnerProcessInfo.FileName);
var remainingProcesses = Process.GetProcessesByName(exeName);
Console.WriteLine($"[TryLaunchPowerToysSettings] After ExitExe, remaining '{exeName}' processes: {remainingProcesses.Length}");
Console.WriteLine($"[TryLaunchPowerToysSettings] Starting runner process: {runnerProcessInfo.FileName} {runnerProcessInfo.Arguments}");
runner = Process.Start(runnerProcessInfo);
Console.WriteLine($"[TryLaunchPowerToysSettings] Process.Start returned: {(runner != null ? $"PID={runner.Id}" : "null")}");
Console.WriteLine($"[TryLaunchPowerToysSettings] Waiting for 'PowerToys Settings' window...");
if (WaitForWindowAndSetCapability(opts, "PowerToys Settings", delayMs, maxRetries))
{
Console.WriteLine($"[TryLaunchPowerToysSettings] Window found successfully on attempt {tryCount}");
// Exit CmdPal UI before launching new process if use installer for test
ExitExeByName("Microsoft.CmdPal.UI");
return;
}
Console.WriteLine($"[TryLaunchPowerToysSettings] Window not found on attempt {tryCount}");
// Window not found, kill all PowerToys processes and retry
if (tryCount < maxTries)
{
Console.WriteLine($"[TryLaunchPowerToysSettings] Killing all PowerToys processes before retry...");
KillPowerToysProcesses();
}
}
catch (Exception ex)
{
Console.WriteLine($"[TryLaunchPowerToysSettings] Exception on attempt {tryCount}: {ex.Message}");
if (tryCount == maxTries)
{
throw new InvalidOperationException($"Failed to launch PowerToys Settings after {maxTries} attempts: {ex.Message}", ex);
}
// Kill processes and retry
Console.WriteLine($"[TryLaunchPowerToysSettings] Killing all PowerToys processes after exception...");
KillPowerToysProcesses();
}
}
Console.WriteLine($"[TryLaunchPowerToysSettings] All {maxTries} attempts failed");
throw new InvalidOperationException($"Failed to launch PowerToys Settings: Window not found after {maxTries} attempts.");
}
@@ -346,10 +339,10 @@ namespace Microsoft.PowerToys.UITest
/// <summary>
/// Restarts now exe and takes control of it.
/// </summary>
public void RestartScopeExe()
public void RestartScopeExe(string? enableModules = null)
{
ExitScopeExe();
StartExe(locationPath + sessionPath, this.commandLineArgs);
StartExe(locationPath + sessionPath, commandLineArgs, enableModules);
}
public WindowsDriver<WindowsElement> GetRoot()
@@ -379,34 +372,26 @@ namespace Microsoft.PowerToys.UITest
{
var powerToysProcessNames = new[] { "PowerToys", "Microsoft.CmdPal.UI" };
Console.WriteLine($"[KillPowerToysProcesses] Starting to kill PowerToys-related processes...");
foreach (var processName in powerToysProcessNames)
{
try
{
var processes = Process.GetProcessesByName(processName);
Console.WriteLine($"[KillPowerToysProcesses] Found {processes.Length} process(es) with name '{processName}'");
foreach (var process in processes)
{
Console.WriteLine($"[KillPowerToysProcesses] Killing process '{processName}' (PID: {process.Id})...");
process.Kill();
process.WaitForExit();
Console.WriteLine($"[KillPowerToysProcesses] Process '{processName}' (PID: {process.Id}) killed and exited");
}
// Verify processes are actually gone
var remainingProcesses = Process.GetProcessesByName(processName);
Console.WriteLine($"[KillPowerToysProcesses] After killing, remaining '{processName}' processes: {remainingProcesses.Length}");
}
catch (Exception ex)
{
Console.WriteLine($"[KillPowerToysProcesses] Failed to kill process {processName}: {ex.Message}");
}
}
Console.WriteLine($"[KillPowerToysProcesses] Finished killing processes");
}
}
}

View File

@@ -26,14 +26,13 @@ namespace Microsoft.PowerToys.UITest
/// <summary>
/// Configures global PowerToys settings to enable only specified modules and disable all others.
/// </summary>
/// <param name="modulesToEnable">Array of module names to enable (e.g., "Peek", "FancyZones"). All other modules will be disabled.</param>
/// <exception cref="ArgumentNullException">Thrown when modulesToEnable is null.</exception>
/// <param name="modulesToEnable">Array of module names to enable (e.g., "Peek", "FancyZones"). All other modules will be disabled. If null or empty, all modules will be disabled.</param>
/// <exception cref="InvalidOperationException">Thrown when settings file operations fail.</exception>
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "This is test code and will not be trimmed")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "This is test code and will not be AOT compiled")]
public static void ConfigureGlobalModuleSettings(params string[] modulesToEnable)
public static void ConfigureGlobalModuleSettings(params string[]? modulesToEnable)
{
ArgumentNullException.ThrowIfNull(modulesToEnable);
modulesToEnable ??= Array.Empty<string>();
try
{

View File

@@ -778,9 +778,9 @@ namespace Microsoft.PowerToys.UITest
/// <summary>
/// Restart scope exe.
/// </summary>
public void RestartScopeExe()
public void RestartScopeExe(string? enableModules = null)
{
this.sessionHelper!.RestartScopeExe();
this.sessionHelper!.RestartScopeExe(enableModules);
this.Session = new Session(this.sessionHelper.GetRoot(), this.sessionHelper.GetDriver(), this.scope, this.size);
return;
}

View File

@@ -55,10 +55,9 @@ namespace UITests_FancyZones
AppZoneHistory.DeleteFile();
FancyZonesEditorHelper.Files.Restore();
SettingsConfigHelper.ConfigureGlobalModuleSettings("Hosts");
SetupCustomLayouts();
RestartScopeExe();
RestartScopeExe("Hosts");
Thread.Sleep(2000);
// Get the current mouse button setting

View File

@@ -217,8 +217,6 @@ namespace UITests_FancyZones
[TestInitialize]
public void TestInitialize()
{
SettingsConfigHelper.ConfigureGlobalModuleSettings("Hosts");
FancyZonesEditorHelper.Files.Restore();
EditorParameters editorParameters = new EditorParameters();
FancyZonesEditorHelper.Files.ParamsIOHelper.WriteData(editorParameters.Serialize(Parameters));
@@ -273,7 +271,7 @@ namespace UITests_FancyZones
};
FancyZonesEditorHelper.Files.AppliedLayoutsIOHelper.WriteData(appliedLayouts.Serialize(appliedLayoutsWrapper));
this.RestartScopeExe();
RestartScopeExe("Hosts");
}
[TestMethod("FancyZones.Settings.TestApplyHotKey")]

View File

@@ -30,13 +30,11 @@ namespace UITests_FancyZones
[TestInitialize]
public void TestInitialize()
{
SettingsConfigHelper.ConfigureGlobalModuleSettings("Hosts");
// kill all processes related to FancyZones Editor to ensure a clean state
Session.KillAllProcessesByName("PowerToys.FancyZonesEditor");
AppZoneHistory.DeleteFile();
this.RestartScopeExe();
RestartScopeExe("Hosts");
FancyZonesEditorHelper.Files.Restore();
// Set a custom layout with 1 subzones and clear app zone history

View File

@@ -124,9 +124,6 @@ public class PeekFilePreviewTests : UITestBase
settings["properties"] = properties;
});
// Disable all modules except Peek in global settings
SettingsConfigHelper.ConfigureGlobalModuleSettings("Peek");
Debug.WriteLine("Successfully updated all settings - Peek shortcut configured and all modules except Peek disabled");
}
catch (Exception ex)
@@ -138,6 +135,7 @@ public class PeekFilePreviewTests : UITestBase
[TestInitialize]
public void TestInitialize()
{
RestartScopeExe("Peek");
Session.CloseMainWindow();
SendKeys(Key.Win, Key.M);
}