Best way to take screenshots of tests in Selenium 2?

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).


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

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();
链接地址: http://www.djcxy.com/p/67384.html

上一篇: 在Selenium中等待页面加载

下一篇: 在Selenium 2中截取测试截图的最佳方式是?