views:

221

answers:

2

So 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 couldnt find a built-in Selenium 2 solution(unless I looked it over).

A: 

Using selenium there were two calls I was familiar with: captureEntirePageScreenshot and captureScreenshot. You might want to look into those calls to see if they'll accomplish what you're after.

Bobby B
those calls are Selenium 1 specific
AutomatedTester
my answer below shows how to do it with Selenium 2. P.s. I didnt vote you down
AutomatedTester
+3  A: 

To do screenshots in Selenium 2 you need to do

driver = new FireFoxDriver(); // Should work in other Browser Drivers
driver.Navigate().GoToUrl("http://www.theautomatedtester.co.uk");
Screenshot ss = 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;

I should say 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
AutomatedTester
Thanks! So I have the string for the filename "C:\\..path..\\screen.png", but it never appears there.
James