In preparation for my talk next Saturday, I am publishing the code that I use to help drive my WinForms application via NUnit tests. The idea is simple — instantiate the form and use extension methods on the controls to find the controls and drive from there.
This is a preliminary release. I will update this file as I find more functionality s needed. It turns out that you don’t need much (besides some creativity) to automate WinForms UIs. This set of extension methods will help you get started.
Download WAX — Winforms Automation Extensions
To run your forms in NUnit, you just need create/show it in the setup and close it in the teardown:
Here is an example of finding a button by name, clicking on it and validating that the app changed the text of the label:
Note that the FindByName method is just a specialization of the Find method. The Find method takes a delegate you can use to find your controls using any method you wish. For instance, say you want to find a control with a known tag:
In my experience, driving the controls directly is the easiest way to automate the UI. For instance, if you have a list control that you want to set, you might write code like this:
If you have any questions or thoughts about this code, please contact me. I want to grow this code over time, and feedback is very welcome.