Best way to take screenshots of tests in Selenium 2?
To do screenshots in Selenium 2 you need to do the following
driver = new FireFoxDriver(); // Should work in other Browser Driversdriver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk");Screenshot ss = ((ITakesScreenshot) driver).GetScreenshot();//Use it as you want nowstring screenshot = ss.AsBase64EncodedString;byte[] screenshotAsByteArray = ss.AsByteArray;ss.SaveAsFile("filename", ImageFormat.Png); //use any of the built in image formatingss.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 clrclr.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
var driver = new InternetExplorerDriver();driver.Navigate().GoToUrl("http://www.google.com");var ss = driver.GetScreenshot(); ss.SaveAsFile("ss.png", System.Drawing.Imaging.ImageFormat.Png);
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();