How to take Screenshots in Selenium

0
24

This tutorial is all about How to take Screenshots in Selenium. In this post we will learn how to take Screenshot in Selenium WebDriver.Taking screenshot during automated run is very important. It is useful in reporting and for better look and feel of the reports.

Below is the code for taking Screenshot:

  1. import java.io.File;
  2. import java.net.URL;
  3.  
  4. import org.openqa.selenium.OutputType;
  5. import org.openqa.selenium.TakesScreenshot;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.remote.Augmenter;
  8. import org.openqa.selenium.remote.DesiredCapabilities;
  9. import org.openqa.selenium.remote.RemoteWebDriver;
  10.  
  11. public class Testing {
  12.  
  13. public void screenshotDemo() throws Exception {
  14. WebDriver driver = new RemoteWebDriver(
  15. new URL("http://localhost:4444/wd/hub"),
  16. DesiredCapabilities.firefox());
  17.  
  18. driver.get("http://www.google.com");
  19.  
  20. // RemoteWebDriver does not implement the TakesScreenshot class
  21. // if the driver does have the Capabilities to take a screenshot
  22. // then Augmenter will add the TakesScreenshot methods to the instance
  23. WebDriver augmentedDriver = new Augmenter().augment(driver);
  24. File screenshot = ((TakesScreenshot)augmentedDriver).
  25. getScreenshotAs(OutputType.FILE);
  26. }
  27. }
Facebook Comments
(Visited 30 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here