我希望能够不断更新CSV或Excel xlsx文件,也许真的使用NPOI或其他任何东西。我只需要完成以下任务。
我的代码示例如下,我正在捕获时间。该代码正在使用Selenium。每次运行测试时,我都需要将这些计时导出到我将存储在C:驱动器上的现有excel / csv文件中。我不想覆盖现有的单元格,而是每次都想在下一个空白行上添加新的计时。
我现有的excel文件将只有3个标题栏。这些将被称为“测试日期”,“测试名称”,“经过时间”
[Test]
public void TimeForWebPagesToLoad()
{
IWebDriver driver = new FirefoxDriver();
DateTime currentDateTime = DateTime.Now;
var dateTime = currentDate.ToString();
var sw1 = Stopwatch.StartNew();
driver.Navigate().GoToUrl("http://www.google.com");
sw1.Stop();
Console.WriteLine("Time for Google to load is {0}", sw1.Elapsed);
var sw2 = Stopwatch.StartNew();
driver.Navigate().GoToUrl("http://www.facebook.com");
sw2.Stop();
Console.WriteLine("Time for Facebook to load is {0}", sw2.Elapsed);
/*TODO code to write the Elapsed time into excel
WriteToExcelMethod(dateTime, "Google Perf Test", sw1.Elapsed)
WriteToExcelMethod(dateTime, "Facebook Perf Test", sw2.Elapsed)
*/
Assert.IsTrue(sw1.Elapsed < TimeSpan.FromSeconds(10));
Assert.IsTrue(sw2.Elapsed < TimeSpan.FromSeconds(10));
}
这样的事情会起作用:
public static void WriteToExcelMethod(DateTime dt, string str, TimeSpan ts)
{
string path = @"c:\temp\MyTest.csv";
string line = String.Format(@"""{0}"",""{1}"",""{2}""", dt, str, ts);
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(line);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句