Screenshot In Selenium Using C#

Its very important to take screenshot when we execute a test script. When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed.
It helps us to debug and identify the problem by seeing the screen shot.

In selenium WebDriver, we can take the screen shot using the below command:

SaveScreenShotClass.SaveScreenshot(Driver, “FileName”);

Check the framework example of taking screenshots:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s