WinAppDriver
WinAppDriver copied to clipboard
Simple and fast way to find element by value
In our .Net-application we have a lot of problems when we can find an element in a tree or in a grid only by its value.
Now we have to use something like this:
var treeItems = desktopSession.FindElementsByXPath("//Group/TreeItem/DataItem");
WindowsElement docItem = null;
foreach (var item in treeItems)
{
if (item.Text.Equals("Docs"))
{
docItem = item;
break;
}
}
Finding items in this way is time consuming. Is there an easier and faster way to find an element by value? I need something like in Coded UI:
docItem.SearchProperties[WinCell.PropertyNames.Value] = "Docs";
I don't think there's such a method. You may use LINQ instead of a foreach loop, it will make the code look elegant but behind the scenes LINQ will also use foreach. Maybe there's another control attribute which you could use with the GetAttribute to cobble together a logic. But essentially, this is just it.
I only use WinAppDriver with classic forms so the following may not be applicable Language is C#
public static OpenQA.Selenium.Appium.AppiumWebElement GetTreeNodeControl(OpenQA.Selenium.Appium.AppiumWebElement treeControl,
String strNodeSelector)
{
string strControlType;
OpenQA.Selenium.Appium.AppiumWebElement commandButton;
string nodeSelector;
nodeSelector = "//TreeItem[@Name=\"" + strNodeSelector + "\"]";
try
{
commandButton = treeControl.FindElementByXPath(nodeSelector);
strControlType = commandButton.TagName;
if ("ControlType.TreeItem" != strControlType)
{
Assert.Fail("Control not a Tree item control");
}
}
catch (Exception ex)
{
commandButton = null;
}
return (commandButton);
}
I have deleted a candidate search child treeitems. Probably should have marked it as a candidate.
The root search is valid
Searching for child treeitems may be problematic
for the tree node ---> node ---> node
consider
treeItem1 = GetTreeNodeControl( treeControl, "node"); treeItem2 = GetTreeNodeControl( treeItem1, "node");
not certain whether treeItem2 will be correct It might just be treeeItem1
@naeemakram thank you for answer. Therefore, it should be feature request. I think this case can be popular – to find element with specific value.
@DLightstone In your case, you use xpath and specific value of element’s “Name”. In my case, I don’t have unique “Name” value, only “ValuePattern.Value”.
@testanna I am confused by your reply. Name in my case corresponds to the text of the the node label for the tree item
For which Windows target environment was the application developed? (I have observed build dependencies which prevent proper XML generation for treeview controls) Which Tree View control are you using?
@DLightstone
My tree's properties:
DataItem in this tree:
@testanna Now I am really confused.
I have associated TreeItems with Trees I have associated DataItems with Grids (display of database tables)
The Tree Control which I am using is definitely not the same as the one which you are using. I am using ClassName="WindowsForms10.SysTreeView32.app.0.141b42a_r6_ad1"
I do not know which application you used to prepare the 2 screen images. (Which application?) My guess is that the application you used prepares its display from the XML file that corresponds to the currently displayed screen. (Capture XML, process, filter and display). Debugging efforts are best conducted from the raw XML.
https://github.com/microsoft/WinAppDriver/issues/856 (you will have to modify it slightly it references libraries which only I have) indicates how you can capture the actual XML. . Insert a call to SaveSourceToFile(string window, string strFile) in the test script where you want the screen image captured as XML
If there is a tree present the xml will contain something similar to the text in the file below XML.txt
@DLightstone I guess we use custom controls based on DevExpress, but I'm not sure. I use VisualUIAVerifyNative.exe in screenshots. But I don't really understand how this information can be helpful in this issue. The path for DataItem is somthing like: //Tree//TreeItem/DataItem. Neither TreeItem no DataItem have a unique value of the “Name” property. My entire conditions - I have element that I can find only by "ValuePattern.Value" (not by "Name") and I want to find it faster than in my example.
@testanna
I do not have access to DevExpress, so there is not a lot I can do. Generate the XML, maybe VisualUIAVerifyNative.exe is concealing some useful information
Would this be possible? (with Linq)
var element = desktopSession.FindElementsByAccessibilityId("_tree").Where(e => e.GetAttribute("Value.Value") == [yourText]).FirstOrDefault();