
A few days ago I wrote about my first experiences in using White. I mentioned I wanted to create a facade around the White API in order to avoid constantly retyping the same code for common use cases. Since fluent interfaces are in fashion now, I decided to create a fluent facade. Just joking, of course.
I’ll present the facade through a few tests of a Kosmos GUI, which is currently under construction. Kosmos GUI is a MDI application for displaying maps from OpenStreetMap geo data. You have to load a Kosmos project, which contains information about where to find the data and how to render it. So after starting the application, the first user action would typically be to open the project:

After clicking the File|Open Project menu item, a dialog for selecting the Kosmos project file appears:

One you have selected the project file, a progress box is shown with an option to cancel the operation:

If you let it finish, a map appears and some additional menu items are now available:

Test Code
Here’s a sample test case which goes through the above steps:
[Test]
public void OpenProject()
{
facade.ClickMenu ("File", "Open Project…")
.FileDialog ("Open Kosmos Project File",
@”D:\MyStuff\projects\OsmUtils\trunk\Data\Kosmos\KosmosProjectExample1.xml")
.MainWindow ().ModalDialog ("Loading Kosmos Project")
.MainWindow().Menu ("View", "Zoom In").AssertIsEnabled (true)
.ClickMenu ("View", "Zoom In")
.ClickMenu ("View", "Zoom Out")
.ClickMenu ("View", "Zoom All");
}
After loading the project the test case executes some of the zoom functions available in the menu. And here’s a test case which cancels the project loading (and then checks that the View|Zoom In menu item is still not enabled):
[Test]
public void OpenProjectCanceled ()
{
facade.ClickMenu ("File", "Open Project…")
.FileDialog ("Open Kosmos Project File",
@”D:\MyStuff\projects\OsmUtils\trunk\Data\Kosmos\KosmosProjectExample1.xml")
.MainWindow().ModalDialog ("Loading Kosmos Project").Button ("Cancel").Click()
.MainWindow ().Menu ("View", "Zoom In").AssertIsEnabled (false);
}
Hopefully the code is self-explanatory. The basic idea is for the facade to remember the current window or other UI element to perform actions on. You can change the active window at any moment. The facade is also supplied with some basic assertion methods useful in the test code (they throw InvalidOperationExceptions, so you can use them with any unit test framework).
Oh I almost forgot. This is how you instantiate the facade:
WhiteFacade facade = WhiteFacade.Run (applicationFilePath);
And if the facade does not offer enough for you, you can always access the underlying White’s Application object through facade.Application property.
Download
You can download the latest version of the WhiteFacade class from here. I have to warn you though: the class is in its infancy and will evolve through my effort on testing Kosmos GUI. It provides only for some basic use cases, but I think those common cases represent the majority of the GUI test code. I just wanted to give you an idea on how to make writing GUI test code as painless as possible.