So am currently working on a project that I intend to post about soon, and ran into an issue when trying to take screenshots using RemoteWebDriver.
Turns out that RemoteWebDriver doesn’t have the capability that the other drivers have, however after a few searches I managed to find a solution and with a small tweak got it working.

So here is how you can take screenshots using RemoteWebDriver.

You need to create a class that extends RemoteWebDriver and inherits ITakeScreenshot, should look like this:

public class ScreenShotRemoteWebDriver : RemoteWebDriver, ITakesScreenshot
{
public ScreenShotRemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities)
: base(remoteAddress, desiredCapabilities)
{
}
public Screenshot GetScreenshot()
{
// Get the screenshot as base64.
Response screenshotResponse =
this.Execute(DriverCommand.Screenshot, null);
string base64 = screenshotResponse.Value.ToString();
// ... and convert it.
return new Screenshot(base64);
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

The issue I had was that the example I found by Jim Evans, must have been when RemoteWebDriver didn’t have any paramters, so had to include the URI and Capabilities.

To use this now you have to use your ScreenShotRemoteWebDriver instead of RemoteWebDriver, like so:

webDriver = new ScreenShotRemoteWebDriver(new Uri(remoteServer), capabilities);
view raw gistfile1.cs hosted with ❤ by GitHub

Then you can take a screenshot using the following code:

Screenshot ss = ((ITakesScreenshot)webDriver).GetScreenshot();
string screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
ss.SaveAsFile(@"C:\Temp", System.Drawing.Imaging.ImageFormat.Jpeg);
view raw gistfile1.cs hosted with ❤ by GitHub

Note that the screenshots are saved on the machine where the test is being executed from, not the client.

Happy screenshoting!

Update

There has been lots of changes in RemoteWebDriver since I wrote this original post, one of those is that screenshots are now a lot easier as per Jan Zdrahal comment below.

So you can now create an extension for IWebDriver to take a screenshot for you that will also work with RemoteWebDriver.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Drawing.Imaging;
namespace Richard.WebDriverExtensions
{
public static class WebDriverExtensions
{
/// <summary>
/// Take a screenshot of the current page
/// </summary>
/// <param name="driver"></param>
/// <param name="FileLocationName">Full directory plus the file name and format, e.g C:\Temp\Image.jpeg</param>
/// <param name="imageFormat">System image format, I tend to stick to jpeg</param>
public static void TakeScreenshot(this IWebDriver driver, string FileLocationName, ImageFormat imageFormat)
{
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(FileLocationName, imageFormat);
}
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

Then you can call the method like so.

webDriver.TakeScreenshot(@"C:\Temp\Richard.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
view raw gistfile1.txt hosted with ❤ by GitHub

Comments