Best way to take screenshots of tests in Selenium 2?
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
- Add a reference of
System.Drawing
in your solution/project. - 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;
}
}
}
James
Please check out my Android apps: https://market.android.com/developer?pub=JVavrik
Updated on January 09, 2020Comments
-
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 almost 14 yearsthose calls are Selenium 1 specific
-
AutomatedTester almost 14 yearsmy answer below shows how to do it with Selenium 2. P.s. I didnt vote you down
-
James over 13 yearsThanks! So I have the string for the filename "C:\\..path..\\screen.png", but it never appears there.
-
MacGyver about 12 yearsReplace this code above: "Screenshot ss = driver.GetScreenshot();" ...with this code: "Screenshot ss = ((ITakesScreenshot)webDriver).GetScreenshot();"
-
MacGyver about 12 yearsIf 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 over 10 yearsIf your running on Grid, you will have to use the Augmenter class.
-
thecodefish about 8 yearsNote that the
TakeScreenshot()
method is an extension method that requires theSelenium.Support
NuGet package to be installed. -
Samuel Rondeau-Millaire about 7 yearsImageFormat.Png is now deprecated, use ScreenshotImageFormat.Png instead
-
TotPeRo over 6 years@Deeb
ImageFormat
now is deprecated useScreenshotImageFormat
instead. -
TotPeRo over 6 years@Toolkit
ImageFormat
now is deprecated useScreenshotImageFormat
instead. -
Bluetree over 6 yearsPlease 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 over 4 yearsss.SaveAsFile("D:\\screenshot", OpenQA.Selenium.ScreenshotImageFormat.Jpeg);