Skip to content

Commit

Permalink
Optimized code format
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 3f5e7d5 commit 785086a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 188 deletions.
38 changes: 9 additions & 29 deletions src/common/UITestAutomation/Element/By.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using OpenQA.Selenium;
using System.Xml.Linq;
using static OpenQA.Selenium.By;

namespace Microsoft.PowerToys.UITest
{
Expand All @@ -20,45 +21,24 @@ private By(OpenQA.Selenium.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));
}
public static By Name(string name) => 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));
}
public static By Id(string id) => 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));
}
public static By XPath(string xpath) => 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));
}
public static By CssSelector(string cssSelector) => 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));
}
public static By LinkText(string linkText) => 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));
}
public static By TagName(string tagName) => 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;
}
public OpenQA.Selenium.By ToSeleniumBy() => by;
}
}
198 changes: 55 additions & 143 deletions src/common/UITestAutomation/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
Expand All @@ -11,6 +12,8 @@
using OpenQA.Selenium.Support.Events;
using static Microsoft.PowerToys.UITest.UITestBase;

[assembly: InternalsVisibleTo("Session")]

namespace Microsoft.PowerToys.UITest
{
// Class representing a UI element in the application
Expand All @@ -23,202 +26,111 @@ public class Element
private WindowsDriver<WindowsElement>? driver;

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

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

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

// Method to get the name attribute of the element
public string GetName()
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
return " ";
}

return WindowsElement.GetAttribute("Name");
}
public string GetName() => GetAttribute("Name");

// Method to get the text attribute of the element
public string GetText()
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
return " ";
}

return WindowsElement.GetAttribute("Value");
}
public string GetText() => GetAttribute("Value");

// Method to get the automation ID of the element
public string GetAutomationId()
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
return " ";
}

return WindowsElement.GetAttribute("AutomationId");
}
public string GetAutomationId() => GetAttribute("AutomationId");

// Method to get the class name of the element
public string GetClassName()
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
return " ";
}

return WindowsElement.GetAttribute("ClassName");
}
public string GetClassName() => GetAttribute("ClassName");

// Method to get the help text of the element
public string GetHelpText()
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
return " ";
}

return WindowsElement.GetAttribute("HelpText");
}
public string GetHelpText() => GetAttribute("HelpText");

// Method to check if the element is enabled
public bool IsEnable()
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
}

return WindowsElement?.GetAttribute("IsEnabled") == "True" ? true : false;
}
public bool IsEnable() => GetAttribute("IsEnabled") == "True";

// Method to check if the element is selected
public bool IsSelected()
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
}

return WindowsElement?.GetAttribute("IsSelected") == "True" ? true : false;
}
public bool IsSelected() => GetAttribute("IsSelected") == "True";

// Method to click the element
public void Click()
{
var element = WindowsElement;
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Click();
actions.Build().Perform();
}
public void Click() => PerformAction(actions => actions.Click());

// Method to right-click the element
public void RightClick()
{
var element = WindowsElement;
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.MoveByOffset(5, 5);
actions.ContextClick();
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;
Actions actions = new Actions(driver);
if (elements?.GetAttribute(attributeKey) == attributeValue)
{
actions.MoveToElement(elements);
actions.Click();
actions.Build().Perform();
actions.MoveByOffset(5, 5);
}
}
public void RightClick() => PerformAction(actions => actions.ContextClick());

// 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;
}
public bool CheckAttribute(string attributeKey, string attributeValue) => GetAttribute(attributeKey) == attributeValue;

// Method to find an element by its name
public T FindElementByName<T>(string name)
where T : Element, new()
{
var item = WindowsElement?.FindElementByName(name) as WindowsElement;
Assert.IsNotNull(item, "Can`t find this element");
T element = new T();
element.SetWindowsElement(item);
return element;
Assert.IsNotNull(item, "Can't find this element");
return NewElement<T>(item);
}

