visual-studio-2013coded-ui-testsdata-driven-testsdata-driven

Replacing recorded values with Data file - Data driven testing


I have recorded a simple Coded UI test from visual Studio 2013. What it does is;

Now, I wish to use a data.csv file to replace those 8 values. All the searching I did, I could only find options where every input field had different method hence was easy to find and replace values. In my case, 1 method and 8 fields.

How do I do that? Where do I make the changes since my main file looks like this:

enter image description here

Where and what changes should I make to use CSV file instead of manual values that I recorded.

My designer file code is as below for couple of input fields:

// Type '123456789' in 'i' text box
uIIEdit.Text = this.createKundeParams.UIIEditText; 

// Type '{Tab}' in 'i' text box
Keyboard.SendKeys(uIIEdit, this.createKundeParams.UIIEditSendKeys, ModifierKeys.None);

// Type 'Jeff Hardy' in 'name_i' text box
uIName_iEdit.Text = this.createKundeParams.UIName_iEditText;

// Type '{Tab}' in 'name_i' text box
Keyboard.SendKeys(uIName_iEdit, this.createKundeParams.UIName_iEditSendKeys, ModifierKeys.None);

Solution

  • Create the CSV file. Add the CSV file to the project. Make sure the CSV file is deployed. Add the CSV file as a data source for an individual test. Read the CSV fields and use them in the test. The detailed steps, with some variations, are explained below.

    Visual Studio 2010 has a "data source wizard" that does some of these steps. Visual Studio versions 2012 and 2013 do not have the wizard and so all the steps have to be done manually.

    Create the CSV file

    One way is to create the file in a spreadsheet then save it as Comma Separated Values. Another way is to use a text editor and just write the file. I use a spreadsheet program for big data source files and a text editor for creating small files. Some editors add a byte order mark (BOM) at the start of a file, that will be added to the first field name of the CSV which appears to make the field unreadable. See this page for more about the BOM.

    Add the CSV file to the project

    Use the context menu in solution explorer, select Add -> Existing Item. Then browse to the required file. Note the file filter will probably need to be altered to be . or *.csv.

    Make sure the CSV file is deployed

    Open the properties panel for the CSV file from solution explorer. Set "Copy to output directory" to "Copy if newer" or to "Copy always". Some documents recommend "Copy if newer" but I prefer "Copy always" as occasionally a file was not copied as I expected. The difference between the two copy methods is a little disk space and a little time, but disks are normally big and the time to copy is normally small. Any savings are, in my opinion, far outweighed by being sure that the file will be copied correctly.

    Add the CSV file as a data source for an individual test

    Replace the [TestMethod] attribute with the correct data source line. This Microsoft blog shows the replacement code for several possible data source file types. For CSV use:

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
        "|DataDirectory|\\data.csv", "data#csv",
        DataAccessMethod.Sequential), DeploymentItem("data.csv"),
        TestMethod]
    

    Note that the file name occurs three times and one copy has a # rather than a .. I have not found any useful documentation about the different fields of the Datasource(...) attribute so cannot advise further on how to choose values for non-CSV data sources.

    The |DataDirectory| part above is replaced by the directory where files are deployed when the tests run. The whole file name within the string quotes could be replaced by a full path name of a file, if required.

    Read the CSV fields and use them in the test

    The Coded UI record and generate tool creates classes with fields that hold values entered into text boxes or used in assertions. Each action method has a ...Params class and each assert method has an ...ExpectedValues class, where the ... is the method name. The default values of these fields are the values used when the test was recorded. The recorded values can be overwritten by an assignment before the action or assertion method is called. The fields of the current row of the data source are accessed from TestContext.DataRow[...].

    Suppose a Coded UI test has an EnterValue method that writes text into two fields of the screen and it also has a CheckResult method that asserts one field. The test method might then be written as follows.

    [DataSource...
        TestMethod]
    public void CodedUITestMethod1()
    {
        this.UIMap.EnterValueParams.UIItem0TextSendKeys = TestContext.DataRow["ValueOne"].ToString();
        this.UIMap.EnterValueParams.UIItem1TextSendKeys = TestContext.DataRow["ValueTwo"].ToString();
        this.UIMap.EnterValue();
    
        this.UIMap.CheckResultExpectedValues.UIItem0TextDisplayText = TestContext.DataRow["Result"].ToString();
        this.UIMap.CheckResult();
    }
    

    The ...Params and ...ExpectedValues classes allow the test to create values when the test runs. For example, if the EnterValue method also wanted to write tomorrow's date into a field we could add the following line before it is called: