How to: Generate a Coded UI Test by Recording the Application Under Test

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Recording the user interface elements is an efficient way to create coded UI tests.

Generate a Coded UI Test by Recording the Application Under Test

To generate a coded UI test by recording the application under test

  1. In Solution Explorer, right-click a test project, point to Add, and then click Coded UI Test.

    - or -

    In the Test List Editor or the Test View window, right-click the surface of the window and then click New Test. In the Add New Test dialog box, click Coded UI Test and then click OK.

    - or -

    On the Test menu, click New Test. In the Add New Test dialog box, click Coded UI Test and then click OK.

    The New Test Project dialog box appears.

  2. Type a name for the new coded UI test and click Create.

  3. Click Record actions, edit UI map or add assertions.

    The Coded UI Test Builder dialog box appears.

  4. To start recording, click the Record icon. Perform the actions that you want to record in your application including starting the application if required.

  5. To view the actions, click the Show Recorded Steps icon

    The actions are displayed in the Coded UI Test Builder - Recorded Actions dialog box.

    NoteNote

    If you do not want to record the actions to start your application under test, you must start your application before you click the Record icon.

  6. To finish recording, click the Generate Code icon. Type a name for your coded UI test method in Method Name, and then click Add and Generate.

    This generates code as follows if the name that you entered is, for example, AddTwoNumbers:

    • Adds the controls and to your UI map (UIMap.uitest)

    • Adds a method called AddTwoNumbers to your UI map. You can view the method in the UIMap.Designer.cs file. This method performs the actions that you recorded when you run the test .

public void AddTwoNumbers()
        {
            #region Variable Declarations
            WinEdit textInput1Edit = this.DemoCalculatorWindowWindow.InputNumber2Window.TextInput1Edit;
            WinEdit textInput2Edit = this.DemoCalculatorWindowWindow.TextInput2Window.TextInput2Edit;
            WinButton addButton = this.DemoCalculatorWindowWindow.AddWindow.AddButton;
            #endregion

            // Launch '%USERPROFILE%\Desktop\SimpleWinformsCalculator.exe'
            ApplicationUnderTest demoCalculatorWindowWindow = ApplicationUnderTest.Launch(this.AddTwoNumbersParams.DemoCalculatorWindowWindowExePath, this.AddTwoNumbersParams.DemoCalculatorWindowWindowAlternateExePath);

            // Type '3' in 'textInput1' text box
            textInput1Edit.Text = this.AddTwoNumbersParams.TextInput1EditText;

            // Type '4' in 'textInput2' text box
            textInput2Edit.Text = this.AddTwoNumbersParams.TextInput2EditText;

            // Click 'Add' button
            Mouse.Click(addButton, new Point(83, 18));
        }

  • Adds a test method to your coded UI test file that calls the AddTwoNumbers method

[TestMethod]
        public void CodedUITestMethod1()
        {
            // To generate code for this test, select "Generate Code" from the shortcut menu and select one of the menu items.
            this.UIMap.AddTwoNumbers();
        }


When you choose a name for the recorded method, choose a name that is descriptive for the actions that you recorded.

public void AssertForAddTwoNumbers()
        {
            #region Variable Declarations
            WinEdit textAnswerEdit = this.DemoCalculatorWindowWindow.AnswerWindow.TextAnswerEdit;
            #endregion

            // Verify that 'textAnswer' text box's property 'Text' equals '40'
            Assert.AreEqual(this.AssertForAddTwoNumbersExpectedValues.TextAnswerEditText, textAnswerEdit.Text);
        }

  • Adds a call to the assert method AssertForAddTwoNumbers to the test method in your coded UI test file

[TestMethod]
        public void CodedUITestMethod1()
        {
            // To generate code for this test, select "Generate Code" from the shortcut menu and select one of the menu items.
            this.UIMap.AddTwoNumbers();
            this.UIMap.AssertForAddTwoNumbers();

        }

When you choose a name for the method that has your assert statements, choose a name that is descriptive for these assertions that you created.

See Also

Tasks

Concepts

Other Resources

Page view tracker