You can use a variety of techniques to test the user interface for Windows® applications. For example, you can use the System.Reflection class to test microsoft®.net Framework applications, or you can use unmanaged C + +, C #, or Visual basic® to invoke Win32®api by using the P/invoke mechanism Functions, such as FindWindow, to test. NET and native applications.
In this month's column, I'll explain how to start UI test automation using the new Microsoft UI Automation library, which is part of Windows presentation Foundation (WPF), contained in the. NET Framework 3.0. 。 You can see it as a successor to the Microsoft Active Accessibility (MSAA) library, which was originally designed for accessibility functionality, but found it useful for UI automation as well. On the other hand, the UI Automation library is specifically designed for accessibility and UI test automation tasks from the outset. You can use the UI Automation library to test hosts that run operating systems that support the. NET Framework 3.0 (for example, Windows XP, Windows vista®, Windows server®2003, and Windows Server 2008) Win32 applications,. NET Windows forms applications, and WPF applications.
In turn, I think the development of the UI Automation library is the most significant advance in the field of test automation to date and will quickly become the most commonly used technology in Windows application UI test Automation. The UI Automation library is more powerful, often easier to use, and more consistent than other alternative methods for UI Automation. Just as the. NET Framework has changed software application development, I think the UI Automation library is highly likely to revolutionize UI test automation.
A good way to explain my point of view is through a screenshot. Figure 1 shows a simple Windows application that I'm going to test. The application, called Statcalc, calculates the arithmetic, geometric, or harmonic averages of a set of integers. Arithmetic averages are just simple averages. For example, an arithmetic average of 30 inches and 60 inches is 45 inches. Geometric averages are used for proportions. For example, the geometric average of 30:1 and 60:1 is 42.4264:1. Harmonic averages are used for ratios. For example, the average of 30 mph and 60 mph in excess of a fixed distance is 40 miles per hour.
Figure 1 Sample application in UI test automation
The UI test automation shown in Figure 1 is a console application that launches the tested applications, uses the UI Automation library to get references to application and user controls in the application, simulates user input 30 and 60, and simulates clicking the Calculate button control. The test automation then examines the final state of the application by examining whether the resulting TextBox control is the expected value, and then outputting the pass or fail results. I capture a screenshot before I test automation to close the application being tested.
In the remainder of this column, I'll brief you on the Statcalc Windows application that you tested, how to start the application you tested, how to use the UI Automation library to get references to application controls and user controls, and how to simulate user actions and view application state. I'll also explain how to extend and modify the test systems described here to meet your own needs. I'm sure you'll find that the ability to use this new UI Automation library is a good addition to your set of test tools.
About Statcalc
Let's take a brief look at the application being tested, so that you understand the goal of testing automation. You'll also learn why it's useful to be able to access the application's source code when you're writing UI tests, but not absolutely necessary.
The Statcalc application is a simple windows-based form. I use C # to write code for an application, but the UI Automation library can work with WIN32 applications and WPF applications. For the sake of simplicity, I accept the default control name for Visual studio®, including Form1, Label1, TextBox1, GroupBox1, RadioButton1, RadioButton2, RadioButton3, Button1 and TextBox2. I used the MenuStrip control (provided in the. NET Framework 2.0 and later) rather than the earlier MainMenu controls, which added top-level menu items: "File" and "Help." The functionality of the Statcalc application is contained in the Button1_Click method, as shown in Figure 2.