Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Method for taking a screenshot with WebDriver.
using System;
using System.Drawing.Imaging;
using System.IO;
using NUnit.Framework;
using OpenQA.Selenium;
namespace EbRevolution.UAT.Tests.Tests
internal class Screenshot
private int _screenshotCount;
public void TakeScreenshot( IWebDriver driver, string fileName = "" )
var screenshotDriver = driver as ITakesScreenshot;
if( screenshotDriver == null ) return;
var currentDirectory = Directory.GetCurrentDirectory();
var testName = TestContext.CurrentContext.Test.Name;
// If no name is given it will default to TESTSUITE_TESTCASE_COUNT.png.
if( String.IsNullOrWhiteSpace( fileName ) )
fileName = string.Format( "{0}_{1}_{2:00}.png", GetType().Name, testName, _screenshotCount );
Console.WriteLine( "Taking screenshot." );
var screenshot = screenshotDriver.GetScreenshot();
Console.WriteLine( "Saving screenshot {0}.", fileName );
screenshot.SaveAsFile( currentDirectory + "\\" + fileName, ImageFormat.Png );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment