Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Time-Stamping Test Case Results

When writing test results to files or folders, you want to time-stamp folders or files so you can distinguish the results of different test runs. Using the DateTime.Now property in DateTime class, we can append time stamp info to a folder name or file name. This article explains how to implement time stamps. In this example,we create a file with a time stamp in the file name. 

 

Step 1: in your .Net project, add a class file named "WriteAFile" and write the following codes. 

 
using System;
using System.IO;
namespace Com.Example.Csharp
{
    class WriteAFile
    {
       public static void WriteToFile(String value, String filename,String fileExtention)
        {
            //opening txt file
            var file = new FileStream(filename+"."+fileExtention, FileMode.OpenOrCreate);
            var sw = new StreamWriter(file);
            //writing to the file
            sw.WriteLine(value);
            //closing file
            sw.Close();
        }
    }
}

Step 2: write the following code in the Main method of the Program file.

using System;

namespace Com.Example.Csharp
{
    class Program
    {
        private static void Main(string[] args)
        {
            //Create time stamp string
            String timeStamp = DateTime.Now.ToString("s");
            timeStamp = timeStamp.Replace(":", "_");
            Console.WriteLine(timeStamp);
            // Write a file with time stamp
            WriteAFile.WriteToFile("This is a test for time stamp "+timeStamp,"Result"+timeStamp,".txt");
            Console.ReadLine();
        }
    }
}

Step 3: run the Program above and see the result as shown below. A file is created with a time stamp in the file name. File content has the time stamp string.

Result2015-07-20T22_30_41.txt
This is a test for time stamp 2015-07-20T22_30_41