This situation generally arises whenever there is a failure in the application. This will help developers know the areas of changes which has caused the issue.
To apprehend the problem we are using the Interface ITakesScreenshot method to take a full-screen screenshot of the browser in required format.
Sometimes you may need to take a screenshot of a single element.
public void TakeScreenshotOfElement(IWebDriver driver, By by, string fileName)
{
// 1. Make screenshot of all screen
var screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
IWebElement element = driver.FindElement(by);
var cropArea = new Rectangle(element.Location, element.Size);
var bitmap = bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
bitmap.Save(fileName);
}
First we make a full-screen screenshot then we locate the specified element by its location and size attributes. After that, the found rectangle chunk is saved as a bitmap.
We get a temp file name through the special "Path" .NET class. By default temp files are generated with ".tmp" extension because of that we replace it with ".png".