Compartilhar via


WPF TestApi and DataGrid Sample

Our WPF Test team has just released a library of tools that can be leveraged to test WPF applications. What's really cool is that these are utilities that we use internally and now we can push them out to external customers for feedback and usage. The library currently lives here on codeplex, https://www.codeplex.com/TestApi. There are a couple more blog posts where you can get more information about it.

https://blogs.msdn.com/llobo/archive/2008/12/08/wpf-test-helper-library.aspx

https://blogs.msdn.com/wpfsdk/archive/2008/12/09/testapi-released.aspx

 The download contains some samples but I thought I would show a sample with one of the DataGrid applications instead.

I made some small modifications to the SampleTestApp to get it working with one of my DataGrid samples. In this sample, I am doing a visual verification test to check that the rows and cells update to the correct visual state when in edit mode. I am using the in-proc, out of thread strategy which is similar to the strategy used in the visual verification in the SampleTestApp.

To get the tests to work with the DataGridSample application, I needed to add automation id’s to some of the elements. In particular, the Window and the DataGrid:

<Window x:Class="DataGridSample.DataGridBasicSample"

   AutomationProperties.AutomationId="DataGridBasicSample">

  <dg:DataGrid x:Name="DataGrid_Standard"

               AutomationProperties.AutomationId="DataGrid_Standard">

  </dg:DataGrid>

</Window>

 

These IDs will be queried by my test in order to find the correct AutomationElements. And here is what the test will basically look like (left out logging and some error checking to make it more concise):               

// retrieve the necessary AutomationElements         

AutomationElement window = AutomationUtilities.FindElementsById(AutomationElement.RootElement, "DataGridBasicSample")[0];

AutomationElement dataGrid = AutomationUtilities.FindElementsById(window, "DataGrid_Standard")[0];

GridPattern grid = dataGrid.GetCurrentPattern(GridPattern.Pattern) as GridPattern;

AutomationElement cell = grid.GetItem(0, 0);

// do action for making cell editable

AutomationHelpers.MoveToAndDoubleClick(cell);

// Capture the actual pixels from the bounds of the screen rectangle

Snapshot actual = Snapshot.FromRectangle(AutomationHelpers.GetElementSize(dataGrid));

// Load the reference/master data from a previously saved file

Snapshot master = Snapshot.FromFile(Path.Combine(TestContext.TestDeploymentDir, "Master_EditTest1.png"));

if (CompareImages(actual, master) == VerificationResult.Fail)

{

  Assert.Fail("Initial State test failed. Actual should look like Master image. Refer to logged images under:" + TestContext.TestLogsDir);

}

 

Download the full sample here. We would love to hear your feedback.

DataGrid_V1_Sample_With_UnitTesting.zip

Comments