WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

How to convert Windows Element to a Data Grid

Open KiranBehara opened this issue 7 years ago • 10 comments

I have data grid & able to identify it using session.FindElementByName("Value") but as the return type is WindowsElement am not able to get the row/column count properties from the return variable.

How to do the type casting for windows element or is there any way to get the row/col count for the windows element variable returned from WinAppDrriver server.

WindowsElement recordCount= session.FindElementByName("Value");

KiranBehara avatar Mar 12 '19 14:03 KiranBehara

Unfortunately you cannot cast WindowsElement to System.Windows.Automation.AutomationElement if that is what you want.

As a workaround you can call AutomationElement.FromPoint with screen coordinate of WindowsElement object to get the System.Windows.Automation.AutomationElement (This only works if WinAppDriver and your test app are running on same machine).

If AutomationElement type is GridItem, call TreeWalker to get its parent DataGrid. Once you have AutomationElement for DataGrid, you can get row and column count through GridPattern object.

JunlinZhu avatar Mar 14 '19 04:03 JunlinZhu

Can you please elaborate with few lines of code

KiranBehara avatar Mar 14 '19 08:03 KiranBehara

Add references UIAutomationClient, UIAutomationTypes, and WindowsBase to your C# project for the following code to build. Let me know if this works for you.

var winElement =  [Your code calling WinAppDriver and returning WindowsElement for DataGrid or DataItem];
if (winElement != null)
{
    var pt = new System.Windows.Point(winElement.Location.X, winElement.Location.Y);
    var uia = System.Windows.Automation.AutomationElement.FromPoint(pt);

    while (uia != null && uia.Current.LocalizedControlType.Contains("datagrid") == false)
    {
        uia = System.Windows.Automation.TreeWalker.RawViewWalker.GetParent(uia);
    }

    if (uia != null)
    {
        var gridPattern = uia.GetCurrentPattern(System.Windows.Automation.GridPattern.Pattern) as System.Windows.Automation.GridPattern;

        if (gridPattern != null)
        {
            int rowCount = gridPattern.Current.RowCount;
            int colCount = gridPattern.Current.ColumnCount;
        }
    }
}

JunlinZhu avatar Mar 14 '19 16:03 JunlinZhu

Awesome. Thank You very much. I am getting the row count now. I just started automating desktop applications(WPF) , Will reach out for more support.

KiranBehara avatar Mar 20 '19 13:03 KiranBehara

Am trying to reuse the above method for other data grids/panels to find the row/column count by passing the data grid property & it is returning windows element too, but it is returning row/col count for few data panels & for few it is returning zero because of null value from the while loop.

var winElement = [Your code calling WinAppDriver and returning WindowsElement for DataGrid or DataItem];

while (uia != null && uia.Current.LocalizedControlType.Contains("datagrid") == false) { uia = System.Windows.Automation.TreeWalker.RawViewWalker.GetParent(uia); }

KiranBehara avatar Apr 02 '19 09:04 KiranBehara

Data Grid always contains "DataGridRow" as its child. so once you identify the datagrid find its child elements of class type "DataGridRow". its count will return the Row Count.

//Here is the code var Element = [Your code calling WinAppDriver and returning WindowsElement for DataGrid] try { return Element.FindElementsByClassName("DataGridRow").Count; } catch { return 0; }

karthikmsctcs avatar Dec 03 '19 09:12 karthikmsctcs

I tried to implement the workaround suggested by @JunlinZhu. But the returned element of AutomationElement.FromPoint is always null. The cached properties of the automation element throws Invalid operation exception.

System.Windows.Point point = new System.Windows.Point(winElement.Location.X, winElement.Location.Y); AutomationElement element = AutomationElement.FromPoint(point);

image

image

To solve this issue, I have tried using CacheRequest but it only caches child elements and there is no support for parent and subtree. CacheRequest cacheRequest = new CacheRequest(); cacheRequest.AutomationElementMode = AutomationElementMode.Full; cacheRequest.TreeFilter = Automation.RawViewCondition; cacheRequest.TreeScope = TreeScope.Children;

Please suggest fix for this issue. @KiranBehara @karthikmsctcs Note: I am trying to automate a UWP application using appium with WinAppDriver.

HenryColes avatar Dec 12 '19 11:12 HenryColes

Share the inspect element detail of the datagrid you are automating.

karthikmsctcs avatar Dec 13 '19 03:12 karthikmsctcs

@karthikmsctcs I forgot to mention previously, here i am trying to get the description text of a tile element which is in raw view. Since I am using appium, FindElementBy methods get only the elements which are in controlview/contentview and returns windows element.

In order to traverse through the rawview, i need an automation element. So, I am calling AutomationElement.FromPoint with screen coordinate of tile's title(windows element)

System.Windows.Point point = new System.Windows.Point(winElement.Location.X, winElement.Location.Y); AutomationElement element = AutomationElement.FromPoint(point);

Inspect details of the tile I am trying to automate: Control View: image In the control view, only the image and title is visible.

Raw View: image In the raw view, all the children are visible(including description text)

HenryColes avatar Dec 13 '19 07:12 HenryColes

I think straight forward method of using FindElementsByName("Explore") will get both Explore Button and Explore Text elements. you can iterate through the elements and get the Explore Text element.

karthikmsctcs avatar Dec 16 '19 11:12 karthikmsctcs