How To Capture Screenshot Using Selenium WebDriver
Capture Screenshot Using Selenium WebDriver:
Test cases may fail while executing the test cases. While we are executing the test cases manually we just take a screenshot and place in a result repository. The same can be done by using Selenium WebDriver. Here in this post, we see how to capture a screenshot using Selenium WebDriver.
Some of the scenarios we may need to capture screenshot using Selenium WebDriver.
i. Application issues
ii. Assertion Failure
iii. Difficulty to find Webelements on the web page
iv. Timeout to find Webelements on the web page
Must Read: How To Capture Screenshot of Failed Test Cases Using Selenium WebDriver
Syntax to capture and save the screenshot.
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Syntax to store it in our local drive
FileUtils.copyFile(screenshotFile, new File("filename_with_path"));
For example:
FileUtils.copyFile(screenshotFile, new FIle("D:\\screenshot.png"));
Below mentioned script shows how to capture a screenshot using Selenium WebDriver.
package softwareTestingMaterial; import java.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class CaptureScreenshot { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.softwaretestingmaterial.com/capture-screenshot-using-selenium-webdriver"); File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFile, new File("D:\\SoftwareTestingMaterial.png")); driver.close(); driver.quit(); } }
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
Subscribe and get a free eBook and regular updates from SoftwareTestingMaterial.com
Hi RajKumar,
I have implemented the same code in my program. I have resolved all the issues except the screen shot method File Utils Exception. Can You please guide me on the same. i am planning to integrate Extent reports in my project
Hi Azeemuddin Shaik, Check this link