Sunday, 21 September 2014

Data Driven Coded UI Tests

After you have created a coded UI test, you can use the steps in the following procedure to add your data source and parameters to your test. This example assumes that you have these code elements in your solution:
  • A coded UI test class named CodedUITest1.
  • A test method named CodedUITestMethod1().
The test method is for a simple calculator application that adds two numbers and verifies that they are added together correctly for this test to pass.
The data source is a .csv file that contains the following data:
Input1
Input2
ExpectedResult
3
4
7
5
6
11
6
8
14
Create the file and add it to your test project.
  1. After adding the data, the file should appear as the following:
    Populate the .CSV file with data
  2. It is important to save the .csv file using the correct encoding. On the FILE menu, choose Advanced Save Options and choose Unicode (UTF-8 without signature) – Codepage 65001 as the encoding.
  3. The .csv file, must be copied to the output directory, or the test can’t run. Use the Properties window to copy it.
    Deploy the .CSV file
    Now that we have the data set created, let’s bind the data to the test.

Create a Data-Driven Coded UI Test

To create a data-driven coded UI test

  1. To bind the data source, add a DataSource attribute within the existing [TestMethod] attribute that is immediately above the test method.


    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), 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.  
        this.UIMap.AddNumbers(); 
    
    } 
  2. The data source is now available for you to use in this test method
  3. In Solution Explorer, open the CodedUITest1.cs file. Make the following changes to the CodedUITestMethod1() method:

  4. Add the following two lines of code before the call to the AddTwoNumbers method to provide values for the numbers to add.
    this.UIMap.AddTwoNumbersParams.TextInput1EditText = 
        TestContext.DataRow["Input1"].ToString();
    this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
        TestContext.DataRow["Input2"].ToString();
    
  5. Add the following line of code before the call to the AssertforAdd method to provide the value for the assert method.
    this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 
        TestContext.DataRow["ExpectedResult"].ToString();
    
    This is how the coded UI test method should look with the parameters and the data source added to it:
    [DeploymentItem("DataDriven.csv"), 
        DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", 
            "|DataDirectory|\\DataDriven.csv", "DataDriven#csv", 
            DataAccessMethod.Sequential), 
        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.
        this.UIMap.AddTwoNumbersParams.TextInput1EditText = 
            TestContext.DataRow["Input1"].ToString();
        this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
            TestContext.DataRow["Input2"].ToString();
        this.UIMap.AddTwoNumbers();
    
        this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 
            TestContext.DataRow["ExpectedResult"].ToString();
        this.UIMap.AssertforAdd();
    }
    


  6. Save the changes to the CodedUITest1.cs source code file.
  7. To run your coded UI test, right-click the coded UI test in the Test View window and click Run Selection.
    After the tests have run, the overall test result for all iterations of the test displays in the Test Results window. To see the details of each iteration, double-click the test in the Test Results window.