Capturing a screenshot with Selenium RC

Sometimes it is kind of difficult to know why automation script is failed or was not able to find elements. Test results will just display the element is not found, but we don’t know the real reason. The problems might be with page not fully loaded, with errors on the page or missing elements, etc. In my opinion it would be good to take a picture at the moment when script fails to see the clear picture of what is going wrong. So, I’ve added a simple function to shot a screenshot of active screen. I call this function CaptureScreenshot on writing fail action and it shots a screenshot and returns the image name. The images are stored in \\Visual Studio 2010\Projects\ImageCapture\ImageCapture\bin\Debug


using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;
using System.Drawing;
using System.Drawing.Imaging;
namespace SeleniumTests
{
[TestFixture]
public class ImageCapture
{
private ISelenium selenium;
private StringBuilder verificationErrors;
public static string CaptureScreenshot()
{
Bitmap bmpScreenshot = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
Graphics Screenshot = Graphics.FromImage(bmpScreenshot);
Screenshot.CopyFromScreen(0, 0, 0, 0, new Size(1024, 768));
string name = (System.Convert.ToString(System.DateTime.Now).Replace("/", "_")).Replace(":", "_") + ".jpg";
bmpScreenshot.Save(name, ImageFormat.Jpeg);
return name;
}
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com");
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
}
[Test]
public void TheUntitledTest()
{
selenium.Open("/");
selenium.WindowMaximize();
selenium.Type("q", "selenium wiki");
selenium.Click("btnG");
Thread.Sleep(5000);
try
{
Assert.IsTrue(selenium.IsTextPresent("Selenium Wiki"));
}
catch (AssertionException e)
{
verificationErrors.AppendLine(e.Message + " SCREENSHOT is " + CaptureScreenshot());
}
try
{
// The correct title is "selenium wiki - Google Search" and the script will take the screen shot
Assert.AreEqual("Selenium Wiki - Google Search", selenium.GetTitle());
}
catch (AssertionException e)
{
verificationErrors.AppendLine(e.Message + " SCREENSHOT is " + CaptureScreenshot());
}
}
}
}

The important things to note is that you will have to add the reference System.Drawing and also use using System.Drawing; and using System.Drawing.Imaging;

Submitted By Irina Burdilo

Selenium captureEntirePageScreenshot

Using selenium you can capture the screen shot of the pages when running the selenium script. This is useful when you want to capture the screen shot of the page where you have a failure/ error.

selenium.CaptureEntirePageScreenshot("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\test.png", "");

captureEntirePageScreenshot(filename, kwargs)
Arguments:
* filename – the path to the file to persist the screenshot as. No filename extension will be appended by default. Directories will not be created if they do not exist, and an exception will be thrown, possibly by native code.
* kwargs – a kwargs string that modifies the way the screenshot is captured. Example: “background=#CCFFDD” . Currently valid options:
Background: The background CSS for the HTML document. This may be useful to set for capturing screenshots of less-than-ideal layouts, for example where absolute positioning causes the calculation of the canvas dimension to fail and a black background is exposed (possibly obscuring black text).

Saves the entire contents of the current window canvas to a PNG file. Contrast this with the captureScreenshot command, which captures the contents of the OS viewport (i.e. whatever is currently being displayed on the monitor), and is implemented in the RC only. Currently this only works in Firefox when running in chrome mode, and in IE non-HTA using the EXPERIMENTAL “Snapsie” utility. The Firefox implementation is mostly borrowed from the Screengrab! Firefox extension. Please see http://www.screengrab.org and http://snapsie.sourceforge.net/ for details.

If you found this solution helpful or have something extra to add, feel free to share it here by commenting below.