Screenshots are important to analyse the execution failures. It helps an automation tester to analyse what went wrong during execution. As one of the best automation testing services companies, we have also faced several challenges when troubleshooting test automation failures. Screenshot is a great reference document to recheck how the functionalities behaved previously and to identify false negatives from script failures.
In this blog article, we would like to share how to take screenshot in Selenium WebDriver using various commands.
Full Page Screenshot
In Selenium 4, you can take full page screenshot. This feature is very helpful to investigate failures in detail.
1 |
File file = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE); |
Note: As of now, full page screenshot works only for Firefox driver.
Element Screenshot
Sometimes you may need screenshot only for a specific element instead of capturing the entire page.
1 |
driver.findElement(By.id("txt1")).getScreenshotAs(OutputType.FILE) |
Full Page Screenshot using aShot
aShot is a WebDriver Screenshot utility. It takes a screenshot of the WebElement on different platforms (i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser).
1 2 |
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver); ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png")); |
Viewport Screenshot
1 |
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); |