Skip to content

Commit

Permalink
Add Module Enable/Disable API and update window operation function in…
Browse files Browse the repository at this point in the history
… UIManager (#37047)

* 1. Enable/Disable API
2. Fix UIManager
3. Fix FancyZone UI test

* Fix spelling error

---------

Co-authored-by: Xiaofeng Wang (from Dev Box) <[email protected]>
  • Loading branch information
urnotdfs and Xiaofeng Wang (from Dev Box) authored Jan 24, 2025
1 parent 300aa9f commit 9fccd86
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
15 changes: 12 additions & 3 deletions src/UITestAPI/src/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ public void StartApp(string appName, string windowName, string appPath)
winDriver.Session = session;
winDriver.AppName = appName;
winDriver.WindowName = windowName;
mWindowList.Push(winDriver);
if (CurrentDriver.Session != null)
{
mWindowList.Push(CurrentDriver);
}

CurrentDriver = winDriver;
}

// Take control of an application that already exists
public void LuanchApp(string appName, string windowName)
public void LaunchApp(string appName, string windowName)
{
if (Root != null)
{
Expand All @@ -103,7 +107,11 @@ public void LuanchApp(string appName, string windowName)
winDriver.Session = appSession;
winDriver.AppName = appName;
winDriver.WindowName = windowName;
mWindowList.Push(winDriver);
if (CurrentDriver.Session != null)
{
mWindowList.Push(CurrentDriver);
}

CurrentDriver = winDriver;
}
else
Expand Down Expand Up @@ -158,6 +166,7 @@ public void CloseApp(string appName)
}

CurrentDriver = mWindowList.Pop();
return;
}

while (mWindowList.Count > 0)
Expand Down
56 changes: 54 additions & 2 deletions src/UITestAPI/src/UITestAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public void StartApp(string appName, string windowName, string appPath)
}

// Take control of an application that already exists
public void LuanchApp(string appName, string windowName)
public void LaunchApp(string appName, string windowName)
{
UIManager.LuanchApp(appName, windowName);
UIManager.LaunchApp(appName, windowName);
}

// Use the name to switch the current driver
Expand Down Expand Up @@ -182,6 +182,58 @@ public void Click_Element(string elementName, string helpText, string? appName =
Assert.IsTrue(buttonClicked, $"No button with elementName '{elementName}' and HelpText '{helpText}' was found.");
}

public void Enable_Module_from_Dashboard(string moduleName, string? appName = null)
{
WindowsDriver<WindowsElement>? session = GetSession(appName);
var elements = GetElements("Enable module");
Actions actions = new Actions(session);
bool buttonFound = false;
foreach (var element in elements)
{
if (element.GetAttribute("HelpText") == moduleName)
{
if (element.GetAttribute("Toggle.ToggleState") == "0")
{
actions.MoveToElement(element);
actions.Click();
actions.Build().Perform();
actions.MoveByOffset(5, 5);
}

buttonFound = true;
break;
}
}

Assert.IsTrue(buttonFound, $"No button with elementName '{moduleName}' and HelpText '{moduleName}' was found.");
}

public void Disable_Module_from_Dashboard(string moduleName, string? appName = null)
{
WindowsDriver<WindowsElement>? session = GetSession(appName);
var elements = GetElements("Enable module");
Actions actions = new Actions(session);
bool buttonFound = false;
foreach (var element in elements)
{
if (element.GetAttribute("HelpText") == moduleName)
{
if (element.GetAttribute("Toggle.ToggleState") == "1")
{
actions.MoveToElement(element);
actions.Click();
actions.Build().Perform();
actions.MoveByOffset(5, 5);
}

buttonFound = true;
break;
}
}

Assert.IsTrue(buttonFound, $"No button with elementName '{moduleName}' and HelpText '{moduleName}' was found.");
}

public void RightClick_Element(string elementName, string? appName = null)
{
WindowsDriver<WindowsElement>? session = GetSession(appName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Microsoft.FancyZones.UnitTests.Utils;
using Microsoft.UITests.API;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -86,8 +87,10 @@ public void RunFancyZones()
}

Assert.IsNotNull(mUITestAPI);
mUITestAPI.Click_Element("Launch layout editor");

mUITestAPI.LuanchApp("PowerToys.FancyZonesEditor", "FancyZones Editor");
Thread.Sleep(5000);
mUITestAPI.LaunchApp("PowerToys.FancyZonesEditor", "FancyZones Layout");
mUITestAPI?.Click_CreateNewLayout();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.UITests.API;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -14,7 +15,7 @@ namespace UITests_KeyboardManager
public class RunKeyboardManagerUITests
{
private const string PowerToysSettingsPath = @"\..\..\..\WinUI3Apps\PowerToys.Settings.exe";
private static UITestAPI? _uITestAPI;
private static UITestAPI? mUITestAPI;

private static TestContext? _context;

Expand All @@ -33,17 +34,17 @@ public static void CleanupAll()
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
_uITestAPI = new UITestAPI();
_uITestAPI.Init("PowerToys.Settings", PowerToysSettingsPath, "PowerToys.Settings");
mUITestAPI = new UITestAPI();
mUITestAPI.Init("PowerToys.Settings", PowerToysSettingsPath, "PowerToys.Settings");
Debug.WriteLine("ClassInitialize executed");
}

[ClassCleanup]
public static void ClassCleanup()
{
if (_uITestAPI != null && _context != null)
if (mUITestAPI != null && _context != null)
{
_uITestAPI.Close(_context);
mUITestAPI.Close(_context);
}

_context = null;
Expand All @@ -66,8 +67,16 @@ public void TestCleanup()
public void EnableKeyboardManager() // verify the session is initialized
{
Debug.WriteLine("Test method executed");
Assert.IsNotNull(_uITestAPI);
_uITestAPI.Click_Element("Enable module", "Keyboard Manager");
Assert.IsNotNull(mUITestAPI);

mUITestAPI.Enable_Module_from_Dashboard("Keyboard Manager");
mUITestAPI.Click_Element("Remap a key");
Thread.Sleep(5000);
mUITestAPI.LaunchApp("PowerToys.KeyboardManagerEditor", "Remap keys");
mUITestAPI.Click_Element("Add key remapping");
mUITestAPI.Click_Element("Cancel");
mUITestAPI.CloseApp("PowerToys.KeyboardManagerEditor");
mUITestAPI.Disable_Module_from_Dashboard("Keyboard Manager");
}
}
}

0 comments on commit 9fccd86

Please sign in to comment.