0

Ok, to cut a long story short I am doing load testing so I need to add 1000+ user credentials (email, password) to the DB. I have been using automated scripts using selenium and c# and would like to write a script to write this data into a csv file. I have random generators where I can make every email unique. I guess I would need a for loop for this? Anyone got the code for this? Btw once I have the csv file I already have a query to import the csv file into the DB.

So I found a script to add 2 columns but I am a total beginner so I haven't got a clue what to try, all google searches doesn't quite do what I am after even though the job should be quite simple.

public static void WriteToExcelMethod(string email, string passwd)
{
    string path = @"c:\temp\MyTest.csv";
    string line = String.Format(@"""{0}"",""{1}"", email, passwd);
    using (StreamWriter sw = File.AppendText(path))
    {
        sw.WriteLine(line);
    }
}

Is this a better script to write 2 columns and 1000 rows:

//before your loop
    var csv = new StringBuilder();

//in your loop
    var first = reader[0].ToString();
    var second = image.ToString();
    //Suggestion made by KyleMit
    var newLine = string.Format("{0},{1}", first, second);
    csv.AppendLine(newLine);  

//after your loop
Butler
  • 47
  • 6
  • Selenium is going to make a poor load tester. You'd be much better off just sending HTTP requests to your web server – Liam Jul 23 '19 at 10:10
  • Liam please dismiss the load testing I am using JMeter, please focus on my problem of writing to a csv file using selenium or c#. Your deviating I just wanted to give some background as to why I need this csv file. Thanks – Butler Jul 23 '19 at 10:15
  • If it's not relevant why did you put it into the question? – Liam Jul 23 '19 at 10:17
  • Possible duplicate of [Writing data into CSV file in C#](https://stackoverflow.com/questions/18757097/writing-data-into-csv-file-in-c-sharp) – Liam Jul 23 '19 at 10:17
  • What does your data source look like? For instance, a `DataTable` with 2 columns Email and Password or an array of objects? How do you "loop" through them? – Jack Le Jul 23 '19 at 10:31
  • Guys I am sorry for writing an overkill explanation. All I need is to write to a csv file on windows 10 using my automated script selenium(may not be necessary) and c# within Visual Studio 2017. The csv file is in my project with a header for the two columns – Butler Jul 23 '19 at 10:38
  • The code posted reader then entire csv file into the StreamWriter and then adds one line. It does nothing to add any columns. Do you need to add the file to the db before adding new columns? – jdweng Jul 23 '19 at 10:51
  • jdweng no I do not need to add new columns before adding to db because once I have the csv file I have a query to update the db table from the info from the csv file so I just need this physical csv file – Butler Jul 23 '19 at 11:04
  • Guys can you please help me? Its a simple task really just need the code – Butler Jul 23 '19 at 12:40

0 Answers0