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 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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With