UI Automation Testing with TestStack.White

TestStack.White is based on the UI Automation libraries (see UI Automation), offering a simplification of such methods for automating a UI and allowing us to write unit tests against such UI automation.

Getting Started

Let’s jump straight in and write a simply UI automation unit test around the Calc.exe application.

  • Create a new C# Unit Test project (or class library, adding your favoured unit testing framework)
  • Install the TestStack.White nuget package

Let’s begin by creating a simple test method which starts the Calc.exe application, get’s access to the calculator window and then disposes of it, we’ll obviously insert code into this test to do something of value soon, but for now, here’s the basics

[TestMethod]
public void TestMethod1()
{
   using(var application = Application.Launch("Calc.exe"))
   {
      var calculator = application.GetWindow("Calculator", InitializeOption.NoCache);

      // do something with the application

      application.Close();
   }
}

Well that doesn’t do anything too exciting, it runs Calc.exe and then closes it, but now we can start interacting with an instance of the calculator’s UI using TestStack.White.

Let’s start by getting the button with the number 7 and click/press it.

var b7 = calculator.Get<Button>(SearchCriteria.ByText("7"));
b7.Click();

By using the Get method with the generic parameter Button, we get back a button object which we can interact directly with. The SearchCriteria allows us to try to find UI control in the Calculator with the text (in this case) 7. As is probably quite obvious, we call the Click method on this button object to simulate a button click event.

We can’t always get as controls by their text so using Spy++ and using the cross-hair/find window tool we can find the “Control ID” (which is in hex.) and we can instead find a control via this id (White calls this the automation id) hence

var plus = calculator.Get<Button>(
       SearchCriteria.ByAutomationId(
           0x5D.ToString()));
plus.Click();

So let’s look at a completed and very simply unit test to see that we can add two numbers and the output (on the screen) is expected

var b7 = calculator.Get<Button>(
   SearchCriteria.ByText("7"));
b7.Click();

var plus = calculator.Get<Button>(
   SearchCriteria.ByAutomationId(
      0x5D.ToString()));
plus.Click();

var b3 = calculator.Get<Button>(
   SearchCriteria.ByText("5"));
b3.Click();

var eq = calculator.Get<Button>(
   SearchCriteria.ByAutomationId(
      0x79.ToString()));
eq.Click();

var a = calculator.Get(
   SearchCriteria.ByAutomationId(
      0x96.ToString()));

var r = a.Name;
Assert.AreEqual("12", r);

Managed applications

In the above example we uses Spy++ to get control id’s etc. for WPF we can use the utility, Snoop and for the automation id use the name of the control, for example

var searchBox = pf.Get<TextBox>(
   SearchCriteria.ByAutomationId("SearchBox"));

where SearchBox is the name associated with the control.

References

http://teststackwhite.readthedocs.io/en/latest/
https://github.com/TestStack/White