Best way to take screenshots of tests in Selenium 2?

68,685

Solution 1

To do screenshots in Selenium 2 you need to do the following

driver = new FireFoxDriver(); // Should work in other Browser Drivers
driver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk");
Screenshot ss = ((ITakesScreenshot) driver).GetScreenshot();

//Use it as you want now
string screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
ss.SaveAsFile("filename", ImageFormat.Png); //use any of the built in image formating
ss.ToString();//same as string screenshot = ss.AsBase64EncodedString;

That code should work, as I quickly tested it in IronPython Repl. See the IronPython code below

import clr
clr.AddReference("WebDriver.Common.dll")
clr.AddReference("WebDriver.Firefox.dll")
from OpenQA.Selenium import *
from OpenQA.Selenium.Firefox import *
driver = FirefoxDriver()
driver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk")
s = driver.GetScreenshot()
s.AsBaseEncodedString
# HUGE string appears in the REPL

Solution 2

var driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
var ss = driver.GetScreenshot();   
ss.SaveAsFile("ss.png", System.Drawing.Imaging.ImageFormat.Png);

Solution 3

I don't know if it matters, but I ended up having to cast the driver when i was writing in c#.

something like:

Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();

Solution 4

Just use the extension method TakeScreenshot() in one line of code.

IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("Your_Homepage_Url");

driver.TakeScreenshot().SaveAsFile("file_name_string", ImageFormat.Jpeg);

Solution 5

  1. Add a reference of System.Drawing in your solution/project.
  2. Use System.Drawing.Imaging namespace in your test.

Here I am capturing the screen shot of Facebook Home page.

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using NUnit.Framework;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;

namespace FacebookRegistrationUsingC_Sharp
{
    [TestFixture]
    public class ScreenShot
    {
        IWebDriver driver = null;
        IWebElement element = null;

        [SetUp]
        public void SetUp()
        {
            driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32");           
            driver.Navigate().GoToUrl("https://www.Facebook.com");
            driver.Manage().Window.Maximize();

        }
        [Test]
        public void TestScreenShot()
        {           

            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile("e:\\pande", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        [TearDown]
        public void TearDown()
        {
            driver = null;
            element = null;
        }
    }
}
Share:
68,685
James
Author by

James

Please check out my Android apps: https://market.android.com/developer?pub=JVavrik

Updated on January 09, 2020

Comments

  • James
    James over 4 years

    I need a way to take screenshots of my functional tests. Right now I'm using Selenium 2 with C# bindings. I pretty much want to take a screenshot at the end of the test to make sure the desired page is displayed. Are there any particular tools you guys know of that I can incorporate into my C# code that will trigger a screenshot? I couldn't find a built-in Selenium 2 solution (without looking it over).

  • AutomatedTester
    AutomatedTester almost 14 years
    those calls are Selenium 1 specific
  • AutomatedTester
    AutomatedTester almost 14 years
    my answer below shows how to do it with Selenium 2. P.s. I didnt vote you down
  • James
    James over 13 years
    Thanks! So I have the string for the filename "C:\\..path..\\screen.png", but it never appears there.
  • MacGyver
    MacGyver about 12 years
    Replace this code above: "Screenshot ss = driver.GetScreenshot();" ...with this code: "Screenshot ss = ((ITakesScreenshot)webDriver).GetScreenshot();"
  • MacGyver
    MacGyver about 12 years
    If using .NET 4.0 and Selenium .NET 4.0 drivers, you will need to explicitly add a reference to System.Drawing (Add Reference > .NET tab > System.Drawing) if you created your class library project in Visual Studio 2010 ... then call this to save the file somewhere: "ss.SaveAsFile(@"C:\ss.png", System.Drawing.Imaging.ImageFormat.Png);"
  • djangofan
    djangofan over 10 years
    If your running on Grid, you will have to use the Augmenter class.
  • thecodefish
    thecodefish about 8 years
    Note that the TakeScreenshot() method is an extension method that requires the Selenium.Support NuGet package to be installed.
  • Samuel Rondeau-Millaire
    Samuel Rondeau-Millaire about 7 years
    ImageFormat.Png is now deprecated, use ScreenshotImageFormat.Png instead
  • TotPeRo
    TotPeRo over 6 years
    @Deeb ImageFormat now is deprecated use ScreenshotImageFormat instead.
  • TotPeRo
    TotPeRo over 6 years
    @Toolkit ImageFormat now is deprecated use ScreenshotImageFormat instead.
  • Bluetree
    Bluetree over 6 years
    Please explain your answer covering exactly how this approach achieves the desired output and why it will work for. This would really help the OP's knowledge.
  • HenryChuang
    HenryChuang over 4 years
    ss.SaveAsFile("D:\\screenshot", OpenQA.Selenium.ScreenshotImageFormat.Jpeg);