Articles in categories

Now on Amazon

Take a screenshot with Selenium

How to take a screenshot with Selenium.

public static void TakeScreenshot(int number)
{
	// check and/or create the Screenshots directory
	var screenshotDir = Directory.GetCurrentDirectory() + @"\Screenshots";
	if (!Directory.Exists(screenshotDir))
		Directory.CreateDirectory(screenshotDir);

	var failScreen = ((ITakesScreenshot)Cache.Driver).GetScreenshot();
	var fileName = String.Format("screenshot{0}.png", number);
	failScreen.SaveAsFile(screenshotDir + "/" + fileName, ImageFormat.Png);
}

I use it when a 'NoSuchElementException' occurs. Makes it easier to find where the test fails.

catch (NoSuchElementException)
{
	// take a screenshot
	General.TakeScreenshot(ct);
	Console.WriteLine();
	Console.WriteLine(string.Format("NoSuchElementException occured!{0}Product: {1} - {2} - {3} - {4}", Environment.NewLine, ct.Category, ct.CardType, ct.CardSize, ct.Design));
	continue;
}