Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 743
James Avatar asked Jul 27 '10 17:07

James


People also ask

What is the recommended method to capture a screenshot using WebDriver?

To take a screenshot in Selenium, we use an interface called TakesScreenshot, which enables the Selenium WebDriver to capture a screenshot and store it in different ways. It has a got a method "getScreenshotAs() " which captures the screenshot and store it in the specified location.

Which Selenium class is used to take screenshots?

Selenium webdriver can automatically take screenshots during the execution. But if users need to capture a screenshot on their own, they need to use the TakeScreenshot method which notifies the WebDrive to take the screenshot and store it in Selenium.

Can Selenium IDE take screenshots?

captureScreenshot (name of screenshot) - Selenium IDE command. captureEntirePageScreenshot takes a screenshot of the whole web page. captureScreenshot takes a screenshot of the visible website part (viewport). This command has the side effect of giving the tab that runs the macro the focus.


2 Answers

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 
like image 133
AutomatedTester Avatar answered Sep 21 '22 08:09

AutomatedTester


var driver = new InternetExplorerDriver(); driver.Navigate().GoToUrl("http://www.google.com"); var ss = driver.GetScreenshot();    ss.SaveAsFile("ss.png", System.Drawing.Imaging.ImageFormat.Png); 
like image 40
Toolkit Avatar answered Sep 22 '22 08:09

Toolkit