ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!