1

I have written a program that reads in data from a csv file.

The code that reads in the data is copied below.

However, I would like to do the following:

(1) Store the values to variables so that they can be used in a Selenium Automation Script.

Some Considerations:

I think what needs to be done is to include a Split Method in the While Loop that treats the comma (",") as the separation between each value. However, I am not sure how to correctly implement this. Moreover, as I am new to programming, If I could get a code example on how to to this, that would be most beneficial.

I would also need to skip the first line as it contains a header that identifies the data.

Last, because each row of the csv represents data for (1) test scenario, I would like the loop to execute until the last row of the csv has been read.

// Read data from CSV file
using (CsvFileReader reader = new CsvFileReader("C:\\Data\\Test_Resources\\Data_Sheets\\Adactin\\Data_Input.csv"))
{
    CsvRow row = new CsvRow();
    while (reader.ReadRow(row))
    {
        foreach (string s in row)
        {
            Console.Write(s);
            Console.Write(" ");
        }
        Console.WriteLine();
     }
}
kaveman
  • 4,339
  • 25
  • 44
Kenito
  • 53
  • 2
  • 10
  • 1
    What is `CsvFileReader` - is this a class you created? I ask because MS provides a CSV reader, albeit not in an obvious namespace (for C# devs anyway). Check out [How to: Read From Comma-Delimited Text Files in Visual Basic](http://msdn.microsoft.com/en-us/library/cakac7e6.aspx) but know that you can use `Microsoft.VisualBasic.FileIO.TextFieldParser` in C# projects just as well – kaveman Dec 02 '14 at 01:36
  • also, this is a potential duplicate: http://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array – kaveman Dec 02 '14 at 01:38
  • so, are you asking how to use something you found on some other website? – Ňɏssa Pøngjǣrdenlarp Dec 02 '14 at 01:46
  • I am asking how to store values into variables based on code that I have that works -- in other words, it reads in the data successfully. What I'd like to do next is to store those values into variables. – Kenito Dec 02 '14 at 02:00
  • By "reading CSV into variables" do you mean assigning the values from the file to a class? If so then do you know the structure of the csv (data types it is using and names of the columns) beforehand? Or do you want the structure of the class created dynamically depending on the csv? – PiotrWolkowski Dec 02 '14 at 03:09

1 Answers1

2

If I understand your task correctly, this is one of the many ways you can accomplish it: 1. Create a class that stores all the required data for one test:

    class TestInfo
    {
        static char[] delimiters = new char[] { ',' };
        static int mRequiredCount = -1;

        public string TestID { get; private set; }
        public int Variable1 { get; private set; }
        public float Variable2 { get; private set; }
        //......other variables that are required to be specified for the test

        public TestInfo(string lineFromCSVfile)
        {
            string[] segments = lineFromCSVfile.Split(delimiters);
            if(mRequiredCount < 0)
                mRequiredCount = this.GetType().GetProperties().Length;
            if (segments.Length < mRequiredCount)
                throw new Exception(string.Format("Cannot extract required test data from CSV line {0}", lineFromCSVfile));
            else
            {// NB! exception InvalidStringFormat can happen below during parsing
                TestID = segments[0].Trim();
                Variable1 = int.Parse(segments[1].Trim());
                Variable2 = float.Parse(segments[2].Trim());
                //........... parse other variables here ............
            }
        }
    }
  1. Before your using block:

        int linesCount = 0;
        List<TestInfo> myTests = new List<TestInfo>();
    
  2. Inside your loop in using block:

            if (linesCount > 0)
            {
                linesCount++;
                try
                {
                    myTests.Add(new TestInfo(row));
                }
                catch (Exception ex)
                {
                    // Create a log record or/and do something else to report the corrupted CSV line
                }
            }
            linesCount++;
    

So, after parsing all the CSV rows you will have a list of objects with data for your automated tests.