c# - How to Store Data Read From CSV into Variables -


i have written program reads in data csv file.

the code reads in data copied below.

however, following:

(1) store values variables can used in selenium automation script.

some considerations:

i think needs done include split method in while loop treats comma (",") separation between each value. however, not sure how correctly implement this. moreover, new programming, if code example on how to this, beneficial.

i need skip first line contains header identifies data.

last, because each row of csv represents data (1) test scenario, loop execute until last row of csv has been read.

// read data 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();      } } 

if understand task correctly, 1 of many ways can accomplish it: 1. create class stores required data 1 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 required specified 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 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 using block:

        int linescount = 0;     list<testinfo> mytests = new list<testinfo>(); 
  2. inside loop in using block:

            if (linescount > 0)         {             linescount++;             try             {                 mytests.add(new testinfo(row));             }             catch (exception ex)             {                 // create log record or/and else report corrupted csv line             }         }         linescount++; 

so, after parsing csv rows have list of objects data automated tests.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -