Monday, March 5, 2012

C# - Taking Screenshots

Here's some code I'm using to take screenshots in my Selenium tests:
            // Get Screenshot
            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            string screenshot = ss.AsBase64EncodedString;
            byte[] screenshotAsByteArray = ss.AsByteArray;
            ss.SaveAsFile("test.jpg", ImageFormat.Jpeg);

The file is saved to the bin\debug folder for your VS project.

I think you only need to add the System.Drawing reference, and add this to the class:
using System.Drawing;
using System.Drawing.Imaging;