https://github.com/tobyqin/wpath
WPath is a library to select Windows UI automatin element like XPath.
https://github.com/tobyqin/wpath
uia uiautomation xpath
Last synced: about 2 months ago
JSON representation
WPath is a library to select Windows UI automatin element like XPath.
- Host: GitHub
- URL: https://github.com/tobyqin/wpath
- Owner: tobyqin
- License: mit
- Created: 2016-05-24T13:55:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-09T06:45:19.000Z (over 8 years ago)
- Last Synced: 2025-05-07T17:12:27.458Z (5 months ago)
- Topics: uia, uiautomation, xpath
- Language: C#
- Size: 79.1 KB
- Stars: 27
- Watchers: 2
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/WPath)
# WPath Introduction
WPath is a library to select Windows UI automation element like XPath. The WPath looks like [XPath][1] which was wildly using to select xml elements, but it is not exactly equal to [XPath][1], it is being used to locate [Microsoft UIAutomation][2] elements. Some special rules list below.### Installation
You can clone the repository and compile source code by yourself. Or just install via nuget packages.
```
PM> Install-Package WPath
```### Get Started
1. The path should be started with '/'.
2. Use '/' to separate element nodes in a path.
3. The node name should be [control type][3] name, but it's optional.
4. The UI element properties are treated as attribute in XPath.
5. Supported attributes:
+ `Name` (NameProperty)
+ `ID` (AutomationIdProperty)
+ `Class` (ClassNameProperty)
+ `Enabled` (IsEnabledProperty)
+ `FrameworkID` (FrameworkIdProperty)### Examples:
> `/Group/Button`
+ Find the first button under first group element.> `//Button[@Name='Save']`
+ Find a button with name "Save" in descendants.> `/[@Name='TabContainer']/Button[2]`
+ Find the second button under an element named with 'TabContainer'> `/Button[@ID='AddButton' and @Name='Add']`
+ Find a button with automation ID 'AddButton' **and** name 'Add'> `/Button[@ID='AddButton' or @Name='Add']`
+ Find a button with automation ID 'AddButton' **or** name 'Add'> `/Button[first()]`
+ Find the first button under current node> `/Button[last()]`
+ Find the last button under current node### Usage
1. Set WPath by attribute, works for C# function and property member.
```cs
[WPath("/Edit[@id='txtId' or @Class='TextBox']")]
public AutomationElement EditControl
{
get { return this.AppElement.FindByWPath(); }
}[WPath("/Button[first()]")]
public AutomationElement GetFirstButton()
{
return this.AppElement.FindByWPath();
}
```2. Call `FindByWPath(path)` method to locate the element
```cs
var path = "/Edit[3]";
var e = this.AppElement.FindByWPath(path);
Assert.AreEqual("txtKey", e.Current.AutomationId);
Assert.AreEqual(ControlType.Edit, e.Current.ControlType);path = "/Button[@name='OK']/Text[1]";
e = this.AppElement.FindByWPath(path);
Assert.AreEqual("OK", e.Current.Name);
Assert.AreEqual(ControlType.Text, e.Current.ControlType);
```### Tips:
- The node name and attribute name is case insensitive
- @name = @Name
- /edit = /Edit
- Welcome to extend the feature by sending pull requests.
- Parent element locator `../` is **not** support yet.
- For more usage please refer to [unit test examples][4].[1]: http://www.w3schools.com/xsl/xpath_intro.asp
[2]: https://msdn.microsoft.com/en-us/library/ms747327(v=vs.110).aspx
[3]: https://msdn.microsoft.com/en-us/library/ms743581(v=vs.110).aspx
[4]: https://github.com/tobyqin/wpath/blob/master/WPath.Tests/UnitTests.cs