Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagrid contents with UI Automation and .net

I'm having some trouble reading the contents of a Datagrid in an external application using UI Automation and could use some pointers. Here's what I have so far:

int id = System.Diagnostics.Process.GetProcessesByName("Book")[0].Id;
AutomationElement desktop = AutomationElement.RootElement;

AutomationElement bw = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, id));

AutomationElement datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "lv"));

AutomationElementCollection lines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));

AutomationElementCollection items = lines[1].FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

GridItemPattern pattern = items[1].GetCurrentPattern(GridItemPattern.Pattern) as GridItemPattern;
TableItemPattern tablePattern = items[1].GetCurrentPattern(TableItemPattern.Pattern) as TableItemPattern;

This works in as much as I can access the column ids and row ids from the GridItemPattern and TableItemPattern but how do I access the value that is in that specific cell? Is it even possible?

Thanks.

like image 268
ChrisO Avatar asked Dec 17 '11 19:12

ChrisO


2 Answers

I think you need to use ValuePattern for it. Just like that:

ValuePattern pattern = items[0].GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
string value = pattern.Current.Value;
like image 138
Natalia Avatar answered Nov 02 '22 16:11

Natalia


I finally figured this out, it requires the use of CacheRequest to request the Name property on the AutomationElement. Here's the final code:

var cacheRequest = new CacheRequest
{
    AutomationElementMode = AutomationElementMode.None,
    TreeFilter = Automation.RawViewCondition
};

cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(AutomationElement.AutomationIdProperty);

cacheRequest.Push();

var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));

cacheRequest.Pop();

var myString = targetText.Cached.Name;
like image 1
ChrisO Avatar answered Nov 02 '22 14:11

ChrisO