4

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.

AdrianHHH
  • 12,664
  • 16
  • 48
  • 79
Inky1980
  • 51
  • 1
  • 1
  • 6
  • possible duplicate of [Parsing CSV files in C#](http://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-sharp) – Tarec May 05 '14 at 09:30

3 Answers3

10

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:

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

this.UIMap.EnterValueParams.UIItem2TextSendKeys = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
Community
  • 1
  • 1
AdrianHHH
  • 12,664
  • 16
  • 48
  • 79
  • `The |DataDirectory| part above is replaced by the directory where files are deployed when the tests run` ... how can I determine that directory? – Scott Baker Jan 08 '19 at 00:08
  • @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. – AdrianHHH Jan 08 '19 at 13:20
3

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 : http://blogs.msdn.com/b/mathew_aniyan/archive/2009/03/17/data-driving-coded-ui-tests.aspx

N-ate
  • 3,202
  • 26
  • 37
Kumar Manish
  • 3,638
  • 3
  • 37
  • 40
  • 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. – Inky1980 May 05 '14 at 09:51
  • 1
    @N-ate Are able to provide a link to any Microsoft documentation about the first data row determining the type? – AdrianHHH May 25 '18 at 12:21
  • 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. – N-ate May 25 '18 at 18:49
2

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();
UIMap.Login();

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

Justin
  • 384
  • 3
  • 7
  • Thank you kindly, I'm going to give this a go. – Inky1980 May 06 '14 at 06:47
  • Just noticed in this [link](http://msdn.microsoft.com/en-us/library/dd380782.aspx) 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? – Inky1980 May 06 '14 at 08:16
  • Yes, when you're inside the UIMap UI, right click on a method to "Move Code". See the attached link [link](http://msdn.microsoft.com/en-us/library/gg269469.aspx) – Justin May 06 '14 at 15:57