How to: Create an Automated Negative Test Case Using Coded UI Test

Prerequisites | Steps | Outcome | Further Reading

The following How-to topic will walk you through the creation of an automated negative test case for your web application using Coded UI Test in Visual Studio 2010 Premium or Ultimate edition. The coded UI test performs actions on the user interface (UI) controls and verifies that the UI controls display the expected values. For this topic, the Mileage Stats Reference Implementation (Mileage Stats) will be the targeted application used for testing.

The automated test created in this topic will assert a negative test case scenario. These tests will help to test the error handling capability of the application at the user level.

Prerequisites

This topic requires you to have the same prerequisites required by Mileage Stats:

It is assumed that the Mileage Stats web application has been deployed to a server running Microsoft Internet Information Services (IIS) in debug mode, and that the test site is https://localhost/mileagestats.

Steps

  1. In Visual Studio, create a new Test Project named NegativeTest. To do this, point to New on the File menu, and then click Project. In the New Project dialog, select Test Documents under Test Projects. Set the project's name to NegativeTest, specify a valid location, and then click OK.

    Hh404089.3f6b9fed-218f-4329-877b-1a1484215087(en-us,PandP.10).png

  2. Add a coded UI test. To do this, in Solution Explorer, right-click the NegativeTest project, point to Add, and select New Test. In the Add New Test dialog, select Coded UI Test. Enter the Test name as NegativeCodedUITest and click OK. In the Generate Code for Coded UI Test dialog, select Record actions, edit UI map or add assertions, and click OK.

    Hh404089.b2e4ac6f-580c-49d4-b926-a75a34d57928(en-us,PandP.10).png

  3. Record the UI Test as follows:

    1. Click the red Record button to start recording steps.

      Hh404089.c69f4fa5-ff51-42bb-9f25-aec18c6b3af4(en-us,PandP.10).png

    2. Open Internet Explorer.

    3. Navigate to the Mileage Stats home page.

    4. Click the Sign in or Register button.

    5. On the Mock Authentication page, click the Sign In button.

    6. On the Dashboard, click Add Vehicle button.

    7. Click the Save button on the Add Vehicle form without entering any data.

  4. Click the Record button to stop recording. Click the Show Recorded Steps button to check if the steps were recorded correctly. The Coded UI Test Builder dialog should look like the following screenshot.

    Hh404089.05177344-0de9-438e-9d83-80bda4780634(en-us,PandP.10).png

  5. If there are unexpected steps, you can remove them by right-clicking the step you want to delete and selecting Delete.

  6. Click the Generate Code button. Name the method AddVehiclewithNullData. Click the Add and Generate button. The code will be generated in a file called UIMap.Designer.cs. This code can be customized according to your needs.

    Note

    Note: Each time you generate the code from a recorded method, the code in the UIMap.Designer.cs file will be overwritten.

  7. Use the Coded UI Test Builder to create a validation method to validate properties of the target UI control. For this example, you will verify whether the error message is displayed after the Save button is clicked by following these steps:

    1. Add an assertion to a UI control. To do this, drag the crosshairs onto the UI control in your application that you want to test. When the box outlines your control, release the mouse. For this example, drag the crosshairs to the UINameisrequiredPane UI element, which displays the validation message "Name is required," and release the mouse.

      Hh404089.e71b2900-2a3f-46c6-a915-0ce0e4f88d79(en-us,PandP.10).png

    2. The properties for this control are now listed in the Coded UI Test Builder - Add Assertions dialog box.

      Hh404089.cd70842b-9b6c-461f-a5d7-0a8e42a35000(en-us,PandP.10).png

    3. Right-click the Display Text property, and select the Add Assertion command. Keep the other default values and click OK.

    4. Click the Generate Code button. Name the method VerifyErrorMessage. The VerifyErrorMessage method will be auto-generated and added to the test method in the NegativeCodedUITest.cs file.

      [CodedUITest]
       public class NegativeCodedUITest
       {
         public NegativeCodedUITest()
         {
         }
      
         [TestMethod]
         public void CodedUITestMethod1()
         {
         // To generate code for this test, select "Generate Code for 
         // Coded UI Test" from the shortcut menu and select one of 
         // the menu items.
         // For more information on generated code, see 
         // https://go.microsoft.com/fwlink/?LinkId=179463
            this.UIMap.AddVehiclewithNullData();
            this.UIMap.VerifyErrorMessage();
         }
      ...
      }
      
  8. Modify the generated code as follows:

    1. Copy the code in UIMap.Designer.cs and paste it into UIMap.cs.

    2. In UIMap.cs, if not already present, add the following using statement:

      using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
      
    3. If you want to close the browser window automatically after each test case runs, add a CloseBrowserWindow function in the UIMap.cs partial class as follows:

      public partial class UIMap
       {
         ...
          public void CloseBrowserWindow()
          {
           #region Variable Declarations
            BrowserWindow currentBrowserWindow = this.mUIBlankPageWindowsInteWindow;
           #endregion
      
            currentBrowserWindow.Close();
          }
            ...
       }
      
    4. Add the following code snippet to the NegativeCodedUITest class in NegativeCodedUITest.cs. The TestCleanup attribute in this method marks this method to be executed every time a test method completes its run.

      // Use TestCleanup to run code after each test has run
       [TestCleanup()]
        public void MyTestCleanup()
        {
        // To generate code for this test, select "Generate Code for 
        // Coded UI Test" from the shortcut menu and select one of the   // menu items.
        // For more information on generated code, see  
        // https://go.microsoft.com/fwlink/?LinkId=179463
           this.UIMap.CloseBrowserWindow();
        }
      
  9. Run the test method as follows:

    1. Close all browser windows. Right-click inside the NegativeCodedUITest.cs file and click Run Tests. The coded UI test begins to execute; this will open a browser and will run the application programmatically based on the recorded steps and will assert if the conditions are met. If they are met, the test will pass. Otherwise, they will fail. In the test below, it passes.

    2. Once the test completes, the results are shown in the Test Results window.

      Hh404089.062c63de-9b98-4183-ba78-6ea2edf1ac37(en-us,PandP.10).png

Outcome

Here we created the Automation test project, which can be used to automate UI testing of your web application for negative test cases.

Further Reading

Testing the User Interface with Automated UI Tests on MSDN

How to: Create a Coded UI Test on MSDN

For other topics about testing in Project Silk, see the following:

Next | Previous | Home | Community