Screenshots are important to analyze the execution failures. It helps an automation tester to analyze 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. The 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 screenshots 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.
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.
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).
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver); ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
Viewport Screenshot
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Comments(0)