By Inky1980

2014-05-05 09:29:14 8 Comments

I am trying to automate some testing for one of our web applications and I need to know how I can make my Coded UI project read data from a CSV file. Lets say I want to test a log in screen. My CSV file will contain a few user names and passwords. I want my Coded UI test to read these log in details and loop through them to run the test on each set of data.


@Kumar Manish 2014-05-05 09:44:05

Add Data Source attribute in the Coded UI Test.

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

Note: This datasource driver determines cell type based on the data in the first data row. If you have a column that should be formatted as a string, but the first data row has a numer e.g.1234. The following rows will be returned as 0 if they are not empty.

Hope, this link may help you :

@Inky1980 2014-05-05 09:51:49

Thanks. The only problem I have with this link is that the author mentions the user going into "Test View". Unfortunately that doesn't exist on Visual Studio 2012.

@AdrianHHH 2018-05-25 12:21:22

@N-ate Are able to provide a link to any Microsoft documentation about the first data row determining the type?

@N-ate 2018-05-25 18:49:29

I found no documentation. I converted a suite of almost 600 tests to use this driver and it gave me a huge headache until I added a mock row to the csv that I ignored in the test. eg. "string", 1, "string", "string". The surprise is that the resulting object has a property for errors and it has no indication that anything went wrong.

@AdrianHHH 2014-09-09 10:16:25

The web has many tutorials on data driving Coded UI tests. The basic steps for data driving with a CSV file are as follows.

  • 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:

    "|DataDirectory|\\data.csv", "data#csv",
    DataAccessMethod.Sequential), DeploymentItem("data.csv"),

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.

public void CodedUITestMethod1()
    this.UIMap.EnterValueParams.UIItem0TextSendKeys = TestContext.DataRow["ValueOne"].ToString();
    this.UIMap.EnterValueParams.UIItem1TextSendKeys = TestContext.DataRow["ValueTwo"].ToString();

    this.UIMap.CheckResultExpectedValues.UIItem0TextDisplayText = TestContext.DataRow["Result"].ToString();

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:

this.UIMap.EnterValueParams.UIItem2TextSendKeys = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");

@Scott Baker 2019-01-08 00:08:15

The |DataDirectory| part above is replaced by the directory where files are deployed when the tests run ... how can I determine that directory?

@AdrianHHH 2019-01-08 13:20:34

@ScottBaker The DataDirectory is available in the TestContext when the tests run. The actual location is dynamic and chosen when the test is deployed to be run. Please read about test deployment in the Microsoft pages about Coded UI.

@Justin 2014-05-05 16:18:54

You don't need to go into test view. Simply replace your [TestMethod] with the below script:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\LoginInfo.csv", "Sheet$1", DataAccessMethod.Sequential), DeploymentItem("LoginInfo.csv"), TestMethod]

From there, change the LoginInfo.csv to the name of your .csv file. To reference your data just use:

// Username and Password are Column Headers
UIMap.LoginParams.UserNameTextBox = TestContext.DataRow["UserName"].ToString();
UIMap.LoginParams.PasswordTextBox = TestContext.DataRow["Password"].ToString();

This will take the item in each column and use it sequentially in each test.

@Inky1980 2014-05-06 06:47:55

Thank you kindly, I'm going to give this a go.

@Inky1980 2014-05-06 08:16:38

Just noticed in this link that they say "Do not modify the UIMap.designer.cs file directly. If you do this, the changes to the file will be overwritten." - Any idea where this code should be entered then?

@Justin 2014-05-06 15:57:54

Yes, when you're inside the UIMap UI, right click on a method to "Move Code". See the attached link link

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] How do I get the path of the assembly the code is in?

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

49 Answered Questions

3 Answered Questions

1 Answered Questions

[SOLVED] Load data for Scenario Context from CSV file

2 Answered Questions

Sponsored Content