Generate a Data Driven Coded UI Test from an Action Recording

One of the very cool new features of VS 2010 and TFS is the ability to turn a manual test into an Automated UI test. As if that wasn’t cool enough how about making it Data Driven and bind it to the original Test Case created by QA.

Using the new Test Manager that comes with VS Ultimate 2010 your test team can create Test Cases using parameters instead of hard coded data values. This allows the manual test to be executed over multiple iterations and validate several scenarios using just one Test Case.

If you have such a Test Case stored in TFS it will look something like this.

image

Notice the @Value1, @Value2 and @Result under the Action and Expected Result. They represent parameters whose values that are listed below, the steps.

Assuming the test team executed this Test Case and created an Action Recording. Which means they let Test Manager record their actions while they ran through a manual test of the application. During the manual test and subsequent recording they will have easily, and possibly without even knowing it bound the Parameters to controls in the application.

From this you can create a Coded UI Test that you can use in part of your arsenal of automated tests against the User Interface.

Here is what you need to do.

Open the Test Project where you want to store the Coded  UI Test. And select Test | New Test… from the Visual Studio menu. On the New Test dialog select Coded UI Test, name it and select the test project to add it to.

image

As the test is being added to the project you will be prompted with a choice to either Record this test yourself of use an existing action recording. Select Use an existing action recording.

image

Next you will be prompted to select a test case. Find and select the test case created by your QA team that contains Parameters and an action recording discussed earlier.

The Coded UI test will be generated for you. Minus the Assertions, we’ll add those ourselves.

You should end up with a Multiplytests class that contains a CodedUITestMethod1(). Rename the method to something more appropriate. In my case I will name it MultiplyTestMethodUI().

The method will look something like this.

image

Notice the reference to Value1 and Value2.

We need to add the assertion to this test.

Place the cursor on a blank line just before the CloseCalc() method call, right click and select Generate Code for Coded UI Test | Use Coded UI Test Builder… from the popup menu.

Visual Studio will minimize and you will see the Coded UI Test Builder.

image

Open the Application and drag the cross hair to the control you want to use to validate the result. In my case it’s the Answer Text Box. You will be presented with a property grid for this control. Select the Text property.

image

Click the Add Assertion button on the property grid toolbar and select AreEqual as the comparator and some value. Then click OK.

image

Click Alt+ G to generate the code for the assertion, when prompted enter a name for the method and click Add and Generate. Stop your recording by clicking on the X in the Coded UI Test Builder. We could have done this all manually. I’m all for letting the tool do it for me. :)

You will notice back in your test method a call will be added to the assertion method you generated.

At this point we want to set the expected value for the assertion to the result from the test case.

Insert a line just before the AssertMultiplicationTest method call and insert the following code.

this.UIMap.AssertMultiplicationTestExpectedValues.UIAnswerTextBoxEditText – TestContext.DataRow[“result”].ToString();

Save and build your solution. Open the Test View window and execute the new Coded UI Test you just created.

Check the test results to see a pass for each Data Row.

image

Take a look at the DataSource Attribute on your test method. It’s pointing back to Test Case 11. Therefore if the test team adds scenarios to their test case your Coded UI Test will run those scenarios also.