Don't Develop GUI Tests, Teach Your App To Test Itself!
How Does It Work?
The basics are very simple: The tested application will be the one running the test code; that's why it is auto-tested. The basic is structure is as follows:
There are two main concepts:
- The application thread is the main GUI thread, the one handling events. This thread executes the "real app code" and potentially collaborates with other worker threads used in the app (depending on the design). This code shouldn't be affected by the test code. It should run as if it was being used by a human user.
- The test thread is the one executing the test code. It will interact with the app simulating a real user, but of course driven by the test code. As the image shows, the test code will eventually perform actions like a
PerformClick()
to run a click on a button or any other control, and in order to do that, it will interact with the application thread usinginvokes
(in the C# world).
Please note that the test code won't invoke the underlying code that the OK button runs; it will perform a click on the OK button instead, which is the way we have to simulate user actions.
Now the question is how the test code discovers what it can do with a certain window or dialog. Take a look at the following figure:
Here comes the trick. Each window will expose an ITesteable
interface, which is the one that the test code will use to perform actions on it.
In fact, each dialog will extend ITesteable
to provide specific functionalities. The Main Window will provide options to navigate different menus (or the menus with the clickable options themselves), some action buttons, and so on. The Create Branch Dialog in the figure will provide ways to get and set the text on the two edit boxes, and a way to click the OK and Cancel buttons.
Of course, each window and dialog will have to be ready to be testable, but we think that is a small price to pay. At the end of the day, code must be testable in order to write unit tests, too.
The second advantage is that, on paper, the same testing code will be valid for the two GUIs: the "create branch dialog" will look different and will use different underlying controls on Mac and GTK. Layouts can be different, styles, fonts, and so on. But at the end of the day, the two will let you click on OK and Cancel and get and set a few text boxes. So if the two implement the same ITesteable
interface (actually a child interface adding more features), the two will be able to be tested with the same code, which is a great advantage for us.
Here's a code snippet:
ITesteablePendingChangesView pendingChangesView = testeablePlastic.GetTesteablePendingChangesView(); pendingChangesView.CollapseRow(0); pendingChangesView.ExpandRow(0); pendingChangesView.ClickCheckboxAtRow(0); pendingChangesView.AddComment(comment); pendingChangesView.ClickCheckinButton();
How Does It Look and Feel?
Since one video is worth a thousand pictures, check out this screencast recorded on a Mac running an initial GUI test suite.