// Method to find an element by its accessibility ID
public T? FindElementByAccessibilityId<T>(string name)
where T : Element, new()
{
var item = WindowsElement?.FindElementByAccessibilityId(name) as WindowsElement;
Assert.IsNotNull(item, "Can`t find this element");
T element = new T();
element.SetWindowsElement(item);
return element;
Assert.IsNotNull(item, "Can't find this element");
return NewElement<T>(item);
}

// Method to find multiple elements by their name
public ReadOnlyCollection<T>? FindElementsByName<T>(string name)
where T : Element, new()
{
var items = WindowsElement?.FindElementsByName(name);
Assert.IsNotNull(items, "Can`t find this element");
List<T> res = new List<T>();
foreach (var item in items)
Assert.IsNotNull(items, "Can't find this element");
var res = items.Select(item =>
{
T element = new T();
var itemTemp = item as WindowsElement;
if (itemTemp != null)
var element = item as WindowsElement;
if (element != null)
{
element.SetWindowsElement(itemTemp);
NewElement<T>(element);
}

res.Add(element);
}

var resReadOnlyCollection = new ReadOnlyCollection<T>(res);
return resReadOnlyCollection;
return element;
}).ToList();
return res == null ? null : new ReadOnlyCollection<T>((IList<T>)res);
}

// Method to take a screenshot of the element
public Screenshot? GetScreenShot()
public Screenshot? GetScreenShot() => WindowsElement?.GetScreenshot();

// Helper method to get an attribute of the element
private string GetAttribute(string attributeName)
{
if (WindowsElement == null)
{
Assert.IsNotNull(null);
return null;
}
Assert.IsNotNull(WindowsElement, $"{attributeName} should not be null");
return WindowsElement?.GetAttribute(attributeName) ?? string.Empty;
}

return WindowsElement?.GetScreenshot();
// Helper method to perform an action on the element
private void PerformAction(Action<Actions> action)
{
var element = WindowsElement;
Actions actions = new Actions(driver);
actions.MoveToElement(element);
action(actions);
actions.Build().Perform();
}

// Method to create a new element of type T
private T NewElement<T>(WindowsElement element)
where T : Element, new()
{
T newElement = new T();
Assert.IsNotNull(driver, "[Element.cs] driver is null");
newElement.SetSession(driver);
Assert.IsNotNull(element, "[Element.cs] element is null");
newElement.SetWindowsElement(element);
return newElement;
}
}
}
13 changes: 0 additions & 13 deletions src/common/UITestAutomation/Element/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,23 @@ public Window()
{
}

// 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");
Assert.IsTrue(IsVisible(), "Window is not visible");
FindElementByName<Button>("Maximize").Click();
return this;
}

// Method to restore the window to its original size
public Window Restore()
{
Assert.IsNotNull(WindowsElement, "WindowsElement should not be null");
Assert.IsTrue(IsVisible(), "Window is not visible");
FindElementByName<Button>("Restore").Click();
return this;
}

// Method to minimize the window
public Window Minimize()
{
Assert.IsNotNull(WindowsElement, "WindowsElement should not be null");
Assert.IsTrue(IsVisible(), "Window is not visible");
FindElementByName<Button>("Minimize").Click();
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/UITestAutomation/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private T NewElement<T>(WindowsElement element)
}

// Method to take control of an existing application
public Session? AttachSession(PowerToysModuleWindow module)
public Session? Attach(PowerToysModuleWindow module)
{
Thread.Sleep(4000);
string windowName = ModuleConfigData.Instance.GetModuleWindowData(module).WindowName;
Expand Down
2 changes: 1 addition & 1 deletion src/common/UITestAutomation/UITestAutomation.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RunFancyZonesTest : UITestBase
public void RunFancyZones()
{
Session.FindElementByName<Button>("Launch layout editor").Click();
Session.AttachSession(PowerToysModuleWindow.FancyZone);
Session.Attach(PowerToysModuleWindow.FancyZone);
Session.FindElementByName<Button>("Create new layout").Click();
}
}
Expand Down

1 comment on commit 785086a

@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/13326824025/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.