Skip to content

Commit

Permalink
add code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhaopeng Wang (from Dev Box) committed Feb 14, 2025
1 parent 8d334e4 commit 3f5e7d5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/common/UITestAutomation/Element/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace Microsoft.PowerToys.UITest
{
// Class representing a button element in the application
public class Button : Element
{
// Constructor to initialize the button
public Button()
: base()
{
}

// Method to get the type of the button
public string GetButtonType()
{
Assert.IsNotNull(WindowsElement, "WindowsElement should not be null");
Expand Down
10 changes: 10 additions & 0 deletions src/common/UITestAutomation/Element/By.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,56 @@

namespace Microsoft.PowerToys.UITest
{
// This class is a wrapper around OpenQA.Selenium.By to provide a more user-friendly API.
#pragma warning disable SA1649 // File name should match first type name
public class By
{
// The OpenQA.Selenium.By object that this class wraps.
private readonly OpenQA.Selenium.By by;

// Private constructor to prevent instantiation of this class from outside.
private By(OpenQA.Selenium.By by)
{
this.by = by;
}

// Factory method to create a By object using the element's name.
public static By Name(string name)
{
return new By(OpenQA.Selenium.By.Name(name));
}

// Factory method to create a By object using the element's ID.
public static By Id(string id)
{
return new By(OpenQA.Selenium.By.Id(id));
}

// Factory method to create a By object using an XPath expression.
public static By XPath(string xpath)
{
return new By(OpenQA.Selenium.By.XPath(xpath));
}

// Factory method to create a By object using a CSS selector.
public static By CssSelector(string cssSelector)
{
return new By(OpenQA.Selenium.By.CssSelector(cssSelector));
}

// Factory method to create a By object using the link text.
public static By LinkText(string linkText)
{
return new By(OpenQA.Selenium.By.LinkText(linkText));
}

// Factory method to create a By object using the tag name.
public static By TagName(string tagName)
{
return new By(OpenQA.Selenium.By.TagName(tagName));
}

// Method to convert this By object to an OpenQA.Selenium.By object.
public OpenQA.Selenium.By ToSeleniumBy()
{
return by;
Expand Down
21 changes: 21 additions & 0 deletions src/common/UITestAutomation/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,34 @@

namespace Microsoft.PowerToys.UITest
{
// Class representing a UI element in the application
public class Element
{
// Property to hold the Windows element
public WindowsElement? WindowsElement { get; set; }

// Property to hold the Windows driver
private WindowsDriver<WindowsElement>? driver;

// Constructor to initialize the element
public Element()
{
WindowsElement = null;
}

// Method to set the Windows element
public void SetWindowsElement(WindowsElement windowsElement)
{
WindowsElement = windowsElement;
}

// Method to set the session driver
public void SetSession(WindowsDriver<WindowsElement> driver)
{
this.driver = driver;
}

// Method to get the name attribute of the element
public string GetName()
{
if (WindowsElement == null)
Expand All @@ -45,6 +52,7 @@ public string GetName()
return WindowsElement.GetAttribute("Name");
}

// Method to get the text attribute of the element
public string GetText()
{
if (WindowsElement == null)
Expand All @@ -56,6 +64,7 @@ public string GetText()
return WindowsElement.GetAttribute("Value");
}

// Method to get the automation ID of the element
public string GetAutomationId()
{
if (WindowsElement == null)
Expand All @@ -67,6 +76,7 @@ public string GetAutomationId()
return WindowsElement.GetAttribute("AutomationId");
}

// Method to get the class name of the element
public string GetClassName()
{
if (WindowsElement == null)
Expand All @@ -78,6 +88,7 @@ public string GetClassName()
return WindowsElement.GetAttribute("ClassName");
}

// Method to get the help text of the element
public string GetHelpText()
{
if (WindowsElement == null)
Expand All @@ -89,6 +100,7 @@ public string GetHelpText()
return WindowsElement.GetAttribute("HelpText");
}

// Method to check if the element is enabled
public bool IsEnable()
{
if (WindowsElement == null)
Expand All @@ -99,6 +111,7 @@ public bool IsEnable()
return WindowsElement?.GetAttribute("IsEnabled") == "True" ? true : false;
}

// Method to check if the element is selected
public bool IsSelected()
{
if (WindowsElement == null)
Expand All @@ -109,6 +122,7 @@ public bool IsSelected()
return WindowsElement?.GetAttribute("IsSelected") == "True" ? true : false;
}

// Method to click the element
public void Click()
{
var element = WindowsElement;
Expand All @@ -118,6 +132,7 @@ public void Click()
actions.Build().Perform();
}

// Method to right-click the element
public void RightClick()
{
var element = WindowsElement;
Expand All @@ -128,6 +143,7 @@ public void RightClick()
actions.Build().Perform();
}

// Method to click the element if a specific attribute matches a value
public void ClickCheckAttribute(string attributeKey, string attributeValue)
{
var elements = WindowsElement;
Expand All @@ -141,12 +157,14 @@ public void ClickCheckAttribute(string attributeKey, string attributeValue)
}
}

// Method to check if a specific attribute matches a value
public bool CheckAttribute(string attributeKey, string attributeValue)
{
var elements = WindowsElement;
return elements?.GetAttribute(attributeKey) == attributeValue;
}

// Method to find an element by its name
public T FindElementByName<T>(string name)
where T : Element, new()
{
Expand All @@ -157,6 +175,7 @@ public T FindElementByName<T>(string name)
return element;
}

// Method to find an element by its accessibility ID
public T? FindElementByAccessibilityId<T>(string name)
where T : Element, new()
{
Expand All @@ -167,6 +186,7 @@ public T FindElementByName<T>(string name)
return element;
}

// Method to find multiple elements by their name
public ReadOnlyCollection<T>? FindElementsByName<T>(string name)
where T : Element, new()
{
Expand All @@ -189,6 +209,7 @@ public T FindElementByName<T>(string name)
return resReadOnlyCollection;
}

// Method to take a screenshot of the element
public Screenshot? GetScreenShot()
{
if (WindowsElement == null)
Expand Down
6 changes: 6 additions & 0 deletions src/common/UITestAutomation/Element/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@

namespace Microsoft.PowerToys.UITest
{
// Class representing a window in the application
public class Window : Element
{
// Class representing a window in the application
public Window()
: base()
{
}

// Method to check if the window is visible
public bool IsVisible()
{
Assert.IsNotNull(WindowsElement, "WindowsElement should not be null");
return WindowsElement.Displayed;
}

// Method to maximize the window
public Window Maximize()
{
Assert.IsNotNull(WindowsElement, "WindowsElement should not be null");
Expand All @@ -32,6 +36,7 @@ public Window Maximize()
return this;
}

// Method to restore the window to its original size
public Window Restore()
{
Assert.IsNotNull(WindowsElement, "WindowsElement should not be null");
Expand All @@ -40,6 +45,7 @@ public Window Restore()
return this;
}

// Method to minimize the window
public Window Minimize()
{
Assert.IsNotNull(WindowsElement, "WindowsElement should not be null");
Expand Down
10 changes: 10 additions & 0 deletions src/common/UITestAutomation/ModuleConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public enum PowerToysModuleWindow

public class ModuleConfigData
{
// URL for Windows Application Driver
public const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";

// Singleton instance of ModuleConfigData
private static readonly Lazy<ModuleConfigData> MInstance = new Lazy<ModuleConfigData>(() => new ModuleConfigData());

public static ModuleConfigData Instance
Expand All @@ -42,10 +44,13 @@ public static ModuleConfigData Instance
}
}

// Dictionary to hold module window names
public Dictionary<PowerToysModuleWindow, ModuleWindowData> ModuleWindowName { get; private set; }

// Dictionary to hold module paths
private Dictionary<PowerToysModule, string> ModulePath { get; set; }

// Private constructor to initialize module data
private ModuleConfigData()
{
ModuleWindowName = new Dictionary<PowerToysModuleWindow, ModuleWindowData>();
Expand All @@ -60,28 +65,33 @@ private ModuleConfigData()
ModulePath[PowerToysModule.Hosts] = @"\..\..\..\WinUI3Apps\PowerToys.Hosts.exe";
}

// Method to get the path of a module
public string GetModulePath(PowerToysModule scope)
{
return ModulePath[scope];
}

// Method to get the URL of the Windows Application Driver
public string GetWindowsApplicationDriverUrl()
{
return WindowsApplicationDriverUrl;
}

// Method to get the window data of a module
public ModuleWindowData GetModuleWindowData(PowerToysModuleWindow scope)
{
return ModuleWindowName[scope];
}
}

// Struct to hold module window data
public struct ModuleWindowData
{
public string ModuleName { get; set; }

public string WindowName { get; set; }

// Constructor to initialize module window data
public ModuleWindowData(string moduleName, string windowName)
{
ModuleName = moduleName;
Expand Down
12 changes: 11 additions & 1 deletion src/common/UITestAutomation/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@

namespace Microsoft.PowerToys.UITest
{
// Class representing a session for UI testing
public class Session
{
// Property to hold the root driver
private WindowsDriver<WindowsElement> Root { get; set; }

// Property to hold the Windows driver
private WindowsDriver<WindowsElement> WindowsDriver { get; set; }

// Importing user32.dll to set the foreground window
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(nint hWnd);

// Constructor to initialize the session with root and Windows driver
public Session(WindowsDriver<WindowsElement> root, WindowsDriver<WindowsElement> windowsDriver)
{
Root = root;
WindowsDriver = windowsDriver;
}

// Method to find an element by a given selector
public T FindElement<T>(By by)
where T : Element, new()
{
Expand All @@ -38,6 +44,7 @@ public T FindElement<T>(By by)
return NewElement<T>(item);
}

// Method to find an element by its name
public T FindElementByName<T>(string name)
where T : Element, new()
{
Expand All @@ -46,6 +53,7 @@ public T FindElementByName<T>(string name)
return NewElement<T>(item);
}

// Method to find multiple elements by their name
public ReadOnlyCollection<T>? FindElementsByName<T>(string name)
where T : Element, new()
{
Expand All @@ -62,6 +70,7 @@ public T FindElementByName<T>(string name)
return resReadOnlyCollection;
}

// Method to create a new element of type T
private T NewElement<T>(WindowsElement element)
where T : Element, new()
{
Expand All @@ -71,9 +80,10 @@ private T NewElement<T>(WindowsElement element)
return newElement;
}

// Take control of an application that already exists
// Method to take control of an existing application
public Session? AttachSession(PowerToysModuleWindow module)
{
Thread.Sleep(4000);
string windowName = ModuleConfigData.Instance.GetModuleWindowData(module).WindowName;
string appName = ModuleConfigData.Instance.GetModuleWindowData(module).ModuleName;

Expand Down
Loading

1 comment on commit 3f5e7d5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (2)

Fancyzone
MInstance

These words are not needed and should be removed ahk AMPROPERTY AMPROPSETID Breadcrumb CDEF comdef ddf devenum DEVMON DEVSOURCE DGR DIIRFLAG dshow DVH DVHD DVSD DVSL EData ERole fdw FILEINFOSIG Filtergraph Filterx HCERTSTORE IKs iljxck IYUV KSPROPERTY lcb ldx lld LONGLONG LTRB majortype makecab MEDIASUBTYPE mediatype mfplat mic mjpg Msimg msiquery ORAW outpin overlaywindow PAUDIO PINDIR Pnp ppmt previouscamera PROPBAG propvarutil reencode reencoded REFGUID REGFILTER REGFILTERPINS REGPINTYPES regsvr shmem sizeread stl strsafe strutil subquery SYNCMFT TMPVAR vcdl vdi vid VIDCAP VIDEOINFOHEADER vih webcam wistd WVC

To accept these unrecognized words as correct and remove the previously acknowledged and now absent words, you could run the following commands

... in a clone of the [email protected]:microsoft/PowerToys.git repository
on the zhaopengwang/UITestAutomation branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.24/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/13325955098/attempts/1'
If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.