logo logo

Read Data From a CSV File in C#

In this tutorial for Learning to create a test automation framework with C#, Selenium 3 and Nunit , we’ll be adding data driven support to the test automation framework we’ve built and will read data from CSV file in C#.


Tutorial Overview:

Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#

Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3

Class 3 – Utilizing Test Automation Framework with Advanced Capabilities


TestProject Test Automation Tool


 

Data driven is external to your functional tests, it is loaded and used to extend your automated test cases. One of the best examples is that of a customer login form with multiple accounts. To use data-driven testing in this scenario, you might record a single automated test.

In our example, we are going to use an external CSV file which will be our external source of information and we will read data from it in our C# code. To create data driven support, we’ll create a new class named ‘General.cs’ and paste the following code:

using System.Collections.Generic;
using System.IO;
namespace Test
{
    public class General
    {
        public List<string> loadCsvFile(string filePath)
        {
            var reader = new StreamReader(File.OpenRead(filePath));
            List<string> searchList = new List<string>();

            while(!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                searchList.Add(line);
            }

            return searchList;
        }
    }
}

This function receives the path we will decide to work with and retrieves a list of the information from the file. In the example below we’ll be creating another class called ‘Servers’, a similar technology to the Page Object Model. Paste the following code:

namespace Test
{
    public static class Servers
    {
        private static T getServers<T>() where T : new ()
        {
            var servers = new T();
            return servers;
        }

        public static General general
        {
            get { return getServers<General>(); }
        }
    }
}

Congratulations, now our test automation framework is will be able to read data from CSV File in C#, meaning loading CSV files! In the following tutorials we’ll be learning how to incorporate these capabilities in our automated tests. Continue on with the tutorial and Create a Test Suite in Selenium Webdriver and C#.

-Please share your ideas and insights in the comment below!


 

About the author

Asya Galinsky

Comments

13 2 comments
  • bernard milan November 23, 2019, 4:39 pm

    Hey Asya,

    Thanks for the tutorial. I’m really new to test automation and I am wondering how large of a data set do I need to be able to run this?

    • Karen Teboulle November 24, 2019, 1:26 pm

      Hi Bernard, the data set can be of any size you like it to be 🙂
      As a side note – Since you mentioned you are new to test automation – I would recommend checking out TestProject Platform (it’s free) – https://testproject.io/. A great team of test automation engineers is there to guide you through your test automation journey 🙂

Leave a Reply

FacebookLinkedInTwitterEmail