Question

Few days ago I received a requirement to create automated UI test cases for my Windows Application (framework 2.0).

I decided to use White as the testing UI framework. Now everything works great except I cannot seem to find the DataGrid control which is displaying 3 records (note: this is not DataGridView) using the White framework

I have used VisualUIAVerify application to validate that indeed the DataGrid is on the form and it is of UI Item Type “Table” and I am defineltly using the correct AutomationId for the control but still no luck.

As mentioned before I can find all the controls on the form except DataGrid. Am I doing some thing wrong ? Or is it that white simply does not support DataGrid.

Any help will be great. Thanks

Bobby

Was it helpful?

Solution

At the end had to upgrade my application to use DataGridView control rather then using DataGrid. This seemed to fix the problem as White does not seem to support DataGrid

OTHER TIPS

I needed access to a dataGrid from White and haven't figured out why White won't work (I have the source and if I have time and dig through it) however, I have written some basic code to extract the grid data into an array. Thankfully the White framework provides access to the AutomationElement.

The code below is not optimised... it was knocked together in LinqPad!

// The first few lines use White
var application = Application.Attach("AppName");
var window = application.GetWindow("The Window Title");
var datagrid = window.Get<White.Core.UIItems.TableItems.Table>("dataGridAutomationId").AutomationElement;

// Now it's using UI Automation
var headerLine = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Header));
var cacheRequest = new CacheRequest { AutomationElementMode = AutomationElementMode.Full, TreeScope = TreeScope.Children };
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.Add(ValuePattern.Pattern);
cacheRequest.Push();
var gridLines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));
cacheRequest.Pop();

Console.WriteLine (headerLine.Count + " columns");
Console.WriteLine (gridLines.Count + " rows");

var gridData = new string[headerLine.Count, gridLines.Count];

var headerIndex = 0;
foreach (AutomationElement header in headerLine)
{
  gridData[headerIndex++, 0] = header.Current.Name;
}

var rowIndex = 1;
foreach (AutomationElement row in gridLines)
{
  foreach (AutomationElement col in row.CachedChildren)
  {
    // Marry up data with headers (for some reason the orders were different
    // when viewing in something like UISpy so this makes sure it's correct
    headerIndex = 0;
    for (headerIndex = 0; headerIndex < headerLine.Count; headerIndex++)
    {
      if (gridData[headerIndex, 0] == col.Cached.Name)
        break;
    }

    gridData[headerIndex, rowIndex] = (col.GetCachedPattern(ValuePattern.Pattern) as ValuePattern).Current.Value;
  }
  rowIndex++;
}

I'm not sure if you have exactly the same problem as me because I don't have enough of your code, but I was struggling with this same thing with a WPF application where I was trying to access a DataGrid which was actually written as a GridView item that lived inside a ListView.

The solution to my problem was to tell White to get a ListView item (i.e. TestStack.White.UIItems.ListView) rather than a Table, then it all worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top