Functional testing, system testing, and acceptance testing of software often involves manual actions by a user. To verify that an application works as designed, a user launches the application, navigates to a specific module, enters some data, and verifies the expected output.
Such testing tends to be expensive in terms of time, labor, and money. In addition, like all activities requiring human intervention, testing in this manner can often be error-prone and inconsistently executed.
Microsoft Visual Studio 2010 Premium and Ultimate contains the Code UI Testing tool that provides a way to automate these tests, so that regression testing can be performed more quickly, efficiently, and consistently.
A Coded UI Test gives developers and testers the ability to create tests that simulate user interactions with an application.
Coded UI Tests are stored in a Test Project. To create a new Test Project in Visual Studio,
- Select File | New | Project… The New Project dialog displays.
- In the Installed Templates panel, expand the Visual C# or Visual Basic node and select the Test category.
- In the Project Type panel in the middle of the New Project dialog, select Test Project.
- Enter a name and location of the Test Project.
- Click the OK button. Visual Studio creates a new Test Project. You can view it in the Solution Explorer.
In our example, the project is named TestProject1. Link: Download TestProject1.
By default, a new Test Project contains a Unit Test class named UnitTest.cs. We won’t need this class, so it is safe to delete it.
Add a Coded UI Test to the project. (Project | Add Coded UI Test…) and provide a meaningful name for the map class.
The Generate Code for Coded UI Tests dialog displays. Select the Record actions, edit UI map or add assertions radio button. Click the OK button.
The Coded UI Test Builder toolbar displays in the bottom right of your screen.
Click the record button () to begin recording your actions. From this point, you can launch an application, navigate to a web site, click buttons, or enter data into forms – pretty much anything you do with your mouse and keyboard will be captured and turned into code by the recorder.
You can stop the recording either by clicking either the Pause button () or the Generate Code button ().
When the recording is paused, you can add an assertion by holding down the left mouse button over the Add Assertion button () and dragging the cursor to a control that contains a property against which you will create an assertion. A blue outline appears around any control as the cursor moves over it. Release the mouse while over the control to create an assertion about that control. An assertion declares an expected property value of the control. Comparing the actual property value against the expected value.
Clicking the Generate Code button generates C# code that reproduces the mouse and keyboard actions that you performed while the recorder was running.
The Generate Code button creates or updates three files in your project. The You can generate code button and create assertions as often as you like. When you close the Coded UI Test Builder, Visual Studio generates three files share a similar name but different extensions: *.uitest, *.cs, and *.Designer.cs. In our TestProject1 demo, the files are named files UIMap.uitest, UIMap.cs, and UIMap.Designer.cs. Because these files are related, UIMap.cs, and UIMap.Designer.cs appear beneath UIMap.uitest within the project hierarchy.
UIMap.uitest contains an XML file with information about the recorded steps. If you don’t have the graphical interface to edit this file, you may want to download the Visual Studio 2010 Feature Pack 2 (http://msdn.microsoft.com/en-US/vstudio/ff655021.aspx ). This XML file is used to generate C# code stored in the UIMap.Deigner.cs file.
Using the Coded UI Coded UI Test Editor, you can view each recorded step, change properties of a step; and split a method into multiple methods.
UIMap.Deigner.cs is overwritten every time any new recordings are created, so you should not edit this file. Instead, move the method you wish to modify into the UIMap.cs file. This file is not overwritten, so your code can store anything you want here. UIMap.cs and UIMap.designer.cs are partial classes of the same class, so it doesn’t matter to the compiler in which file a method, property, or field is located.