25 Oct Automating Windows Applications with WinAppDriver
When automating a Windows application, IDs are the preferred approach to find a reliable locator. These map to the Accessibility ID/Automation ID using WinAppDriver and the Inspect tool, however you can also use Name and XPath. Whenever selecting a locator on screen using the inspector always look for an Automation ID (Accessibility ID) first and check it isn’t a generated value (e.g. lots of numbers). If you can’t find the Automation ID you should go for a unique Name and finally XPath can be used if the first two are not available.
Many Windows applications have multiple small windows or panes. Each pane can contain elements that also appear on other panes, e.g. Ok Buttons, and thus you might have several elements with the same ID/Name but on different panes.
In order to make sure we find the correct element in our tests we write Page Objects that contain elements with parent window/pane locator and the child elements within those panes. To access a sub-pane or a particular element of that screen, the child element definition also contains the parent locator. Below is an example of how we use the parent/child locators.
public WinElement pane = new WinElement(LocatorType.ACCESSIBILITYID, “CGSMainAdvancedForm”);
public WinElement groupNameField = new WinElement(LocatorType.ACCESSIBILITYID, “txtGroupName”, pane);
public WinElement startTimeFirstRow = new WinElement(LocatorType.NAME, “Start Time row 0”, pane);
Occasionally we also need to find an element within an element on a pane (e.g. in a table) so you can use any element to be the parent of another, however we would only do this if the element we are trying to find was duplicated within the pane or couldn’t be found from the pane directly as it would be slower than trying to find it directly under the parent pane tree.
Difference in defining Elements between Windows app and Web app
The main difference between Windows apps and Web apps using Selenium is how the elements are being defined. In Windows applications it is time-consuming to go through all elements as WinAppDriver is a slower than a browser driver and there may be more elements if many windows/panes are open, plus ids are more commonly reused and less likely to be unique. This makes searching from the root of the element tree slower and also more likely to retrieve the wrong element. By defining the pane and then only searching within that, you will get a faster and more accurate result.
In web apps you are not usually required to get the parent locator to access any child elements or sub-panes because it is much faster to find elements and they tend to have unique names and ids most of the time. You also have more options for web locators which are as follows:- ID, Name, Xpath, ClassName, TagName, LinkText or CSS selector.