Capture Screenshot in selenium:
- A screenshot, or screen capture, is a picture taken from your computer’s desktop.
- It may include desktop backgrounds, icons of files and folders, and open windows.
- It is mandatory to take the screenshot for verification so that can prove also that your test case has covered certain functionality or not.
- When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed.
- Selenium can automatically take screenshots during execution.
- You need to type cast WebDriver instance to TakesScreenshot.
When / Why Need to Capture ScreenShot in Selenium WebDriver:
Application issue:
- Sometimes Applications not run properly due to Network Issue, Internet Speed, Cache Issue that Selenium script chances to Fail so, we capture screenshot.
Assertion Failure:
- Assertion’s used to Verify case Pass or Fail.
- In Both Pass or Fail cases we capture screenshot.
Timeout to find WebElements on the web page:
- Sometimes Selenium script throw exception Due to not able to find WebElement that time we capture screenshot to know where bug is found.
To Verify Bug is Valid or Not?
- Sometimes bug is not valid Or need to compare existing project UI / Functionality with Expected one.
Capture the screenshot:
- Using TakesScreenshot and getScreenshotAs capture ScreenShot and Store into File.
1 | File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); |
Store the screenshot:
- Using the help of FileUtils.copyFile we store ScreenShot into Local Device path.
1 | FileUtils.copyFile(screenshotFile, new FIle("D:\\screenshot.png")); |
Scenario’s to Capture ScreenShot:
- setProperty of Browser path to Launch Browser.
- Create driver Instance / Object of WebDriver.
- Wait until Full page load.
- Open URL.
- Maximize Browser Screen.
- TakeScreenShot using getScreenshotAs in-Built option.
- Store captured ScreenShot internal path.
Example: Capture Screenshot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; 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; public class Screen_shot { public static void main(String[] args) throws IOException { // Path of GeckoDriver EXE. System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Go to URL driver.get("https://www.stqatools.com/"); // Maximize Window driver.manage().window().maximize(); // Take ScreenShot File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); // Store Screenshot FileUtils.copyFile(screenshotFile, new File("D:\\screenshot.png")); // Close Driver driver.quit(); } } |
How to Take Screenshot in Selenium WebDriver and Take a screenshot with Selenium WebDriver and selenium take screenshot on failure