Take Screenshots in Selenium using c#

A simple code to take screenshot

 

 class SaveScreenShotClass
{
public static string  SaveScreenshot(IWebDriver driver, string ScreenShotFileName) // Definition
{
var folderLocation = ("Path to save the Screenshots");

if (!System.IO.Directory.Exists(folderLocation))
{
System.IO.Directory.CreateDirectory(folderLocation);
}

var screenShot = ((ITakesScreenshot)driver).GetScreenshot();
var fileName = new StringBuilder(folderLocation);

fileName.Append(ScreenShotFileName);
fileName.Append(DateTime.Now.ToString("_dd-mm-yyyy_mss"));
//fileName.Append(DateTime.Now.ToString("dd-mm-yyyym_ss"));
fileName.Append(".jpeg");
screenShot.SaveAsFile(fileName.ToString(), System.Drawing.Imaging.ImageFormat.Jpeg);
return fileName.ToString();
}
}


 

Usage is verysimple, just call this method and pass the driver &  file name as the parameter.

SaveScreenShotClass.SaveScreenshot(Driver, "FileName");

 

Leave a Reply

Your email address will not be published. Required fields are marked *